Laravel 5.0 - Event Scheduling

Posted on November 20, 2014 | By Matt Stauffer


Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

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