def _add_worker(self, worker_id, service_name, ttl, worker_type, log_added=True): """ Adds worker-related configuration, no matter if it is an internal or a ZeroMQ-based one. """ now = datetime.utcnow() expires_at = now + timedelta(seconds=ttl) wd = WorkerData(worker_type, worker_id, service_name, now, None, expires_at) # Add to details of workers self.workers[wd.id] = wd # Add to the list of workers for that service (but do not forget that the service may not have a client yet possibly) service = self.services.setdefault(service_name, Service(service_name)) service.workers.append(wd.id)
def on_event_disconnect(self, worker_id, data): """ A worker wishes to disconnect - we need to remove it from all the places that still reference it, if any. """ with self.lock: wrapped_id = WorkerData.wrap_worker_id(const.worker_type.zmq, worker_id) # Need 'None' because the worker may not exist self.workers.pop(wrapped_id, None) for service in self.services.values(): # Likewise, this worker may not exist at all if wrapped_id in service.workers: service.workers.remove(wrapped_id)
def on_event_ready(self, worker_id, service_name): """ A worker informs the broker that it is ready to handle messages destined for a given service. Must be called with self.lock held. """ service_name = service_name[0] if self.has_info: logger.info('Worker `%r` ready for `%s`', WorkerData.wrap_worker_id(const.worker_type.zmq, worker_id), service_name) with self.lock: self._add_worker(worker_id, service_name, const.ttl, const.worker_type.zmq) self.dispatch_requests(service_name)
def on_event_heartbeat(self, worker_id, _ignored): """ Updates heartbeat data for a worker. """ with self.lock: wrapped_id = WorkerData.wrap_worker_id(const.worker_type.zmq, worker_id) worker = self.workers.get(wrapped_id) if not worker: logger.warn('No worker found for HB `%s`', wrapped_id) return now = datetime.utcnow() expires_at = now + timedelta(seconds=const.ttl) worker.last_hb_received = now worker.expires_at = expires_at