1

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");
Timo002
  • 3,138
  • 4
  • 40
  • 65

1 Answers1

0

I assume now that each time your script is launched, the file is created again with the posix_mkfifo function.

Can you confirm that by adding to your script :

umask(0);
if(!file_exists($pipefile)){
    if(!posix_mkfifo($pipefile,0666)) {
        die('unable to create named pipe');
    } else {
        echo "Pipe created";
    }
}

Then launch it a first time. It will echo. Then exit it, and relaunch. If it still echoes, that means that the problem come from the fact that PHP doesn't see the file already exists (maybe from the permission of the file ?)

Thibault Henry
  • 816
  • 6
  • 16
  • File is not created on starting the queue listener because it already exists. In the example I link to they did that, but I already fixed it. – Timo002 Dec 13 '19 at 11:44