Skip to content

Quick Start Guide

Jason M edited this page May 31, 2017 · 4 revisions

Installation

Adding the library to your application is very simple:

  1. Add the library to your composer.json: composer require lifo/php-daemon
  2. that's it

Setup Basic Daemon

A real daemon application will require a few things to make it more useful then some of the examples you may have seen so far. Sure, in just a few lines of code you can have a daemon up and running but it won't be very interesting. Here, we'll setup a daemon that uses the FileLock plugin so the daemon will only allow 1 instance to run at a time. We'll use a few common configuration settings. And we'll listen for some Events and act on them.

QuickStartDaemon Code

<?php

use Lifo\Daemon\Daemon;
use Lifo\Daemon\Event\DaemonEvent;
use Lifo\Daemon\Event\SignalEvent;

class QuickStartDaemon extends Daemon
{

    protected function initialize()
    {
        // add the FileLock plugin. Does not allow a second QuickStartDaemon from running
        $this->addPlugin('Lifo\Daemon\Plugin\Lock\FileLock', [
            // if the lock isn't updated within X seconds, then other QuickStartDaemon processes will see it as stale
            'ttl'  => 10,
            'file' => '/tmp/quick_start_daemon.pid',
        ]);

        // when the daemon goes into shutdown mode, call our function so your daemon can clean itself up.
        // the onShutdown call is wrapped in a function so that it can remain a 'private' method 
        // (instead of using [$this, 'onShutdown'] which would require the method to be public)
        $this->on(DaemonEvent::ON_SHUTDOWN, function () {
            $this->onShutdown();
        });

        // listen for any signals caught and log it
        $this->on(DaemonEvent::ON_SIGNAL, function (SignalEvent $e) {
            $this->log("Signal %d caught!", $e->getSignal());
        });
    }

    protected function execute()
    {
        $this->log("Loop %d", $this->getLoopIterations());
        // do stuff here!!
        // note: this function should usually return as quickly as possible.
    }

    protected function onShutdown()
    {
        $this->log("OH NO!!! we're shutting down! I better do some clean up");
        // do stuff here...
    }
}

QuickStartDaemon Setup

Our QuickStartDaemon daemon needs some initial configuration before it runs. So in our entry script we configure it and then call run().

<?php

require __DIR__ . '/vendor/autoload.php';

declare(ticks = 1); // needed for signal handling

QuickStartDaemon::getInstance()
  ->setDaemonize(false) 
  ->setLoopInterval(1)
  ->setVerbose(true)
  ->setDebug(true)
  ->setDebugLevel(3)
  ->setLogFile('/tmp/quick_start_daemon.log')
  ->run();

The daemon will now run! If you want to see the FileLock plugin actually work then either try to run the daemon in another window or call setDaemonize(true) and then try to run multiple instances of the daemon. Note: if you daemonize it, you can run kill -INT {pid} to kill the process. Look at the daemon log to see it's PID.

Clone this wiki locally