Slow work, such as converting an uploaded video or sending a newsletter, shouldn't keep a user waiting. Put it in a queue: the request finishes at once, and a background worker does the job.
Workers run after a request's output is sent, so queues work without cron. Admins see the queues on the Dashboard.
jrCore_queue_create('xxFoo', 'process_foo', array('foo_id' => $id));The array is passed to the worker. A fourth argument delays the job by that many seconds.
Register it in _init():
jrCore_register_queue_worker('xxFoo', 'process_foo', 'xxFoo_process_foo_worker', 0, 1);0: how many jobs one worker does before it stops. 0 is no limit; the default is 1.1: how many workers may run this queue at once.The worker gets the job's data, and returns what happened:
function xxFoo_process_foo_worker($_queue)
{
$_it = jrCore_db_get_item('xxFoo', $_queue['foo_id']);
if (!$_it) {
return true; // nothing to do - delete the job
}
// ... do the work ...
return true; // done - delete the job
}| Return | Meaning |
|---|---|
true |
Done: the job is deleted |
false |
Something went wrong: the job goes back in the queue |
| a number | Try again in that many seconds |
'EXIT' |
Put the job back and stop this worker |
If a queue stops moving: pause the queues on the Dashboard, run an Integrity Check with Repair Modules checked, start the queues again, and reset the stuck queue.