Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Queuing Issue - Redundant processes

Tags:

php

queue

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?

like image 910
user36975 Avatar asked Oct 29 '12 21:10

user36975


2 Answers

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.

like image 179
John Haugeland Avatar answered Oct 16 '22 06:10

John Haugeland


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

like image 2
bumperbox Avatar answered Oct 16 '22 05:10

bumperbox