tags:

views:

22

answers:

1

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?

A: 

Fixed my own problem: The answer is the "ticks" declaration. I had, as part of the Daemon process startup done the declare(ticks=1); action, but it wasn't seeming to carry over to the main script (since that was inside a function, in an include file?. Adding a declare(ticks=1) line before the while(true) loop causes signals to come through immediately (i.e. the sleep(1) command causes a tick, so after waking up from sleep, signals are processed).

MidnightLightning