I am using Beanstalkd with laravel for handling queue job. How can i prevent from adding same job if job is already in queue. I am using laravel 5.3 with Beanstalkd 3.1
Asked
Active
Viewed 1,569 times
5
-
You have a low rate. Important on SO, you have to mark accepted answers by using the tick on the left of the posted answer, below the voting. This will increase your rate. See how this works by visinting this link: http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work#5235 – Pentium10 Dec 18 '17 at 12:20
3 Answers
0
There is no such concept of preventing a job to be part of a message queue. Simply you cannot do this.
Make sure that your code is written in a manner than it will handle duplicates. If you still need something to work out, maybe check Redis's SortedSet, and use that to store permanently your job.

Pentium10
- 204,586
- 122
- 423
- 502
0
This workaround will help:
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]));
}
}

nolan23
- 1
- 1
-1
There is a workaround solution you can try to add below code before dispatch the queue
$queue = \DB::table(config('queue.connections.database.table'))->first();
if($queue){
$payload = json_decode($queue->payload,true);
if($payload['displayName'] == 'App\Jobs\ProcessReport'){
\flash('The report in process','info');
return back()->withInput();
}
}
//dispatch the queue
ProcessReport::dispatch();

vipmaa
- 1,022
- 16
- 25
-
OP specifically mentioned using beanstalkd, not a database, as their queue driver. Databases should not be used for queues in production. – hackel May 06 '20 at 15:00