Task Scheduling

Introducción

In the past, developers have generated a Cron entry for each task they need to schedule. Esto es un dolor de cabeza. Your task schedule is no longer in source control, and you must SSH into your server to add the Cron entries. El planificador de comando Laravel permite definir de manera fluida y expresiva la programación de comandos usando para ello al mismo Laravel, y utilizando solamente una entrada de Cron en el servidor.

Your task schedule is defined in the app/Console/Kernel.php file's schedule method. Para ayudarte a comenzar, se incluye un ejemplo sencillo. You are free to add as many scheduled tasks as you wish to the Schedule object.

Starting The Scheduler

Here is the only Cron entry you need to add to your server:

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

This Cron will call the Laravel command scheduler every minute. Then, Laravel evaluates your scheduled tasks and runs the tasks that are due.

Defining Schedules

You may define all of your scheduled tasks in the schedule method of the App\Console\Kernel class. To get started, let's look at an example of scheduling a task. In this example, we will schedule a Closure to be called every day at midnight. Within the Closure we will execute a database query to clear a table:

<?php

namespace App\Console;

use DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        'App\Console\Commands\Inspire',
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            DB::table('recent_users')->delete();
        })->daily();
    }
}

In addition to scheduling Closure calls, you may also schedule Artisan commands and operating system commands. For example, you may use the command method to schedule an Artisan command:

$schedule->command('emails:send --force')->daily();

The exec command may be used to issue a command to the operating system:

$schedule->exec('node /home/forge/script.js')->daily();

Schedule Frequency Options

Of course, there are a variety of schedules you may assign to your task:

Method Descripción
->cron('* * * * *'); Run the task on a custom Cron schedule
->everyMinute(); Run the task every minute
->everyFiveMinutes(); Run the task every five minutes
->everyTenMinutes(); Run the task every ten minutes
->everyThirtyMinutes(); Run the task every thirty minutes
->hourly(); Run the task every hour
->daily(); Run the task every day at midnight
->dailyAt('13:00'); Run the task every day at 13:00
->twiceDaily(); Run the task daily at 1:00 & 13:00
->weekly(); Run the task every week
->monthly(); Run the task every month

These methods may be combined with additional constraints to create even more finely tuned schedules that only run on certain days of the week. For example, to schedule a command to run weekly on Monday:

$schedule->call(function () {
    // Runs once a week on Monday at 13:00...
})->weekly()->mondays()->at('13:00');

Below is a list of the additional schedule constraints:

Method Descripción
->weekdays(); Limit the task to weekdays
->sundays(); Limit the task to Sunday
->mondays(); Limit the task to Monday
->tuesdays(); Limit the task to Tuesday
->wednesdays(); Limit the task to Wednesday
->thursdays(); Limit the task to Thursday
->fridays(); Limit the task to Friday
->saturdays(); Limit the task to Saturday
->when(Closure); Limit the task based on a truth test

Truth Test Constraints

The when method may be used to limit the execution of a task based on the result of a given truth test. In other words, if the given Closure return true, the task will execute as long as no other constraining conditions prevent the task from running:

$schedule->command('emails:send')->daily()->when(function () {
    return true;
});

Preventing Task Overlaps

By default, scheduled tasks will be run even if the previous instance of the task is still running. To prevent this, you may use the withoutOverlapping method:

$schedule->command('emails:send')->withoutOverlapping();

In this example, the emails:send Artisan command will be run every minute if it is not already running. The withoutOverlapping method is especially useful if you have tasks that vary drastically in their execution time, preventing you from predicting exactly how long a given task will take.

Task Output

The Laravel scheduler provides several convenient methods for working with the output generated by scheduled tasks. First, using the sendOutputTo method, you may send the output to a file for later inspection:

$schedule->command('emails:send')
         ->daily()
         ->sendOutputTo($filePath);

Using the emailOutputTo method, you may e-mail the output to an e-mail address of your choice. Note that the output must first be sent to a file using the sendOutputTo method. Also, before e-mailing the output of a task, you should configure Laravel's e-mail services:

$schedule->command('foo')
         ->daily()
         ->sendOutputTo($filePath)
         ->emailOutputTo('foo@example.com');

Note: The emailOutputTo and sendOutputTo methods are exclusive to the command method and are not supported for call.

Post Task Hooks

Using the then method, you may specify code to be executed after the scheduled task is complete:

$schedule->command('emails:send')
         ->daily()
         ->then(function () {
            // Task is complete...
         });

Pinging URLs

Using the thenPing method, the scheduler can automatically ping a given URL when a task is complete. This method is useful for notifying an external service, such as Laravel Envoyer, that your scheduled task is complete:

$schedule->command('emails:send')
         ->daily()
         ->thenPing($url);

Using the thenPing($url) feature requires the Guzzle HTTP library. You can add Guzzle to your project by adding the following line to your composer.json file:

"guzzlehttp/guzzle": "~5.3|~6.0"