Laravel計画タスク(タスクスケジュール)の使用


スケジューラの起動
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

スケジュールの定義
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();

スヶジューリングしゅうはすう
スケジュール:
->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       

その他の制約:
->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時間が期限切れになり、パラメータを使用して期限切れの時間を入力できます.
単一サーバのタスクの実行を制限
デフォルトキャッシュドライバを適用するにはdatabasememcachedまたはredisはこの特性を使用することができます.すべてのサーバが同じ中央キャッシュサーバを使用する必要があります.
$schedule->command('report:generate')
    ->fridays()
    ->at('17:00')
    ->onOneServer();

バックグラウンドでタスクを実行
デフォルトでは、タスクは順次シリアルで実行されます.runInBackgroundメソッドを使用すると、コマンドをバックグラウンドで同時に実行できます.
$schedule->command('analytics:report')
    ->daily()
    ->runInBackground();

このメソッドは、commandおよびexecメソッドによって実行されるタスク.
タスク出力
$schedule->command('emails:send')
    ->daily()
    ->appendOutputTo($filePath)
    ->emailOutputTo('[email protected]');
$schedule->command('foo')
    ->daily()
    ->emailOutputOnFailure('[email protected]');

タスク出力のみサポートcommandおよびexecメソッドによって実行されるタスク.
タスクフック
$schedule->command('emails:send')
    ->daily()
    ->before(function () {
        //       ...
    })
    ->after(function () {
        //     ...
    });
$schedule->command('emails:send')
    ->daily()
    ->onSuccess(function () {
        //     ...
    })
    ->onFailure(function () {
        //     ...
    });