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
Die Kommentarfunktion wurde vom Besitzer dieses Blogs in diesem Eintrag deaktiviert.
Kommentare
Ansicht der Kommentare: Linear | Verschachtelt