def bind(self): """Bind the ZMQ socket """ if self._socket: raise error.StateError("Socket is already bound") self._socket = self._context.socket(zmq.ROUTER) self._socket.rcvtimeo = int(self._heartbeat_interval * 1000) self._socket.bind(self._bind_url) self._log.info("Broker listening on %s", self._bind_url)
def add_worker(self, worker_id, service, expire_at, next_heartbeat): # type: (bytes, bytes, float, float) -> Worker """Add a worker to the list of available workers """ if worker_id in self._workers: raise error.StateError( "Worker '{}' has already sent READY message".format(worker_id)) worker = Worker(worker_id, service, expire_at, next_heartbeat) self._workers[worker_id] = worker self._services[service].add_worker(worker) return worker
def set_worker_busy(self, worker_id, expire_at): # type: (bytes, float) -> Worker """Mark a worker as busy - that is currently processing a request and not available for more work """ if worker_id not in self._workers: raise error.StateError( "Worker id {} is not in the list of available workers".format( id_to_int(worker_id))) worker = self._workers.pop(worker_id) worker.is_busy = True worker.expire_at = expire_at self._busy_workers[worker.id] = worker return worker
def remove_worker(self, worker_id): # type: (bytes) -> None """Remove a worker from the list of known workers """ try: worker = self._workers[worker_id] self._services[worker.service].remove_worker(worker_id) del self._workers[worker_id] except KeyError: try: del self._busy_workers[worker_id] except KeyError: raise error.StateError( "Worker id {} is not known".format(worker_id))
def set_worker_available(self, worker_id, expire_at, next_heartbeat): # type: (bytes, float, float) -> Worker """Mark a worker that was busy processing a request as back and available again """ if worker_id not in self._busy_workers: raise error.StateError( "Worker id {} is not previously known or has expired".format( id_to_int(worker_id))) worker = self._busy_workers.pop(worker_id) worker.is_busy = False worker.expire_at = expire_at worker.next_heartbeat = next_heartbeat self._workers[worker_id] = worker self._services[worker.service].add_worker(worker) return worker
def _send_reply(self, method, *args, **kwargs): if self._last_client is None: raise error.StateError("Cannot send reply: no client waiting") method(self._last_client, *args, **kwargs)