Laravel Queue与Supervisor不使用多个网站

问题描述 投票:1回答:1

将运行Laravel队列的单个网站添加到Supervisor时,会按原样处理延迟的作业。

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

我尝试重新读取,重新加载和重新启动服务。 supervisord.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个)?

laravel supervisor
1个回答
0
投票

我在Laravel和Redis上使用过Supervisor,但一次只用于1个网站。但是我做了一些研究,发现你可以在Laravel https://laravel.com/docs/5.8/queues#connections-vs-queues中指定队列进程的名称。

Laravel qazxsw poi有一个setter方法qazxsw poi来指定队列的名称。 laravel docs示例:

Job

但是在你的代码onQueue()中你可以用Job::dispatch()->onQueue('emails'); 直接调用它;你的代码可以像:

SomeJob

在网站A的Supervisor流程中,将队列参数指定为$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.');
    }
}

这样,当Laravel发送Job时,它会将它发送到指定的Queue而不是它找到的默认Queue。

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

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

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 ,因此您不必在每个作业上使用// Website A : config/queue.php ... 'redis' => [ 'driver' => 'redis', 'connection' => 'default', 'queue' => 'websiteAQueue', 'retry_after' => 600, 'block_for' => null, ], ...

© www.soinside.com 2019 - 2024. All rights reserved.