如果作业已在 laravel 中的队列中,请阻止类似的队列作业



我正在使用带有laravel的Beanstalkd来处理队列作业。如果作业已在队列中,如何防止添加相同的作业。我正在使用 laravel 5.3 和 Beanstalkd 3.1

没有阻止作业成为消息队列一部分的概念。只是你不能这样做。

确保代码的编写方式不会处理重复项。如果你仍然需要一些东西来解决,也许可以查看Redis的SortedSet,并用它来永久存储你的工作。

此解决方法将有所帮助:

class ClientSendInvoice implements ShouldQueue {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $uuid, $cacheKey;
/**
 * Create a new job instance.
 *
 * @return void
 * @throws InvalidArgumentException
*/
public function __construct()
{
   $this->delay = 5;
   $this->uuid = Str::uuid();
   $this->cacheKey = "ClientSendInvoice_{$this->invoice->id}";
   cache()->put($this->cacheKey, $this->uuid, $this->delay);
}
/**
 * Execute the job.
 *
 * @return void
 */
public function handle(Invoice $invoice)
{
    if (cache()->get($this->cacheKey, $this->uuid) === $this->uuid) {
        Mail::to($invoice->client)
            ->send(new ClientInvoice(['invoice' => $invoice]));
    }
}
有一个

解决方法,您可以在调度队列之前尝试添加以下代码

$queue = DB::table(config('queue.connections.database.table'))->first();
    if($queue){
        $payload = json_decode($queue->payload,true);
        if($payload['displayName'] == 'AppJobsProcessReport'){
            flash('The report in process','info');
            return back()->withInput();
        }
    }
//dispatch the queue
ProcessReport::dispatch();

最新更新