views:

746

answers:

2
+2  Q: 

PHP + MySQL Queue

I need a simple table that acts as a Queue. My MySQL server restriction is I can't use InnoDB tables, only MyISAM.

Clients/workers will work at the same time and they will need to receive differents jobs each time.

My idea is to do the following (pseudo-code):

$job <- SELECT * FROM queue ORDER BY last_pop ASC LIMIT 1;
UPDATE queue SET last_pop WHERE id = $job->id
return $job

I had tried table lock and "GET_LOCK" but nothing happends, workers sometimes receives same jobs.

+7  A: 

You need to turn your ordering around so there is no timing window.

Consumer POP

Update queue 
set last_pop = '$consumer_id' 
where taken last_pop is null 
order by id limit 1;

$job = 
  Select * from queue 
  where last_pop = '$consumer_id' 
  order by id desc 
  limit 1;

Supplier PUSH

insert into queue 
  (id, last_pop, ...) 
values 
  (NUL, NULL, ...);
Don
+1 - I was going with a status field, but your idea was along the same of mine.
James Black
A: 

Just for information, there's another option that is using Gearman instead of a table to make the queue: Rasmus Lerdorf wrote a very nice article about it.

Felipe Ribeiro