PHP daemon questions
Posted: Thu Nov 06, 2003 1:48 am
I have this daemon that runs against a MySQL database every minute and does stuff. Some of the things that is does requires it to update a bunch of records in the database. Simple. So I wrote a daemon process so I can run it as a service.
Here is a segment of my code:
Now mainloop() has a while (true) {} section that just runs and runs... In the course of the loop it might get a request to update the database. This process requires it to update some 50 to 100 records (some are dependent upon each other).
We would like to integrate some functionality to reload a configuration file. This simplest way, short of killing the service and restarting would be to use the signals, since that's what they are there for.
So I have been analyzing the code for doing the signals and wanted to get some feedback for implementing this. I think that it is straight forward but what I want to ensure this that if I do a HUP that it at leasts completes the batch of processing that it is working on prior to just dumping out.
So, what's the best approach for this. The reason for wanting to finish the batch is because most of the config changes are database changes (right now anyways) so if we HUP in the middle then it ends up getting the wrong DB information... (that's just one of a few cases).
Code segment from php manual:
Does this make sense or is there is simpler way to do this.
BTW, the daemon is run as "nohup ./mydaemon.php &"
TIA,
Gary
Here is a segment of my code:
Code: Select all
function main(&$properties) {
// <-- switch below for debugging.
//$cpid = 0;
$cpid = pcntl_fork();
// Are we a child precoess?
if ($cpid==0) {
// Yes we are...
$i = 1;
init($properties);
mainloop($properties);
}
else {
//We are not a child process. We are the parent so //get out and die. This allows the service to run properly...
}
}We would like to integrate some functionality to reload a configuration file. This simplest way, short of killing the service and restarting would be to use the signals, since that's what they are there for.
So I have been analyzing the code for doing the signals and wanted to get some feedback for implementing this. I think that it is straight forward but what I want to ensure this that if I do a HUP that it at leasts completes the batch of processing that it is working on prior to just dumping out.
So, what's the best approach for this. The reason for wanting to finish the batch is because most of the config changes are database changes (right now anyways) so if we HUP in the middle then it ends up getting the wrong DB information... (that's just one of a few cases).
Code segment from php manual:
Code: Select all
<?php
// tick use required as of PHP 4.3.0
declare (ticks = 1);
// signal handler function
function sig_handler($signo) {
switch($signo) {
case SIGTERM:
// handle shutdown tasks
exit;
break;
case SIGHUP:
// handle restart tasks
break;
case SIGUSR1:
print "Caught SIGUSR1...\n";
break;
default:
// handle all other signals
}
}
print "Installing signal handler...\n";
// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");
pcntl_signal(SIGUSR1, "sig_handler");
// or use an object, available as of PHP 4.3.0
// pcntl_signal(SIGUSR1, array($obj, "do_something");
print "Generating signal SIGTERM to self...\n";
// send SIGUSR1 to current process id
posix_kill(posix_getpid(), SIGUSR1);
print "Done\n"
?>BTW, the daemon is run as "nohup ./mydaemon.php &"
TIA,
Gary