When does a PHP <5.3.0 daemon script receive signals?

Posted by MidnightLightning on Stack Overflow See other posts from Stack Overflow or by MidnightLightning
Published on 2010-04-29T18:42:10Z Indexed on 2010/04/29 19:17 UTC
Read the original article Hit count: 562

Filed under:
|
|

I've got a PHP script in the works that is a job worker; its main task is to check a database table for new jobs, and if there are any, to act on them. But jobs will be coming in in bursts, with long gaps in between, so I devised a sleep cycle like:

while(true) {
  if ($jobs = get_new_jobs()) {
    // Act upon the jobs
  } else {
    // No new jobs now
    sleep(30);
  }
}

Good, but in some cases that means there might be a 30 second lag before a new job is acted upon. Since this is a daemon script, I figured I'd try the pcntl_signal hook to catch a SIGUSR1 signal to nudge the script to wake up, like:

$_isAwake = true;
function user_sig($signo) {
  global $_isAwake;
  daemon_log("Caught SIGUSR1");
  $_isAwake = true;
}
pcntl_signal(SIGUSR1, 'user_sig');

while(true) {
  if ($jobs = get_new_jobs()) {
    // Act upon the jobs
  } else {
    // No new jobs now
    daemon_log("No new jobs, sleeping...");
    $_isAwake = false;
    $ts = time();
    while(time() < $ts+30) {
      sleep(1);
      if ($_isAwake) break; // Did a signal happen while we were sleeping? If so, stop sleeping
    }
    $_isAwake = true;
  }
}

I broke the sleep(30) up into smaller sleep bits, in case a signal doesn't interrupt a sleep() command, thinking that this would cause at most a one-second delay, but in the log file, I'm seeing that the SIGUSR1 isn't being caught until after the full 30 seconds has passed (and maybe the outer while loop resets).

I found the pcntl_signal_dispatch command, but that's only for PHP 5.3 and higher. If I were using that version, I could stick a call to that command before the if ($_isAwake) call, but as it currently stands I'm on 5.2.13.

On what sort of situations is the signals queue interpreted in PHP versions without the means to explicitly call the queue parsing? Could I put in some other useless command in that sleep loop that would trigger a signal queue parse within there?

© Stack Overflow or respective owner

Related posts about php

Related posts about php5