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.
<?php
use Indatus\Dispatcher\ScheduledCommand;
class MyCommand extends ScheduledCommand {
//your command name, description etc.
public function schedule(Schedulable $scheduler)
{
//every day at 4:17am
return $scheduler
->daily()
->hours(4)
->minutes(17);
}
}- Schedule artisan commands to run automatically
- Scheduling is maintained within your version control system
- Run commands as other users
- Run commands in certain environments
You can install the library via Composer by adding the following line to the require block of your composer.json file:
"indatus/dispatcher": "dev-master"
Next run composer update.
Add this line to the providers array in your app/config/app.php file :
'Indatus\Dispatcher\ServiceProvider',Add the following cron. If you'd like for scheduled commands to be able to run as different users, be sure to add this to the root crontab. Otherwise all commands run as the user whose crontab you've added this to.
* * * * * php /path/to/artisan scheduled:run 1>> /dev/null 2>&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.
Simply extend \Indatus\Dispatcher\ScheduledCommand and implement the schedule() method within your command. This method is injected with a Schedulable which provides a simple interface for scheduling your commands.
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);
}You may override user() to run a given artisan command as a specific user. Ensure your scheduled:run artisan command is running as root.
public function user()
{
return 'backup';
}You may override environment() to ensure your command is only scheduled in specific environments. It should provide a single environment or an array of environments.
public function environment()
{
return ['development','staging'];
}You can build your own drivers or extend a driver that's included. Create a packagepath such as \MyApp\ScheduleDriver\ and create two classes:
Schedulerthatimplements Indatus\Dispatcher\SchedulableScheduleServicethatextends \Indatus\Dispatcher\Services\ScheduleService
Then update your driver configuration to reference the package in which these 2 classes are included (do not include a trailing slash):
'driver' => '\MyApp\ScheduleDriver'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.
Why do I see a RuntimeExceptionWhen I use php artisan scheduled:run?
When running scheduled commands, exceptions from a command will appear as if they came from scheduled:run. More than likely, it's one of your commands that is throwing the exception.
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.



