Yii Queue, how can I avoid overlaping

I would like two jobs not to overlap, i.e. only one job can be executed at a time. Is there anything to do this?

If not, as far as I can see, I think to establish some kind of “flag” set to 1 when Queue::EVENT_BEFORE_EXEC and to 0 when Queue::EVENT_AFTER_EXEC.
Also, how can I set a real listener? a cron just works once each minute, but my jobs can be handled in seconds.

You can use mutex for the purpose: https://github.com/yiisoft/yii2/blob/master/framework/mutex/Mutex.php


Thank you @samdark … do you have any example using Yii2-Queue + Mutex? I can’t see the “merge” clearly of this two features.

Mutex allows to get an idea if something is “locked”. This something could be a job of a certain type. How to handle it depends on your case. You can mark the job as done and put it into queue again or make the worker to actually wait till the lock is released.

1 Like

If you are executing jobs using queue/run from a command line (cron for instance), you can use:
/usr/bin/flock -n /tmp/anyfile.lockfile php yii your-queue/run

flock will not let to start a new run if a another is being executed.

1 Like

Seems pretty easy :-D. thank you !