Laravel 計劃任務(任務調度)的使用

啓動調度器

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

定義調度

<?php

namespace App\Console;

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

class Kernel extends ConsoleKernel
{
    /**
     * 應用中自定義的 Artisan 命令
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * 定義應用中的命令調度
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            DB::table('recent_users')->delete();
        })->daily();
    }
}
// 閉包執行調度
$schedule->call(function () {})->daily();

// Artisan 命令調度
$schedule->command('emails:send Taylor --force')->daily();

// Artisan 命令調度,另外一種寫法
$schedule->command(EmailsCommand::class, ['Taylor', '--force'])->daily();

// Shell 調度命令
$schedule->exec('node /home/forge/script.js')->daily();

// 隊列任務調度
$schedule->job(new Heartbeat)->everyFiveMinutes();

// 分發任務到「heartbeats」隊列
$schedule->job(new Heartbeat, 'heartbeats')->everyFiveMinutes();

調度頻率

調度計劃:php

->cron('* * * * *');        自定義 Cron 計劃執行任務
->everyMinute();            每分鐘執行一次任務
->everyTwoMinutes();        每兩分鐘執行一次任務
->everyFiveMinutes();       每五分鐘執行一次任務
->everyTenMinutes();        每十分鐘執行一次任務
->everyThirtyMinutes();     每三十分鐘執行一次任務
->hourly();                 每小時執行一次任務
->hourlyAt(17);             每小時第 17 分鐘執行一次任務
->everyTwoHours();          每兩小時執行一次任務
->daily();                  天天 0 點執行一次任務
->dailyAt('13:00');         天天 13:00 執行一次任務
->twiceDaily(1, 13);        天天 01:00 和 13:00 各執行一次任務
->weekly();                 每週日 00:00 執行一次任務
->weeklyOn(1, '8:00');      每週一的 08:00 執行一次任務
->monthly();                每個月第一天 00:00 執行一次任務
->monthlyOn(4, '15:00');    每個月 4 號的 15:00 執行一次任務
->lastDayOfMonth('15:00');  每個月最後一天 15:00 執行一次任務
->quarterly();              每季度第一天 00:00 執行一次任務
->yearly();                 每一年第一天 00:00 執行一次任務

其餘約束條件:node

->weekdays();                   限制任務在工做日執行
->weekends();                   限制任務在週末執行
->sundays();                    限制任務在週日執行
->mondays();                    限制任務在週一執行
->tuesdays();                   限制任務在週二執行
->wednesdays();                 限制任務在週三執行
->thursdays();                  限制任務在週四執行
->fridays();                    限制任務在週五執行
->saturdays();                  限制任務在週六執行
->days(array|mixed);            限制任務在每週的指定日期執行
->between($start, $end);        限制任務在 $start 和 $end 區間執行
->unlessBetween($start, $end);  限制任務不在 $start 和 $end 區間執行
->when(Closure);                限制任務在閉包返回爲真時執行
->skip(Closure);                限制任務在閉包返回爲真時不執行
->environments(array|mixed);    限制任務在特定環境中執行

避免任務重複執行

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

withoutOverlapping 會對任務進行加鎖,默認24小時過時,可經過參數傳入過時時間。redis

限制單個服務器執行任務

應用默認緩存驅動必須是 databasememcachedredis 才能使用這個特性。而且全部服務器必須使用同一臺中央緩存服務器。
$schedule->command('report:generate')
    ->fridays()
    ->at('17:00')
    ->onOneServer();

後臺執行任務

默認狀況下,任務是順序串行執行的。使用 runInBackground 方法能夠讓命令在後臺同時運行。緩存

$schedule->command('analytics:report')
    ->daily()
    ->runInBackground();
該方法只支持經過 commandexec 方法執行的任務。

任務輸出

$schedule->command('emails:send')
    ->daily()
    ->appendOutputTo($filePath)
    ->emailOutputTo('foo@example.com');
$schedule->command('foo')
    ->daily()
    ->emailOutputOnFailure('foo@example.com');
任務輸出只支持 commandexec 方法執行的任務。

任務鉤子

$schedule->command('emails:send')
    ->daily()
    ->before(function () {
        // 任務即將開始...
    })
    ->after(function () {
        // 任務結束...
    });
$schedule->command('emails:send')
    ->daily()
    ->onSuccess(function () {
        // 任務成功...
    })
    ->onFailure(function () {
        // 任務失敗...
    });
相關文章
相關標籤/搜索