indatus / dispatcher
Schedule your artisan commands within your application's source code
Installs: 693 578
Dependents: 3
Suggesters: 0
Security: 0
Stars: 1 061
Watchers: 39
Forks: 71
Open Issues: 0
Requires
- php: >=5.3.2
- mtdowling/cron-expression: ~1.0.3
- wp-cli/php-cli-tools: v0.9.4-patch46
Requires (Dev)
- mockery/mockery: ~0.9
- orchestra/testbench: ~2.1.1
- phpunit/phpunit: ~4.0
README
Dispatcher allows you to schedule your artisan commands within your Laravel project, eliminating the need to touch the crontab when deploying. It also allows commands to run per environment and keeps your scheduling logic where it should be, in your version control.
use Indatus\Dispatcher\Scheduling\ScheduledCommand; use Indatus\Dispatcher\Scheduling\Schedulable; use Indatus\Dispatcher\Drivers\DateTime\Scheduler; class MyCommand extends ScheduledCommand { public function schedule(Schedulable $scheduler) { //every day at 4:17am return $scheduler ->daily() ->hours(4) ->minutes(17); } }
README Contents
- Features
- Tutorial
- Installation
- For Laravel 4 (see 1.4 branch)
- For Laravel 5 - discontinued, see Laravel 5's scheduler
- Upgrading from 1.4 to 2.0
- Usage
- Drivers
- Custom Drivers
- FAQ
By Ben Kuhl at the Laravel Louisville meetup (@lurvul): Video - Slides
By Jefferey Way at Laracasts: Recurring Tasks the Laravel Way
## InstallationNOTICE: Laravel 5 now includes scheduling out of the box. This package will no longer be maintained for Laravel 5 and above
Requirements | 1.4.* | 2.* |
---|---|---|
Laravel | 4.1/4.2 | 5.x |
PHP | 5.3+ | 5.4+ |
HHVM | 3.3+ | 3.3+ |
Install with Composer... | ~1.4 | ~2.0@dev |
If you're using Laravel 4 view the readme in the 1.4 branch
Add this line to the providers array in your config/app.php
file :
'Indatus\Dispatcher\ServiceProvider',
Add the following to your root Crontab (via sudo crontab -e
):
* * * * * php /path/to/artisan scheduled:run 1>> /dev/null 2>&1
If you are adding this to /etc/cron.d
you'll need to specify a user immediately after * * * * *
.
### Upgrading from 1.4 to 2.0You may add this to any user's Crontab, but only the root crontab can run commands as other users.
- Replace
use Indatus\Dispatcher\Drivers\Cron\Scheduler
withuse Indatus\Dispatcher\Drivers\DateTime\Scheduler
- Replaced uses of
Scheduler::[DAY_OF_WEEK]
withDay::[DAY_OF_WEEK]
andScheduler::[MONTH_OF_YEAR]
withMonth::[MONTH_OF_YEAR]
executable
config option has been removed. Dispatcher now inherits the path to the binary that was initially used to runscheduled:run
scheduled
scheduled:make Create a new scheduled artisan command
scheduled:run Run scheduled commands
scheduled:summary View a summary of all scheduled artisan commands
If commands are not visible via php artisan
then they cannot be scheduled.
Use php artisan scheduled:make
to generate a new scheduled command, the same way you would use artisan's command:make
. Then register your command with Laravel.
Scheduler
class.
use Indatus\Dispatcher\Scheduling\ScheduledCommand; use Indatus\Dispatcher\Scheduling\Schedulable; use Indatus\Dispatcher\Drivers\DateTime\Scheduler;Extend
\Indatus\Dispatcher\Scheduling\ScheduledCommand
Implement schedule():
/** * When a command should run * * @param Scheduler $scheduler * * @return Scheduler */ public function schedule(Schedulable $scheduler) { return $scheduler; }
For details and examples on how to schedule, see the DateTime Driver.
### Running Commands As Userspublic function user() { return 'backup'; }
### Environment-Specific CommandsThis feature may not be supported by all drivers.
public function environment() { return ['development','staging']; }### Maintenance Mode
public function runInMaintenanceMode() { return true; }### Advanced scheduling
public function schedule(Schedulable $scheduler) { return [ // 5am Mon-Fri $scheduler->everyWeekday()->hours(5), // 2am every Saturday App::make(get_class($scheduler)) ->daysOfTheWeek(Scheduler::SATURDAY) ->hours(2) ]; }
You may also schedule a command to run with arguments and options.
public function schedule(Schedulable $scheduler) { return [ // equivalent to: php /path/to/artisan command:name /path/to/file $scheduler->args(['/path/to/file']) ->everyWeekday() ->hours(5), // equivalent to: php /path/to/artisan command:name /path/to/file --force --toDelete="expired" --exclude="admins" --exclude="developers" $scheduler->args(['/path/to/file']) ->opts([ 'force', 'toDelete' => 'expired', 'exclude' => [ 'admins', 'developers' ] ]) ->daysOfTheMonth([1, 15]) ->hours(2) ]; }
## Drivers
Drivers provide the ability to add additional context to your scheduling. Building custom drivers is a great way to customize this context to your application's needs.
### DateTime (Default)public function schedule(Schedulable $scheduler) { //every day at 4:17am return $scheduler->daily()->hours(4)->minutes(17); }
public function schedule(Schedulable $scheduler) { //every Tuesday/Thursday at 5:03am return $scheduler->daysOfTheWeek([ Scheduler::TUESDAY, Scheduler::THURSDAY ])->hours(5)->minutes(3); }
public function schedule(Schedulable $scheduler) { //the second and third Tuesday of every month at 12am return $scheduler->monthly()->week([2, 3])->daysOfTheWeek(Day::TUESDAY); }## Custom Drivers
Create a packagepath such as \MyApp\ScheduleDriver\
and create two classes:
Scheduler
that implements Indatus\Dispatcher\Scheduling\Schedulable
. This class should provide a useful interface for programmers to schedule their commands.
ScheduleService
that extends \Indatus\Dispatcher\Services\ScheduleService
. This class contains logic on how to determine if a command is due to run.
'driver' => '\MyApp\ScheduleDriver'
## FAQ
Schedule scheduled:run
to run every minute with rcron:
* * * * * /usr/bin/rcron php /path/to/artisan scheduled:run 1>> /dev/null 2>&1
Why are my commands not running when I've scheduled them correctly? I'm also not seeing any error output
-
Verify that mcrypt is installed and working correctly via the command
php -i | mcrypt
. -
Utilizing
php artisan scheduled:run --debug
will tell you why they're not running. If you do not see your command listed here then it is not set up correctly.
Example:
$ php artisan scheduled:run --debug
Running commands...
backup:avatars: No schedules were due
command:name: No schedules were due
myTestCommand:name: No schedules were due
cache:clean: /usr/bin/env php /Users/myUser/myApp/artisan cache:clean > /dev/null &
mail:subscribers: /usr/bin/env php /Users/myUser/myApp/artisan mail:subscribers > /dev/null &
I have commands that extend ScheduledCommand
but why don't they appear in when I run scheduled:summary
?
Commands that are disabled will not appear here. Check and be sure isEnabled()
returns true on those commands.