-
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.
Event fired every time Daemon::error()
is called. If propagation is stopped normal error handling is not performed.
Receives an ErrorEvent
object.
Event fired every time a process is forked. The CHILD process receives the event.
Receives an DaemonEvent
object.
Event fired when the Mediator
generates a GUID for Workers. If a GUID is set on the event then it will be used instead of generating a new one in Mediator::generateId
Receives an GuidEvent
object.
Event fired every time the daemon is idle.
Receives an DaemonEvent
object.
Event fired during daemon initialization. This is useful for Plugins. So they can initialize themselves when the daemon starts up. Called once.
Receives an DaemonEvent
object.
Event fired every time Daemon::log()
is called. If propagation is stopped normal log handling is not performed.
Receives an LogEvent
object.
Event fired every time a process is forked. The PARENT process receives the event.
Receives an DaemonEvent
object.
Event fired if the parent PID changes. This is usually only called once after the main Daemon is forked into the background.
Receives an PidEvent
object.
Event fired after Daemon::execute
is called. If propagation is stopped from any handler then Daemon::wait
is not called.
Receives an DaemonEvent
object.
Event fired before Daemon::execute
is called. If propagation is stopped from any handler then (Daemon::execute
) is not called.
Receives an DaemonEvent
object.
Event fired every time a child is reaped from the ProcessManager
plugin. This event is NOT fired from within the SIGCHLD
signal. So free to take as long as you want inside your callback.
Receives an ReapedEvent
object.
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 in shutdown
mode.
Receives an DaemonEvent
object.
Event fired every time an OS Signal is caught. Handlers should do as little as possible within this event. See Signal Handling for more information on catching this event.
Receives an SignalEvent
object.
Event fired every time Daemon::stats
is called. Allows plugins and your main application to add or modify the stats that are returned.
Receives an StatsEvent
object.