The cliDaemon class contains the necessary logic for building a PHP daemon. Process controls are required from cliProcessControls as the posix functions can not be classed and must be called explicitly.
A single method requires implementation: execute() and this takes an instance of the cliRequest (in keeping with the cliApplication interface). An optional method terminate(), can be overloaded to provide additional shutdown instructions or to handle shutdown tasks beyond the defaults. Terminate is called from the destructor() method which is called on daemon kills.
All daemons that inherit this class should NOT replace the constructor, or if this is required, the parent constructor must be called as this installs the process handling features, shutdown function and loads base defaults.
For an example daemon see loggingDaemon located in /daemons.
// very basic example of a daemon
require_once(dirname(dirname(__FILE__)).'/libraries/system.inc');
// PHP <5.3.0 we need to declare ticks to install signal handling
declare(ticks=1);
// setup some logging
systemLog::getInstance()->setSource('StartUp');
systemLog::message('--------------------------------------------------');
systemLog::message('Initialising Logging Daemon');
class myDaemon extends cliDaemon {
function __construct() {
parent::__construct('myDaemon', 'Example Daemon');
}
function execute() {
$this->setStatusParam('Status', 'Running');
$this->updateStatus();
$loop = true;
do {
if ( $this->signalTrapped() ) {
$loop = false;
}
// do something cool in here that takes ages or processes
// lots of data or whatever. Then have a little rest.
sleep(2);
} while ( $loop === true );
}
function terminate() {
$this->setStatusParam('Status', 'Stopped');
$this->updateStatus();
return true;
}
}
// get request, initialise cli and daemonise
$oRequest = cliRequest::getInstance();
cliProcessControls::initialise($oRequest, 'loggingDaemon');
cliProcessControls::daemonise();
// now start up the daemon
$oDaemon = new myDaemon();
$oDaemon->setPosixId(cliProcessControls::getPosixId());
$oDaemon->setPidFile(cliProcessControls::getPidFile());
$oDaemon->setPosixUser(system::getConfig()->getSystemUserId());
$oDaemon->setPosixGroup(system::getConfig()->getSystemGroupGid());
$oDaemon->trapSignal(SIGINT, SIGHUP, SIGTERM); // exit on these signals
$oDaemon->getListeners()->attachListener(new cliApplicationListenerLog());
$oDaemon->execute();
Note: you need to set-up your own signal handling to capture signals from other processes. While this is installed by cliApplication, YOU have to check for trapped signals within your main process loop. Failure to do so will mean that any kill -9 won't be handled gracefully and you may suffer data loss.
Note 2: signal handling changed in PHP5.3+ see cliProcessControls for examples of setting up signal handling.
public __construct([$inAppName = null], [$inAppDescription = null])
Returns a new cliDaemon object
public execute($inRequest)
Executes the application stack
public getLastStatusUpdate()
Returns the time of the last update status
public updateStatus()
Set last status update to now
public setStatusParam($inParamName, $inParamValue)
Sets a param to monitor with a value to record, these are written to a status file
public unsetStatusParam($inParamName)
Remove a param from the daemonStatus array
public resetStatusParams()
Removes all current status params, resetting to an empty array
public writeStatus()
Writes out the daemonStatus params to a file
public setPosixId($inPosixId)
Set the process ID
public setPosixUser($inUserID)
Set the posix userId
public setPosixGroup($inGroupID)
Set the posix groupId
public setPidFile($inPidFile)
Set the PID file
public destructor()
Global daemon destructor, calls the terminate() method if it has been defined in an extended class
public terminate()
Performs custom clean-up on daemon shutdown
Tags: cli, cliapplication, clidaemon,
cli Articles