Laravel Schedule Tasks Artisan Commands with Dispatcher

Wiki

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.

Cron

The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like connecting to the Internet and downloading email at regular intervals. The name cron comes from the Greek word for time, χρόνος chronos.

In this post, I will show you how to schedule analytics user information artisan commands with Dispatcher in Laravel.

Install Dispatcher

Get more information from GitHub Indatus/dispatcher

Search Indatus/dispatcher in packagist.org.

Add "indatus/dispatcher": "2.0.*@dev" in composer.json, add 'Indatus\Dispatcher\ServiceProvider', line to the providers array in laravel app/config/app.php file, then run php composer update in a terminal.

Add the following to your root Crontab (via sudo crontab -e):

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

If you wanna to run crontab as user:www-data, you can edit the crontab of user www-data with su:

$ sudo su -c "crontab -e" www-data

Validate Installation
Run php artisan command in a terminal, if you can see following info mean's that install successful.

scheduled
    scheduled:make              Create a new scheduled artisan command
    scheduled:run               Run scheduled commands
    scheduled:summary           View a summary of all scheduled artisan commands

Validate Cron Config

$ crontab -l

Generating New Scheduled Commands

Use php artisan scheduled:make to generate a new scheduled command.

$ php artisan scheduled:make analytics

Register your command in app/start/artisan.php using the following method

Artisan::add(new analytics);

After that, will generate analytics.php in app/commands folder. Use your favorite text editor edit this file like this.

<?php

use Indatus\Dispatcher\Scheduling\ScheduledCommand;
use Indatus\Dispatcher\Scheduling\Schedulable;
use Indatus\Dispatcher\Drivers\Cron\Scheduler;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class analytics extends ScheduledCommand {

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'command:analytics';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Automatically analytics.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * When a command should run
     *
     * @param Scheduler $scheduler
     * @return \Indatus\Dispatcher\Scheduling\Schedulable
     */
    public function schedule(Schedulable $scheduler)
    {
        //every day at 4:10am
        return $scheduler->daily()->hours(4)->minutes(10);
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function fire()
    {
        // Analytics user information ...
    }
}

Usage

Run php artisan command:analytics in terminal to run analytics command. Because we installed Dispatcher, so we use php artisan scheduled:run to management commands. We can alse use php artisan scheduled:summary to show scheduled command list.

$ php artisan scheduled:summary
+----------------+-------------------+-----------+--------+------+--------------+-------+-------------+--------+
| Environment(s) | Name              | Args/Opts | Minute | Hour | Day of Month | Month | Day of Week | Run as |
+----------------+-------------------+-----------+--------+------+--------------+-------+-------------+--------+
| *              | command:analytics |           | 10     | 4    | *            | *     | *           |        |
+----------------+-------------------+-----------+--------+------+--------------+-------+-------------+--------+
0.00 avg. rating (0% score) - 0 votes