I am running a webapp (php) that needs to process some “heavy-work” tasks (from 30 sec to 3 min). I know it is not extremely heavy, but I cannot have my user waiting for them, so I’ve set up an internal API like: http://localhost/process-picture/745884/ and I store this operation in a MySQL table.
Now I want to create a “process” that fetches that MySQL table and performs the oldest queued operation, and once it is done, it get the next, and so on.
First of all I thought about making a PHP Script that calls by cURL the system like that:
fetchOperation.php connects to DB and gets the URL to the operation to call it by cURL.
Every operation: performs itself, and after that deletes itself from the queue and calls (cURL) fetchOperation.php again.
I feel that this system is kinda tricky, so I was wondering if there is (and in which language to write it) any way to set up a background process that checks the DB every 15 sec, and does the following:
- Delete all rows marked as
DONE. - Check if there is any row marked as
PROCESSING, if so, just exit and wait for the next 15 sec. - If there’s no
PROCESSINGrow, fire the oldestPENDINGone (FIFO queue).
This way I can manage what is being processed at any time, and even control the server load (for instance, at night allow to have up to three PROCESSING items).
Sorry for such a long explanation, and thanks in advance!
Finally I solved it that way:
1st. Design the operations in a way that they are url callable. For instance:
http://localhost/render-image/145236652nd. Store pending operations in a table like this: operations(opertation-id, operation-url, operation-status).
Once this was ready I designed a short PHP script that does the following:
This auxiliar functions (
launch_operationand theOperationsQueueclass) do as follows (notice it’s not yet implemented):I thought it could be useful to some other people. As you see it chains the operations.