SkillAgentSearch skills...

Dispatcher

Dispatcher is a Laravel artisan command scheduling tool used to schedule artisan commands within your project so you don't need to touch your crontab when deploying.

Install / Use

/learn @Indatus/Dispatcher
About this skill

Quality Score

0/100

Category

Operations

Supported Platforms

Universal

README

Dispatcher

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.

<img align="left" height="300" src="https://s3-us-west-2.amazonaws.com/oss-avatars/dispatcher_round_readme.png">
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);
    }
}

Latest Stable Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

README Contents

<a name="features" /> ## Features
  • Schedule artisan commands to run automatically
  • Scheduling is maintained within your version control system
  • Single source of truth for when and where commands run
  • Schedule commands to run with arguments and options
  • Run commands as other users
  • Run commands in certain environments
  • Use custom drivers for custom scheduling contexts
<a name="tutorial" /> ## Tutorial

By Ben Kuhl at the Laravel Louisville meetup (@lurvul): Video - Slides

By Jefferey Way at Laracasts: Recurring Tasks the Laravel Way

<a name="installation" /> ## Installation

NOTICE: 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 * * * * *.

You may add this to any user's Crontab, but only the root crontab can run commands as other users.

<a name="upgrading-1.4-2.0" /> ### Upgrading from 1.4 to 2.0

In all scheduled commands...

  • Replace use Indatus\Dispatcher\Drivers\Cron\Scheduler with use Indatus\Dispatcher\Drivers\DateTime\Scheduler
  • Replaced uses of Scheduler::[DAY_OF_WEEK] with Day::[DAY_OF_WEEK] and Scheduler::[MONTH_OF_YEAR] with Month::[MONTH_OF_YEAR]
  • executable config option has been removed. Dispatcher now inherits the path to the binary that was initially used to run scheduled:run
<a name="usage" /> ## Usage
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.

<a name="new-commands" /> ### Generating New Scheduled Commands

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.

<a name="scheduling-commands" /> ### Scheduling Existing Commands

You may either implement \Indatus\Dispatcher\Scheduling\ScheduledCommandInterface or follow the below steps.

  1. Add use statements to your command. If you're using a custom driver you will use a different Scheduler class.
use Indatus\Dispatcher\Scheduling\ScheduledCommand;
use Indatus\Dispatcher\Scheduling\Schedulable;
use Indatus\Dispatcher\Drivers\DateTime\Scheduler;
  1. Extend \Indatus\Dispatcher\Scheduling\ScheduledCommand
  2. 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.

<a name="commands-as-users" /> ### Running Commands As Users

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';
    }

This feature may not be supported by all drivers.

<a name="environment-commands" /> ### Environment-Specific Commands

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'];
    }
<a name="maintenance-mode" /> ### Maintenance Mode

By default, cron commands will not run when application is in Maintenance Mode. This will prevent all sorts of weird output that might occur if a cron command is run while you are migrating a database or doing a composer update.

You may override runInMaintenanceMode() to force your command to still be run while the application is in maintenance mode.

    public function runInMaintenanceMode()
    {
        return true;
    }
<a name="advanced-scheduling" /> ### Advanced scheduling

You may schedule a given command to to run at multiple times by schedule() returning multiple Schedulable instances.

	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)
        ];
	}

NOTE: Both args() and opts(), whichever is called first, will internally create a new Schedulable instance for you so you don't need to App::make().

<a name="drivers" /> ## 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.

<a name="datetime" /> ### DateTime (Default)

Examples of how to schedule:

	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
            ])
View on GitHub
GitHub Stars1.1k
CategoryOperations
Updated8d ago
Forks70

Languages

PHP

Security Score

95/100

Audited on Mar 23, 2026

No findings