Пример #1
0
    def callback(self, ch, method, properties, body):
        """
        Sets channel as class variable.
        Runs a task.
        Sends message to next queue, back to the same queue or
        to queue with the failed tasks depended on task result.
        """
        result_status = None
        self.channel = ch
        self.body = simplejson.loads(body)
        self.body[Body.WORKER_NR] = self.worker_nr
        self.body[Body.NODE] = self.node
        self.properties = properties

        if self.body.get(Body.IS_HEARTBEAT):
            self.body[Body.TIME] = time.time()
            self.body[Body.WORKER_STATUS] = Action.ALIVE
            self.log.info("HEARTBEAT AT {}".format(datetime.datetime.today().isoformat()))
            ch.basic_reject(delivery_tag=method.delivery_tag, requeue=False)
            return

        self.set_task_status(self.STARTED)
        self.log.info("Task is {}".format(self.STARTED))
        self.set_task_status("")

        try:
            result_status = perform_task(
                self.body[Body.SCENARIO_ID], int(self.task_code), self.worker_nr, self.broker_logging_handler
            )
        except Exception as ex:
            self.log.error("{0}".format(ex))
            result_status = False

        if self.status_task(result_status):
            self.set_task_status(self.SUCCESS)
            self.log.info("Task is finished with {}.".format(self.SUCCESS))
            self.set_task_status("")
            self.proceed_next_trigger()
        else:
            self.set_task_status(self.FAILED)
            self.log.info("Task is {}".format(self.FAILED))
            self.set_task_status("")
            self.requeue_failed_message(ch, method)
        ch.basic_reject(delivery_tag=method.delivery_tag, requeue=False)
Пример #2
0
 def handle(self, *args, **options):
     perform_task(options['scenario_id'],
                  options['tasktype_id'],
                  options['worker_nr'])