Laravel Queue with Supervisor无法与多个网站一起使用

时间:2019-03-29 15:28:15

标签: laravel supervisor

将单个运行Laravel队列的网站添加到Supervisor时,将按应有的方式处理延迟的作业。

当我将另一个网站添加到Supervisor时,任何网站/队列的延迟作业都不会得到处理。

我尝试重新读取,重新加载和重新启动服务。 administratord.log显示所有正在运行的进程,但作业没有被触发。

如果我停止除1个工人之外的所有工人,则该队列有效。

Laravel 5.7

Supervisord 3.3.1

redis-cli 3.2.6

Debian / Nginx

sudo nano /etc/supervisor/conf.d/website-a-worker.conf
[program:website-a-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/website.a/artisan queue:work redis --sleep=3 --tries=3
autostart=true
autorestart=true
#user=laravel
numprocs=8
autostart=true
autorestart=true
stdout_logfile=/var/www/website.a/storage/logs/worker.log
stderr_logfile=/var/www/website.a/storage/logs/worker.err.log
sudo nano /etc/supervisor/conf.d/website-b-worker.conf
[program:website-b-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/website.b/artisan queue:work redis --sleep=3 --tries=3
autostart=true
autorestart=true
#user=laravel
numprocs=8
autostart=true
autorestart=true
stdout_logfile=/var/www/website.b/storage/logs/worker.log
stderr_logfile=/var/www/website.b/storage/logs/worker.err.log
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start website-a-worker:*
sudo supervisorctl start website-b-worker:*

// config/queue.php
...
'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => 'default',
            'retry_after' => 600,
            'block_for' => null,
        ],
...
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

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

    public function handle()
    {
       \Log::info('This only gets triggered if 1 supervisor worker is running.');
    }
}
> sudo service supervisor restart
> cd /var/www/website.a
> php artisan tinker

>>> App\Jobs\SomeJob::dispatch()->delay(1);
# Job does not get triggered
>>> exit

工作永远不会触发。 但是,如果我停止了website.b的主管,则website.a将处理作业。

> sudo supervisorctl stop website-b-worker:*
website-b-worker:website-b-worker_02: stopped
website-b-worker:website-b-worker_03: stopped
website-b-worker:website-b-worker_00: stopped
website-b-worker:website-b-worker_01: stopped
website-b-worker:website-b-worker_06: stopped
website-b-worker:website-b-worker_07: stopped
website-b-worker:website-b-worker_04: stopped
website-b-worker:website-b-worker_05: stopped

> php artisan tinker

>>> App\Jobs\SomeJob::dispatch()->delay(1);
# Job gets triggered!

如何运行多个工作人员(每个网站至少要有1个工作人员)?

1 个答案:

答案 0 :(得分:0)

我曾经将Supervisor与Laravel和Redis一起使用,但一次仅用于一个网站。但是,我进行了一些研究,发现可以在Laravel https://laravel.com/docs/5.8/queues#connections-vs-queues中指定Queue进程的名称。

Laravel Job具有一个setter方法onQueue(),用于为队列指定名称。 laravel文档示例:

Job::dispatch()->onQueue('emails');

但是在您的代码SomeJob中,您可以直接使用$this->onQueue('queue_name')来调用它;您的代码可以像:

<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

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

    public function handle()
    {
        // on website a
        $this->onQueue('websiteAQueue');
        \Log::info('This only gets triggered if 1 supervisor worker is running.');
    }
}

然后在网站A的Supervisor流程中,将队列参数指定为websiteAQueue

[program:website-a-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/website.a/artisan queue:work redis --queue=WebsiteAQueue --sleep=3 --tries=3
autostart=true
autorestart=true
#user=laravel
numprocs=8
autostart=true
autorestart=true
stdout_logfile=/var/www/website.a/storage/logs/worker.log
stderr_logfile=/var/www/website.a/storage/logs/worker.err.log

这样,当Laravel发送作业时,它将发送到指定的队列,而不是找到的默认队列。

编辑------------------------

或者更好的是,您可以为每个网站配置使用不同的队列名称:

// Website A : config/queue.php
...
'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => 'websiteAQueue',
            'retry_after' => 600,
            'block_for' => null,
        ],
...

这样,所有队列都会自动发送到websiteAQueue,因此您不必在每个作业上使用$this->onQueue()