为什么即使我停止了主管,laravel的工作也没有停止?

时间:2020-01-21 10:17:13

标签: php laravel

我有以下supervisor.conf用于运行作业:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command= php artisan queue:work --sleep=5 --tries=5
autostart=true
autorestart=true
numprocs=4
stopwaitsecs=5
redirect_stderr=true
stdout_logfile=/home/ubuntu/laravel-worker.log

然后我执行以下工作:

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class DelayJobTestJob  implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle(): void
    {

        sleep(rand(5,6));
        Log::channel('jobs')->debug("Job Termination test jobId: ".$this->job->getJobId());
    }

}

然后我通过在修补程序上运行来模拟作业分配:

while(true){ App\Jobs\DelayJobTestJob::dispatch(); }

然后我尝试通过以下方式终止我的工作:

sudo supervisorctl stop laravel-worker:*

但是,即使在管理程序上,作业也已停止(根据日志):

$ tail -f /home/vagrant/laravel-worker.log
[2020-01-21 11:58:44][57] Processed:  App\Jobs\DelayJobTestJob
[2020-01-21 11:58:44][61] Processing: App\Jobs\DelayJobTestJob
[2020-01-21 11:58:44][58] Processed:  App\Jobs\DelayJobTestJob
[2020-01-21 11:58:44][62] Processing: App\Jobs\DelayJobTestJob
[2020-01-21 11:58:46][59] Processed:  App\Jobs\DelayJobTestJob
[2020-01-21 11:58:46][63] Processing: App\Jobs\DelayJobTestJob
[2020-01-21 11:58:49][61] Processed:  App\Jobs\DelayJobTestJob
[2020-01-21 11:58:49][64] Processing: App\Jobs\DelayJobTestJob
[2020-01-21 11:58:49][60] Processed:  App\Jobs\DelayJobTestJob
[2020-01-21 11:58:49][65] Processing: App\Jobs\DelayJobTestJob #Last Log

工作正在继续调度:

[2020-01-21 12:07:28] local.DEBUG: Job Termination test jobId: 438  
[2020-01-21 12:07:29] local.DEBUG: Job Termination test jobId: 439  
[2020-01-21 12:07:31] local.DEBUG: Job Termination test jobId: 440  
[2020-01-21 12:07:33] local.DEBUG: Job Termination test jobId: 441  
[2020-01-21 12:07:33] local.DEBUG: Job Termination test jobId: 442  
[2020-01-21 12:07:35] local.DEBUG: Job Termination test jobId: 443  
[2020-01-21 12:07:36] local.DEBUG: Job Termination test jobId: 444  
[2020-01-21 12:07:38] local.DEBUG: Job Termination test jobId: 446  
[2020-01-21 12:07:39] local.DEBUG: Job Termination test jobId: 445  
[2020-01-21 12:07:41] local.DEBUG: Job Termination test jobId: 447  
[2020-01-21 12:07:42] local.DEBUG: Job Termination test jobId: 448  
[2020-01-21 12:07:43] local.DEBUG: Job Termination test jobId: 449  
[2020-01-21 12:07:44] local.DEBUG: Job Termination test jobId: 450  
[2020-01-21 12:07:46] local.DEBUG: Job Termination test jobId: 451  
[2020-01-21 12:07:48] local.DEBUG: Job Termination test jobId: 452  
[2020-01-21 12:07:48] local.DEBUG: Job Termination test jobId: 453  
[2020-01-21 12:07:50] local.DEBUG: Job Termination test jobId: 454  
[2020-01-21 12:07:51] local.DEBUG: Job Termination test jobId: 455  
[2020-01-21 12:07:53] local.DEBUG: Job Termination test jobId: 456  
[2020-01-21 12:07:54] local.DEBUG: Job Termination test jobId: 457  
[2020-01-21 12:07:56] local.DEBUG: Job Termination test jobId: 458  
[2020-01-21 12:07:56] local.DEBUG: Job Termination test jobId: 459  
[2020-01-21 12:07:59] local.DEBUG: Job Termination test jobId: 460  
[2020-01-21 12:08:00] local.DEBUG: Job Termination test jobId: 461  
[2020-01-21 12:08:01] local.DEBUG: Job Termination test jobId: 462  
[2020-01-21 12:08:02] local.DEBUG: Job Termination test jobId: 463  # Last Log

我发现原因是因为jobs通道上的最后一次登录时间大于主管的登录时间,而12:08:02的时间磨合时间大于{5}的5个时间我期望上一份工作的秒差。

停止分发作业的唯一方法是运行一次:

11:58:49

你知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

while (true) {
    App\Jobs\DelayJobTestJob::dispatch();
}

是一个无限循环,它将作业无休止地推送到队列中。

down命令虽然挂起了调度程序,所以它们停止运行。

https://laravel.com/docs/5.8/scheduling#maintenance-mode