Devs.tw 是讓工程師寫筆記、網誌的平台。歡迎您隨手紀錄、寫作,方便日後搜尋!
$schedule->command('queue:restart')
->everyFiveMinutes()
->appendOutputTo(storage_path('logs/task.log'));
$schedule->command('queue:work --daemon --timeout=900 --tries=254')
->everyMinute()
->withoutOverlapping()
->appendOutputTo(storage_path('logs/task.log'));
https://divinglaravel.com/preventing-scheduled-jobs-overlapping
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Queue/Console/WorkCommand.php
src/Illuminate/Queue/Console/WorkCommand.php
定義了 queue:work
指令
此指令會執行 runWorker
跑worker. worker 會根據對應的 driver 從 queue 中取出任務. 接著根據參數執行 runNextJob
或是 daemon
return $this->worker->{$this->option('once') ? 'runNextJob' : 'daemon'}(
$connection, $queue, $this->gatherWorkerOptions()
);
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Queue/Worker.php
/src/Illuminate/Queue/Worker.php
首先使用 pcntl_signal
php 函式監聽 Unix Signal 訊號. 此為 php 開發 daemon 程式常見作法.
接著 while (true) {}
跑無限迴圈來達成 daemon 效果. 再從對應的 driver 取出 queue 內任務
$job = $this->getNextJob(
$this->manager->connection($connectionName), $queue
);
然後執行任務
if ($job) {
$this->runJob($job, $connectionName, $options);
} else {
$this->sleep($options->sleep);
}