Laravel 5.0 - Event Scheduling

(This is part of a series of posts on New Features in Laravel 5.0.)

  1. Laravel 5.0 - Form Requests
  2. Laravel 5.0 - ValidatesWhenResolved
  3. Laravel 5.0 - Directory structure and namespace
  4. Laravel 5.0 - Route Caching
  5. Laravel 5.0 - Cloud File Drivers
  6. Laravel 5.0 - Method Injection
  7. Laravel 5.0 - Route Annotations (removed)
  8. Laravel 5.0 - Event Annotations (removed)
  9. Laravel 5.0 - Middleware (Filter-style)
  10. Laravel 5.0 - Environment Detection & Environment Variables
  11. Laravel 5.0 - Event Scheduling
  12. Laravel 5.0 - Commands & Handlers
  13. Upgrading from Laravel 4 to Laravel 5
  14. Bringing Whoops Back to Laravel 5
  15. Laravel 5.0 - Events & Handlers
  16. Laravel 5.0 - Generating Missing Events
  17. Laravel 5.0 - Custom Error Pages
  18. Laravel 5.0 - Eloquent Attribute Casting

Eric Barnes has a longer write up on this, so I'll just keep it short:

Laravel 5.0 is introducing a pretty incredible cron-style scheduler (similar to Indatus' Dispatcher) built into the core. Just point a cron job that's scheduled once a minute at artisan schedule:run and you're good to go.

* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1

For example, you can now bind the following event to clear your auth reminders daily:

$schedule
    ->command('auth:clear-reminders')
    ->daily()
    ->sendOutputTo($logPath)
    ->emailOutputTo('me@me.com');

You can use commmand() to call artisan commands, call to call methods or functions, or terminal() to run command line scripts:

$schedule->call('YourClass@someMethod')->twiceDaily();
$schedule->call(function() {
    // Do stuff
})->everyFiveMinutes();
$schedule->terminal('cp oldThing newThing')->dailyAt('8:00');

You can also use a callback to decide when something will or won't run, using when() or skip():

$schedule
    ->call('Mailer@BusinessDayMailer')
    ->weekdays()
    ->skip(function(TypeHintedDeciderClass $decider)
    {
        return $decider->isHoliday();
    }
);

This is just a quick introduction, though; check out Eric's post for a fuller rundown: Laravel 5 Scheduler on Laravel-News


Comments? I'm @stauffermatt on Twitter


Tags: laravel | 5.0 | laravel 5

Matt Stauffer headshot

Hi, I'm Matt Stauffer.

I'm partner & technical director at Tighten Co.

You can find me on Twitter at @stauffermatt


Like what you're reading?

I wrote an entire 450+ page book for O'Reilly: Laravel: Up and Running.

You can order the eBook or print book today.