【发布时间】:2021-05-22 06:51:41
【问题描述】:
我使用 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,
],
如果有任何提示,我会非常高兴,谢谢!
【问题讨论】:
-
您可以在
handle()函数中检查通知是否已经发送。如果它已经发送,不要做任何事情。在这种情况下,您需要将推送通知存储在数据库中。 -
@RifatBinReza 是的,我可以做到。但我想知道这是否有必要?
-
根据我的经验,这是必要的。如果您的工作需要更长的时间才能完成,然后它会再次开始,并且在一段时间后完成前一个工作怎么办。执行作业时始终检查是安全的。
标签: laravel redis queue supervisord