Laravel Redis 队列和主管工作人员 - 作业被多次执行

时间:2021-02-19 09:26:30

标签: laravel redis queue supervisord

我使用 Redis 作为队列驱动程序并分派向用户发送通知的作业。问题是用户多次收到相同的通知。

Laravel 8.0,标准 Redis 配置。

主管配置:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/vhosts/website.com/httpdocs/artisan queue:work --sleep=0 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
numprocs=10
redirect_stderr=true
stdout_logfile=/var/www/vhosts/website.com/httpdocs/storage/logs/worker.log
stopwaitsecs=3600

工作:

class NotifySubscribersOfReply implements ShouldQueue, ShouldBeUnique
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $thread;
    public $reply

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($thread, $reply)
    {
        $this->thread = $thread;
        $this->reply = $reply;
    }

    /**
     * The unique ID of the job.
     *
     * @return string
     */
    public function uniqueId()
    {
        return "reply-" . $this->reply->id . "-notification";
    }

    /**
     * The number of seconds after which the job's unique lock will be released.
     *
     * @var int
     */
    public $uniqueFor = 3600;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // send notification
    }
}

队列配置:

'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => env('REDIS_QUEUE', 'default'),
            'retry_after' => 1800,
            'block_for' => null,
        ],

如果有任何提示,我会很高兴,谢谢!

0 个答案:

没有答案