Having an issue where one of my team members is making a queuing process in PHP. The PHP script runs on the command line, and recursively calls itself after each cycle to check if there's any items in our DB waiting to be processed. If there are, it forks itself, processes the item in the queue, and repeats. If there's nothing, it dies, and a cron job restarts the queue every 5 minutes.
Occasionally two processes run at the same time, grabbing the same queue, and get in each others way. I was thinking of introducing a jitter to the wake-up process so that it's a random amount of time between the process starting.
Is there a better way?
Jitter just makes the collisions less predictable.
Put a mutex on disk. Check if it's older than two cycles ago. If it is, ignore and delete it; it got left behind by something that crashed. Otherwise, there's one already running, so bail.
If the items in your queue do not have to be performed in order you could do something like this and just leave both processes to run in parallel.
Select next job from queue (obviously the syntax will be database dependent)
LOCK TABLE queue;
SELECT * FROM queue WHERE status <> 'INPROGRESS' ORDER BY id LIMIT 1
UPDATE queue SET status = 'INPROGRESS' WHERE id = ?
UNLOCK TABLES;
Do job then mark job as completed or delete from queue
If you want to find jobs stuck in limbo, you could store a job started timestamp and put it back in the queue if it is there for more then x mins
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With