Skip to content

POSIX signal handling in a php zend framework 2 console controller action call

I played around with php console applications for a while now. The projects are getting bigger and sometimes, the load on the system or the runtime itself increases a lot.
At night, it made "click" and I asked myself "Are you nuts or why aren't you simple using POSIX signals for your php console applications (like normal people are doing it)?". Thanks to the book Signaling PHP, I could "kickstart" into the topic and created a simple example script.

While solving this problem and migrating some own scripts, I wanted to implement the signal handling in a bigger zend framework 2 project.
Quickly done, I copy/pasted/adapted my example code and thought "this is it". To bad, I need to take one pitfall. I could not debug it right know, but zf2 needs to have the "signal handler method" public. The following code is the final outcome of this adaptation session.


class MyController extends AbstractCliActionController
{
    //AbstractCliActionController extends zend AbstractActionController
    //...usefull controller code
    /**
     * @param AbstractCliActionController $object
     * @param string $method
     */
    protected function attachSignalHandler(AbstractCliActionController $object, $method = 'defaultSignalHandler')
    {
        declare(ticks = 10);

        pcntl_signal(SIGHUP,    array($object, $method));
        pcntl_signal(SIGINT,    array($object, $method));
        pcntl_signal(SIGUSR1,   array($object, $method));
        pcntl_signal(SIGUSR2,   array($object, $method));
        pcntl_signal(SIGQUIT,   array($object, $method));
        pcntl_signal(SIGILL,    array($object, $method));
        pcntl_signal(SIGABRT,   array($object, $method));
        pcntl_signal(SIGFPE,    array($object, $method));
        pcntl_signal(SIGSEGV,   array($object, $method));
        pcntl_signal(SIGPIPE,   array($object, $method));
        pcntl_signal(SIGALRM,   array($object, $method));
        pcntl_signal(SIGCONT,   array($object, $method));
        pcntl_signal(SIGTSTP,   array($object, $method));
        pcntl_signal(SIGTTIN,   array($object, $method));
        pcntl_signal(SIGTTOU,   array($object, $method));
    }

    /**
     * @param $signal
     */
    public function defaultSignalHandler($signal)
    {
        echo 'caught signal "' . $signal . '"' . PHP_EOL;
    }
}

You can easily extend the method "defaultSignalHandler" to whatever you need (push something to a log, flip a flag to stop execution etc) and thats it. Injoy your work :-).

Thanks to the nice irc freenode channel #zftalk, I can provide a second implementation to prove my example to be right.

Trackbacks

Keine Trackbacks

Kommentare

Ansicht der Kommentare: Linear | Verschachtelt

Noch keine Kommentare

Die Kommentarfunktion wurde vom Besitzer dieses Blogs in diesem Eintrag deaktiviert.

Kommentar schreiben

Die angegebene E-Mail-Adresse wird nicht dargestellt, sondern nur für eventuelle Benachrichtigungen verwendet.
Um einen Kommentar hinterlassen zu können, erhalten Sie nach dem Kommentieren eine E-Mail mit Aktivierungslink an ihre angegebene Adresse.

Um maschinelle und automatische Übertragung von Spamkommentaren zu verhindern, bitte die Zeichenfolge im dargestellten Bild in der Eingabemaske eintragen. Nur wenn die Zeichenfolge richtig eingegeben wurde, kann der Kommentar angenommen werden. Bitte beachten Sie, dass Ihr Browser Cookies unterstützen muss, um dieses Verfahren anzuwenden.
CAPTCHA

Markdown-Formatierung erlaubt
Formular-Optionen