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.

web - php orm project propel has a new leader

William Durand – 23 April 2014

Three years ago, I became the lead developer of the Propel ORM project, a decision that probably changed my life, and, for sure, literally propelled me out of my comfortable zone. This was not easy. I was young, inexperienced, and a sort of "outsider".

While Propel was a "one-man project" thanks to the awesome François, I decided not to follow such path. I rather created an organization, gathering skilled people, and new projects, with a long-term vision in mind. That is actually what I like the most in Open Source, enrolling new people to this wonderful world, and giving them responsibilities or, at least, opportunities to become Open Source developers.

In three years, Propel grew up a lot: a brand new upcoming version, more documentation, more tests, more components to integrate with various frameworks, but also more standard tools and best practices. The whole project moved to Git and GitHub, I simplified the way releases were built, as well as, how one could contribute to the project (code but also documentation and, generally speaking, everything related to the project at large). In my opinion, this is my main contribution to the project, and my only regret is not to ship Propel2 in a stable version myself.

But now, after three years, it's time to step down. Having me as project leader is what prevents Propel from developing even faster, and for two simple reasons: I don't have enough time to dedicate to it, and I don't do any web programming anymore. I took over the project three years ago because I did not want to see it die, not because I wanted to lead an Open Source project. Today, Propel is still alive, and I like to think that the long-term vision I chose to follow three years ago is not completely unrelated.

I am sincerely glad to welcome Marc J. Schmidt as new lead developer of Propel. He is @MarcJSchmidt on Twitter, and @marcj on GitHub. If you follow the Propel2 development, I am sure you know him already! He is German, and an amazing developer. His understanding of the Propel design, of the Open Source philosophy, and of the ORM landscape, would be enough to let him lead the project. Note that I won't abandon the project right away. I will be right behind his shoulder watching him for a while.

I am very happy for Propel. I’m also very happy for me, this will, hopefully, give me some time for other activities. And most of all, I’m very happy for Marc, who deserves a warm welcome from you guys! :heart:

source (with clickable links)

Thanks for the great work so far William Durand. You did a great job on reactivating and restructuring propel.
Good luck to Marc J. Schmidt, I am glad you have accept the challange! :-)

web - Edward Snowden in Berlin für Zivilcourage ausgezeichnet

NSA-Whistleblower Edward Snowden ist am vergangenen Wochenende gleich zweimal für sein Engagement ausgezeichnet worden. Der ehemalige Geheimdienstmitarbeiter machte auf die rechtswidrigen Überwachungspraktiken des US-Geheimdienstes NSA aufmerksam, indem er streng geheime Dokumente an die Presse weitergab.

Quelle und ganzer Artikel

Wie Chelsea Manning einer der zwei wichtigen Persönlichkeiten seit 2012 für unsere westliche, so aufgeklärte, Gesellschaft. Spendet für Beide, geht für beide auf die Straße, sie habe für uns schon ihr Leben geopfert.

Nicht nur TTIP wird geheim verhandelt, auch TISA existiert und enthält böse Absichten

So schafft man Vertrauen. Dank wikileaks.org wurde bekannt, dass nicht nur am weltenvernichtenden TTIP im geheimen verhandelt wird, nein auch TISA gibt es - natürlich auch nur im Geheimen.
Besonders "nett", die Öffentlichkeit darf erst fünf Jahre nach dem Inkrafttreten des Papiers darüber veröffentlicht werden. Um was geht es bei TISA? Um altbekannte Wünsche, Energie- und Wasserversorgung, dem nutzlosen Datenschutz und eine weitere Deregulierung des Finanzmarkts.
Kurzum "Gewinne privatisieren, Verluste sozialisieren". Mir fällt dazu eine Antwort und eine Frage ein. Antwort: "Abwählen!", Frage: "Mit welchen Drücken arbeiten die Amerikaner da, dass europäische Politiker überhaupt darüber reden? Sind wir so kurz vor einem Krieg?".

Ihr findet das auch schlecht und wollt wikileaks.org helfen? Ihr könnt dies, direkt oder indirekt, es liegt an euch.