def do_task(self, **params): """Perform the next ready task in the queue. """ # Get next task and lock it. t = model.Task.next() if not t: self.log.info("No ready tasks found.") return LOOP_SLEEP t.start(self) Session.commit() if not t.check_lock(self): # Another worker got the task, abort. return LOOP_CONTINUE # Perform task self.log.info("Starting task: {0}".format(t)) fn = get_task_method(t.method) try: r = fn(task_id=t.id, **t.params) self.log.info("Task completed: {0}".format(t)) t.complete(self) except TaskDelayResource, e: new = t.retry(self, delay=e.delay) self.log.warn("Task error, will retry in {0} seconds: {1}".format(e.delay, e.message)) resource = e.resource or t.resource_group if resource: self.log.warn("Delaying resource group by {0} seconds: {1}".format(e.delay, resource)) model.Task.delay_resource_group(resource, timedelta(seconds=e.delay))
def rebuild(worker, **params): method = params.get('method') if not method: log.error("No rebuild method given.") fn = get_task_method(method) if not fn: log.error("Rebuild method not found: %s" % method) log.debug("Deleting pending tasks.") t = model.Task.__table__ q = t.delete().where(t.c.state<'completed') Session.execute(q).close() Session.commit() fn(worker)