When does php <5.3.0 script signal appear?
I have a PHP script in the works that works in the workplace; its main task is to check the database table for new jobs, and if any, act on them. But jobs will appear in queues with long gaps in between, so I designed a sleep cycle like:
while(true) { if ($jobs = get_new_jobs()) { // Act upon the jobs } else { // No new jobs now sleep(30); } }
Okay, but in some cases this means that it may take a 30 second lag before a new job is executed. Since this is a script daemon, I figured I would try to pcntl_signal
intercept the SIGUSR1 signal to nudge the script to wake up, like this:
$_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 sleep(30)
into smaller sleep bits, in case the signal does not interrupt the command sleep()
, assuming that this would result in a maximum delay of one second, but in the log file that SIGUSR1 does not hit until the full 30 seconds have passed (and, perhaps the outer loop is while
reset).
I found a command pcntl_signal_dispatch
, but this is only for PHP 5.3 and up. If I were using this version, I could invoke this command before invoking if ($_isAwake)
, but since it currently stands, I'm in 5.2.13.
In what situations is a signal queue interpreted in PHP versions without means to explicitly call the queue parsing? Can I add some other useless command to this sleep loop that will run the signal queue syntax internally?
source share
Fixed my issue: The answer is ticks . "I had an action being executed while starting the Daemon process declare(ticks=1);
, but it didn't seem to be carried over to the main script (since it was inside a function in an included file?) Adding declare(ticks=1)
before the loop while(true)
calls immediate signals (i.e., the command sleep(1)
triggers a check mark, so after waking up from sleep, the signals are processed).
source share