【问题标题】:Laravel Scheduled queue:work not working automatically need to manual triggerLaravel 调度队列:工作不自动工作需要手动触发
【发布时间】:2018-11-23 04:08:18
【问题描述】:

我有一个非常大的 cron 作业需要执行,但由于内存泄漏,服务器无法处理它,所以我遵循使用 laravel Queue 的建议,因为我不能总是运行php artisan queue:work,所以我写了一个预定的队列命令,每 5 分钟触发一次,但是除非我在服务器上手动触发,否则队列不起作用。

这是我的队列命令:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class EnsureQueueListenerIsRunning extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'queue:checkup';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Ensure that the queue listener is running.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $this->startQueueListener();
        if ( ! $this->isQueueListenerRunning()) {
            $this->comment('Queue listener is being started.');
            $pid = $this->startQueueListener();
            $this->saveQueueListenerPID($pid);
        }
        $this->comment('Queue listener is running.');
    }

    /**
     * Check if the queue listener is running.
     *
     * @return bool
     */
    private function isQueueListenerRunning()
    {
        if ( ! $pid = $this->getLastQueueListenerPID()) {
            return false;
        }
        $process = exec("ps -p $pid -opid=,cmd=");
        //$processIsQueueListener = str_contains($process, 'queue:listen'); // 5.1
        $processIsQueueListener = ! empty($process); // 5.6 - see comments
        return $processIsQueueListener;
    }
    /**
     * Get any existing queue listener PID.
     *
     * @return bool|string
     */
    private function getLastQueueListenerPID()
    {
        if ( ! file_exists(__DIR__ . '/queue.pid')) {
            return false;
        }
        return file_get_contents(__DIR__ . '/queue.pid');
    }
    /**
     * Save the queue listener PID to a file.
     *
     * @param $pid
     *
     * @return void
     */
    private function saveQueueListenerPID($pid)
    {
        file_put_contents(__DIR__ . '/queue.pid', $pid);
    }
    /**
     * Start the queue listener.
     *
     * @return int
     */
    private function startQueueListener()
    {
        //$command = 'php-cli ' . base_path() . '/artisan queue:listen --timeout=60 --sleep=5 --tries=3 > /dev/null & echo $!'; // 5.1
        $command = 'php artisan queue:work --timeout=60 --sleep=5 --tries=3 > /dev/null & echo $!'; // 5.6 - see comments
        $pid = exec($command);
        return $pid;
    }
}

这是内核:

$schedule->command('queue:checkup')->everyFiveMinutes();

我应该怎么做才能让它工作?

【问题讨论】:

    标签: laravel queue schedule


    【解决方案1】:

    如果您想将 cron 作业设置为自动运行而不使用命令启动。**

    转到您的终端:并运行此命令

    crontab -e
    

    这将打开服务器 crontab 文件,将这段代码粘贴进去,保存并退出

    * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-20
      • 2019-10-09
      • 2022-12-18
      • 2014-11-19
      • 2018-07-30
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      相关资源
      最近更新 更多