For example : When I want to update product I create job for that and I put job into queue. Job is waiting there and it is still not processed, In meantime I need to create new job for update the same product but with different data and now I want to remove old update job from queue and push new one to queue, any ideas ?
1 Answer
I had an idea. On every job dispatch you would set a cache key, this uuid would also be set on the job's properties.
$uuid = uuid();
Redis::set("update-product-token-$ID",$uuid);
$data['uuid'] = $uuid;
ProductUpdateJob::dispatch($data);
On the first line of the handle function, you check if the redis token matches the job's token. If they don't the job exits gracefully
$uuid = Redis::get("update-product-token-$ID",NULL);
if($uuid !== NULL && $uuid !== $this->uuid) return;
//proceed with update here
No querying the queue, no guessing, no halting an update mid execution. Elegant stuff. Let me know what you think.
pop(string $queue = null)