Skip to content
Jason M edited this page Jul 5, 2017 · 3 revisions

Synopsis

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.

Example

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'
        });
    }
}

Events List

The DaemonEvent class has a list of constants you can use to target different event names.

ON_ERROR

Event fired every time Daemon::error() is called. If propagation is stopped normal error handling is not performed.
Receives an ErrorEvent object.

ON_FORK

Event fired every time a process is forked. The CHILD process receives the event.
Receives an DaemonEvent object.

ON_GENERATE_GUID

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.

ON_IDLE

Event fired every time the daemon is idle.
Receives an DaemonEvent 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 an DaemonEvent object.

ON_LOG

Event fired every time Daemon::log() is called. If propagation is stopped normal log handling is not performed.
Receives an LogEvent object.

ON_PARENT_FORK

Event fired every time a process is forked. The PARENT process receives the event.
Receives an DaemonEvent 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 an PidEvent object.

ON_POST_EXECUTE

Event fired after Daemon::execute is called. If propagation is stopped from any handler then Daemon::wait is not called.
Receives an DaemonEvent object.

ON_PRE_EXECUTE

Event fired before Daemon::execute is called. If propagation is stopped from any handler then (Daemon::execute) is not called.
Receives an DaemonEvent object.

ON_REAPED

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.

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 in shutdown mode.
Receives an DaemonEvent object.

ON_SIGNAL

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.

ON_STATS

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.