Is there a problem to use queue:work on a cron job? [closed]

核能气质少年 提交于 2020-05-16 04:35:41

问题


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?


回答1:


You should look into implementing supervisor to monitor your queue workers for you.

https://laravel.com/docs/5.8/queues#supervisor-configuration

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.

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

Daemon queue workers do not "reboot" the framework before processing each job. Therefore, you should free any heavy resources after each job completes. For example, if you are doing image manipulation with the GD library, you should free the memory with imagedestroy when you are done.

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);
    }
}


来源:https://stackoverflow.com/questions/57471917/is-there-a-problem-to-use-queuework-on-a-cron-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!