views:

523

answers:

3

I'm working an image processing website, instead of having lengthy jobs hold up the users browser I want all commands to return fast with a job id and have a background task do the actual work. The id could then be used to check for status and results (ie a url of the processed image). I've found a lot of distributed queue managers for ruby, java and python but I don't know nearly enough of any of those languages to be able to use them.

My own tests have been with shared mysql database to queue jobs, lock them to a worker, and mark them as completed (saving the return data in the db). It was just a messy prototype, and the entire time I felt as if I was reinventing the wheel (and not very elegantly). Does something exist in php (or that I can talk to RESTfully?) that I could use?

Reading around a bit more, I've found that what I'm looking for is a queuing system that has a php api, it doesn't have to be written in php. I've only found classes for use with Amazon's SQS, but not only is that not free, it's also quite latent sometimes (over a minute for a message to show up).

+2  A: 

Do you have full control over server?

MySQL queue could be fine in such case. Have a PHP script that is running constantly (in endless while loop), querying the MySQL database for new "tasks" and sleep()ing in between to reduce the load in idle time.

When each task is completed, mark it in the database and move to the next one.

To prevent that whole thing stops if your script crashes/exists (PHP memory overflow, etc.) you can, for example, place it in inittab (if you use Linux as a server) and init will restart it automatically.

Milan Babuškov
I like the inittab approach - php tends to leak memory over time so having it die once a minute might not be a bad idea
wizard
I use a shell script that at the end does an 'exec $0' to re-run itself. The client can exit at will and be restarted.
Alister Bulman
+3  A: 

Have you tried ActiveMQ? It makes mention of supporting PHP via the Stomp protocol. Details are available on the activemq site.

I've gotten a lot of mileage out of the database approach your describing though, so I wouldn't worry too much about it.

matschaffer
+1  A: 

Zend_Framework has a queue class, with a number of implementations of Mysql-backed, SQS and some other back-ends.

Personally, I've had excellent results with BeanstalkD recently, which also has a PHP client. I'm just serialising some data with JSON to throw into it, which gets decoded and run on the worker(s).

Alister Bulman