def work(self): """Work on jobs.""" if not self.job: return # Try Queuey heartbeat connection self.queuey_conn.connect() self.configure_partitions() atexit.register(self.stop) timer = get_logger().timer partitioner = self.partitioner with self.job_context() as context: if partitioner.allocating: partitioner.wait_for_acquire(self.zk_party_wait) waited = 0 while 1: if self.shutdown or partitioner.failed: break if partitioner.release: partitioner.release_set() elif partitioner.allocating: partitioner.wait_for_acquire(self.zk_party_wait) elif partitioner.acquired: no_messages = 0 partitions = list(self.partitioner) for name in partitions: partition = self.partition_cache[name] messages = partition.messages(limit=2) if not messages: no_messages += 1 continue message = messages[0] message_id = message['message_id'] try: with timer('worker.job_time'): self.job(message, context) except StopWorker: self.shutdown = True break except Exception as exc: with timer('worker.job_failure_time'): self.job_failure(message, context, name, exc, self.queuey_conn) # record successful message processing partition.last_message = message_id if no_messages == len(partitions): # if none of the partitions had a message, wait self.wait(waited) waited += 1 else: waited = 0 # give up the partitions and leave party self.partitioner.finish()
def _log_raven(): """Log exceptions using metlog-raven if it's configured.""" logger = get_logger() raven = getattr(logger, 'raven', None) if raven is not None: raven()
def wait(self, waited=1): get_logger().incr('worker.wait_for_jobs') jitter = random.uniform(0.8, 1.2) time.sleep(self.wait_interval * jitter * 2 ** min(waited, 10))
def wait(self, waited=1): get_logger().incr('worker.wait_for_jobs') jitter = random.uniform(0.8, 1.2) time.sleep(self.wait_interval * jitter * 2**min(waited, 10))