Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disconnect ZeroMQ socket after timeout?

Tags:

php

zeromq

I've been using ZeroMQ pretty successfully with PHP and wrote a job server. I have an admin script which checks on the health of the server and does stuff like pinging it with a timeout.

Everything works great when the server is up, but when it's down (and ZMQPoll times out as expected) my script does what it's supposed to - BUT the script never terminates.

I've tried cleaning up and unsetting all socket variables, etc.. but even calling exit() the PHP script hangs.

There does not appear to be a socket disconnect() method, so how do I tell PHP that the socket is dead and I don't want it to hang?

This is a snippet of code below from my admin script -

        // ...
        // waiting for dead server on zmqsock to respond after sending a message
        // 
        $poll = new ZMQPoll;
        $poll->add( $this->zmqsock, ZMQ::POLL_IN );
        $readable = $writeable = array();
        $poll->poll( $readable, $writeable, $timeout * 1000 );
        if( $errors = $poll->getLastErrors() ) {
            foreach ( $errors as $err ) {
                throw new Exception($err);
            }
        }
        if( ! $readable ){
             // clean up everything, raise errors, etc..
             $poll->clear();
             unset( $poll, $this->zmqsock, $this->zmqcontext );
             // Script hangs here
             exit(0);
        }
        // ..
like image 943
Tim Avatar asked Jun 08 '12 10:06

Tim


1 Answers

ZeroMQ will try and deliver pending messages when it shuts down - you can control that by setting the ZMQ::SOCKOPT_LINGER socket option (which you must do before you connect), which should allow you to terminate quickly.

Take a look at the ZMQ_LINGER bit on http://api.zeromq.org/2-1:zmq-setsockopt

like image 141
Ian Barber Avatar answered Oct 24 '22 09:10

Ian Barber