使用queue:cron作业有问题吗? [英] Is there a problem to use queue:work on a cron job?
问题描述
我已将laravel应用程序部署在托管程序上,并且它具有一些队列作业(如发送电子邮件),我使用工匠命令 queue:work添加了cron作业,并且每1分钟执行一次。我已经进行了一些研究,并且看到有人说它消耗大量RAM,尤其是因为我的Cron作业设置为每1分钟执行一次工匠命令。
如果不对,该怎么办?
I've deployed my laravel application on hostinger, and it has some queue jobs (as sending e-mails), I added a cron job with the artisan command "queue:work" and it's executing every 1 minute. I've done some research, and I've seen some people saying that it consumes a lot of RAM - especially because my cron job is setted up to execute the artisan command every 1 minute. If it's wrong, what should I do?
推荐答案
您应该考虑实现 supervisor
为您监视队列工作人员。
You should look into implementing supervisor
to monitor your queue workers for you.
https://laravel.com/docs/5.8/queues#supervisor-configuration
要使队列:工作流程在后台永久运行,您应该使用诸如
Supervisor
之类的流程监视器,以确保队列工作器不会停止运行。 / p>
To keep the queue:work process running permanently in the background, you should use a process monitor such as
Supervisor
to ensure that the queue worker does not stop running.
高RAM使用率通常与-daemon
标志相关。但是,可以通过偶尔使用 queue:restart
重新启动队列工作器来解决此问题。但是,如果使用得当,则守护程序工作人员效率最高,因为它们不会为每个新作业重新启动应用程序。
High RAM usage is generally associated with the --daemon
flag. However, this can be addressed by simply restarting the queue workers occasionally with queue:restart
. But when used properly, daemon workers are the most efficient because they do not "reboot" the application for each new job.
https://laravel.com/docs/5.8/queues#running-the-queue-worker
守护进程队列工作者在处理每个作业之前不会重新启动框架。因此,每个作业完成后,您都应释放大量资源。例如,如果要使用GD库进行图像处理,则在完成后应使用
imagedestroy
释放内存。
如果由于共享托管限制而无法实现 supervisor
,则有一些解决方法,例如使用没有OverOverlapping()
选项仅在前一个队列工作器死亡时启动一个新的队列工作器。我在某些无法使用 supervisor
的项目中使用了以下代码,但我从未遇到过任何问题。
If you cannot implement supervisor
due to shared hosting restrictions, there are some workarounds such as using the withoutOverlapping()
option to only start a new queue worker if the previous one has died. I have used the following code in certain projects where supervisor
was unavailable, and I have never had any issues.
class Kernel extends ConsoleKernel
{
// define your queues here in order of priority
protected $queues = [
'notifications',
'default',
];
protected function schedule(Schedule $schedule)
{
// run the queue worker "without overlapping"
// this will only start a new worker if the previous one has died
$schedule->command($this->getQueueCommand())
->everyMinute()
->withoutOverlapping();
// restart the queue worker periodically to prevent memory issues
$schedule->command('queue:restart')
->hourly();
}
protected function getQueueCommand()
{
// build the queue command
$params = implode(' ',[
'--daemon',
'--tries=3',
'--sleep=3',
'--queue='.implode(',',$this->queues),
]);
return sprintf('queue:work %s', $params);
}
}
这篇关于使用queue:cron作业有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!