L
L
Leonid Gorshkov2021-08-26 14:56:23
Laravel
Leonid Gorshkov, 2021-08-26 14:56:23

How to set a unique field for a queue in laravel?

Good afternoon. It is necessary to set a unique field in jobs (for example, user_id), so that, if necessary, this particular task can be removed from the queue. (Use intermediate redis, mysql is not an option).
How to achieve this?
And how to add information to this field during dispatch()?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vism, 2021-08-26
@vism

Why delete?
Write a condition in the Job itself, so that first there is a check whether to execute this Job or not

R
RacceGatel, 2021-09-03
@RacceGatel

I also had the same need. However, as I understand it, it is impossible to pull out a specific work from jobs and delete it "in a human way". I solved this problem by adding Locks with a dynamic key, and already in the body of the job, I checked the state of the lock.
In other words, when adding a job to the queue, a lock is added that is tied specifically to this job, if the queue has not reached our job, you can simply remove the lock and then this job will obviously not work.
This arrangement allowed me, roughly speaking, to control the state of the job until it was the turn in the worker.
But apparently this option does not suit you, so I'll wait for someone to offer something better.
Question with a unique field - how to ask

class Job implements ShouldQueue, ShouldBeUnique
{
protected $user_id;

public function __construct($user_id)
    {
        $this->user_id = $user_id;
    }

public function handle()
    {
        //
    }

public function uniqueId()
    {
        return $this->user_id;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question