I need to build a queue system and came across this topic How to Build a PHP Queue System.
When the queue listener is running, this works like a charm. But once in a while, the queue listener might crash or stopped and I need to restart it. Not an issue.
The issue that exists is that in the meantime, jobs are added to the queue. But those jobs are not executed as soon when the listener is started again. The queue is empty. How can I take care that if the queue listener is not running, jobs keep being queue and processed when the queue listener is started?
Queue listener:
<?php
//queue-listener.php
function process($job) {
sleep(1); //. make it look like we did work.
return;
}
$queue = array();
//////// setup our named pipe ////////
$pipefile = '/storage/queueserver';
umask(0);
if(!file_exists($pipefile)){
if(!posix_mkfifo($pipefile,0666)) {
die('unable to create named pipe');
}
}
$pipe = fopen($pipefile,'r+');
if(!$pipe) die('unable to open the named pipe');
stream_set_blocking($pipe,false);
//////// process the queue ////////
while(1) {
while($input = trim(fgets($pipe))) {
stream_set_blocking($pipe,false);
$queue[] = $input;
}
$job = current($queue);
$jobkey = key($queue);
if($job) {
echo 'processing job ', $job, PHP_EOL;
process($job);
next($queue);
unset($job,$queue[$jobkey]);
} else {
echo 'no jobs to do - waiting...', PHP_EOL;
stream_set_blocking($pipe,true);
}
}
Code to add something to the queue
$pipefile = '/storage/queueserver';
$fhp = fopen($pipefile, 'r+') or die ("can't open file $pipefile");
fwrite($fhp, "GenerateLabel|". date('H:i:s')."\n");