def create_workflow(self):
     """
     Creates and sets workflow, tasks.
     """
     try:
         template = WorkflowTemplate.objects.get(pk=self.workflowtemplate_id)
         if template is None:
             self.log.warning("Workflow template '%s' does not exist.")
             return False
         template_tasks = WorkflowTemplateTask.objects.filter(workflow_template=template.id)
         if template_tasks.exists() == False:
             self.log.warning("Workflow template '%s' has not any task." % template.code)
             return False
         # Adde field tcreated to workflow
         self.workflow = Workflow(
             scenario=self.scenario_id,
             code=template.code,
             template=template,
             tcreated=datetime.datetime.today(),
             priority=self.workflowpriority,
         )
         self.workflow.save()
         # TODO define default body
         # self.log.debug("Created workflow '%s' for scenario '%s'." % (
         #         template.code, self.scenario_id))
         self.bulk_tasks = []
         for template_task in template_tasks:
             task = WorkflowTask(
                 workflow=self.workflow,
                 code=template_task.code,
                 tcreated=datetime.datetime.today(),
                 parent_code=template_task.parent_code,
                 max_failures=template_task.max_failures,
                 max_duration_minutes=template_task.max_duration_minutes,
             )
             task.save()
             self.bulk_tasks.append(task)
         return True
     except Exception as ex:
         self.log.error("{0}".format(ex))
         return False
class ActionWorkflow(Action):
    def __init__(self, connection, scenario_id, workflowtemplate_id, workflowpriority=0, worker_nr="999"):
        self.connection = connection
        self.log = logging.getLogger("flooding.action.workflow")
        self.scenario_id = scenario_id
        self.workflowtemplate_id = workflowtemplate_id
        self.workflowpriority = workflowpriority
        self.workflow = None
        self.bulk_tasks = []
        self.channel = self.connection.channel()
        self.worker_nr = worker_nr

    def callback(self, ch, method, properties, body):
        pass

    def perform_workflow(self):
        """
        Creates workflow and tasks.
        Creates message body as instruction
        Sends message to next queue(s)
        """
        if not self.create_workflow():
            # TODO define default body or log it only to logfile
            # self.log.error("Workflow is interrupted.")
            return

        # self.body = self.retrieve_workflow_options()
        self.set_message_body()
        self.set_message_properties()
        try:
            self.start_workflow()
            success = True
        except:
            success = False
        return success

    def create_workflow(self):
        """
        Creates and sets workflow, tasks.
        """
        try:
            template = WorkflowTemplate.objects.get(pk=self.workflowtemplate_id)
            if template is None:
                self.log.warning("Workflow template '%s' does not exist.")
                return False
            template_tasks = WorkflowTemplateTask.objects.filter(workflow_template=template.id)
            if template_tasks.exists() == False:
                self.log.warning("Workflow template '%s' has not any task." % template.code)
                return False
            # Adde field tcreated to workflow
            self.workflow = Workflow(
                scenario=self.scenario_id,
                code=template.code,
                template=template,
                tcreated=datetime.datetime.today(),
                priority=self.workflowpriority,
            )
            self.workflow.save()
            # TODO define default body
            # self.log.debug("Created workflow '%s' for scenario '%s'." % (
            #         template.code, self.scenario_id))
            self.bulk_tasks = []
            for template_task in template_tasks:
                task = WorkflowTask(
                    workflow=self.workflow,
                    code=template_task.code,
                    tcreated=datetime.datetime.today(),
                    parent_code=template_task.parent_code,
                    max_failures=template_task.max_failures,
                    max_duration_minutes=template_task.max_duration_minutes,
                )
                task.save()
                self.bulk_tasks.append(task)
            return True
        except Exception as ex:
            self.log.error("{0}".format(ex))
            return False

    def set_message_body(self):
        """
        Creates a body as instruction mechanizm
        for messaging.
        """
        self.body = Body().body
        instruction = {}
        workflow_tasks = {}
        task_failures = {}

        for task in self.bulk_tasks:
            instruction[task.code.name] = task.parent_code.name
            workflow_tasks[task.code.name] = unicode(task.id)
            task_failures[task.code.name] = task.max_failures

        self.body[Body.INSTRUCTION] = instruction
        self.body[Body.WORKFLOW_TASKS] = workflow_tasks
        self.body[Body.MAX_FAILURES] = task_failures
        self.body[Body.MAX_FAILURES_TMP] = task_failures
        self.body[Body.WORKFLOW_ID] = self.workflow.id
        self.body[Body.PRIORITY] = self.workflow.priority
        self.body[Body.SCENARIO_ID] = self.scenario_id
        self.body[Body.TIME] = time.time()

    def start_workflow(self):
        """
        Send trigger and logging messages to broker.
        """

        queues = self.root_queues()
        for queue in queues:
            self.set_task_status(self.QUEUED)
            self.set_current_task(queue)
            self.log.info("Task is {}.".format(self.QUEUED))
            self.send_trigger_message(self.body, "Message emitted to queue %s" % queue, queue)

    def set_message_properties(self, priority=0, message_id=0):
        if self.workflow is not None:
            priority = self.workflow.priority
            message_id = self.workflow.id

        self.properties = BasicProperties(
            content_type="application/json", delivery_mode=2, priority=priority, message_id=str(message_id)
        )