-
Notifications
You must be signed in to change notification settings - Fork 15
Events
The core Daemon class has many events that can trigger during the life time of your application. These events allow you to intercept various behaviors of the daemon and either alter the behavior of the event or use them to trigger your own code to do almost anything.
An example of listening to some events and acting on them.
use Lifo\Daemon\Daemon;
use Lifo\Daemon\Event\StatsEvent;
class MyDaemon extends Daemon {
protected function initialize() {
$this->on(DaemonEvent::ON_STATS, function(StatsEvent $e) {
$stats = $e->getStats();
// add a single, trival 'stat' to the stats array.
$stats['random_number'] = mt_rand();
$e->setStats($stats);
});
$this->on(DaemonEvent::ON_IDLE, function(DaemonEvent $e) {
// hmmm. we're idle?! Interesting. Will be called anytime the daemon is considered to be 'idle'
});
}
}
The DaemonEvent
class has a list of constants you can use to target different event names.
-
ON_ERROR
Event fired every timeDaemon::error()
is called. If propagation is stopped normal error handling is not performed.
Receives anErrorEvent
object. -
ON_FORK
Event fired every time a process is forked. The CHILD process receives the event.
Receives anDaemonEvent
object. -
ON_GENERATE_GUID
Event fired when theMediator
generates a GUID for Workers. If a GUID is set on the event then it will be used instead of generating a new one inMediator::generateId
Receives anGuidEvent
object. -
ON_IDLE
Event fired every time the daemon is idle.
Receives anDaemonEvent
object. -
ON_INIT
Event fired during daemon initialization. This is useful for Plugins. So they can initialize themselves when the daemon starts up. Called once.
Receives anDaemonEvent
object. -
ON_LOG
Event fired every timeDaemon::log()
is called. If propagation is stopped normal log handling is not performed.
Receives anLogEvent
object. -
ON_PARENT_FORK
Event fired every time a process is forked. The PARENT process receives the event.
Receives anDaemonEvent
object. -
ON_PID_CHANGE
Event fired if the parent PID changes. This is usually only called once after the main Daemon is forked into the background.
Receives anPidEvent
object. -
ON_POST_EXECUTE
Event fired afterDaemon::execute
is called. If propagation is stopped from any handler thenDaemon::wait
is not called.
Receives anDaemonEvent
object. -
ON_PRE_EXECUTE
Event fired beforeDaemon::execute
is called. If propagation is stopped from any handler then (Daemon::execute
) is not called.
Receives anDaemonEvent
object. -
ON_REAPED
Event fired every time a child is reaped from theProcessManager
plugin. This event is NOT fired from within theSIGCHLD
signal. So free to take as long as you want inside your callback.
Receives anReapedEvent
object. -
ON_SHUTDOWN
Event fired when the daemon goes into shutdown mode. This is the last method called before the daemon exits. You can still use Plugins. Some methods within the Daemon will not work once it's inshutdown
mode.
Receives anDaemonEvent
object. -
ON_SIGNAL
Event fired every time an OS Signal is caught. Handlers should do as little as possible within this event. See Signal Handlers for more information on catching this event.
Receives anSignalEvent
object. -
ON_STATS
Event fired every timeDaemon::stats
is called. Allows plugins and your main application to add or modify the stats that are returned.
Receives anStatsEvent
object.