Ejemplo n.º 1
0
    def _try_fire(self, my_task, force=False):
        """Returns False when successfully fired, True otherwise"""

        # Deserialize async call if necessary
        if not hasattr(my_task, 'async_call') and \
                my_task._get_internal_attribute('task_id') is not None:
            task_id = my_task._get_internal_attribute('task_id')
            my_task.async_call = default_app.AsyncResult(task_id)
            my_task.deserialized = True
            LOG.debug("Reanimate AsyncCall %s" % task_id)

        # Make the call if not already done
        if not hasattr(my_task, 'async_call'):
            self._send_call(my_task)

        # Get call status (and manually refresh if deserialized)
        if getattr(my_task, "deserialized", False):
            my_task.async_call.state  # must manually refresh if deserialized
        if my_task.async_call.state == 'FAILURE':
            LOG.debug("Async Call for task '%s' failed: %s" % (
                    my_task.get_name(), my_task.async_call.info))
            info = {}
            info['traceback'] = my_task.async_call.traceback
            info['info'] = Serializable(my_task.async_call.info)
            info['state'] = my_task.async_call.state
            my_task._set_internal_attribute(task_state=info)
        elif my_task.async_call.state == 'RETRY':
            info = {}
            info['traceback'] = my_task.async_call.traceback
            info['info'] = Serializable(my_task.async_call.info)
            info['state'] = my_task.async_call.state
            my_task._set_internal_attribute(task_state=info)
        elif my_task.async_call.ready():
            result = my_task.async_call.result
            if isinstance(result, Exception):
                LOG.warn("Celery call %s failed: %s" % (self.call, result))
                my_task._set_internal_attribute(error=Serializable(result))
                return False
            LOG.debug("Completed celery call %s with result=%s" % (self.call,
                    result))
            # Format result
            if self.result_key:
                data = {self.result_key: result}
            else:
                if isinstance(result, dict):
                    data = result
                else:
                    data = {'result': result}
            # Load formatted result into attributes
            if self.merge_results:
                merge_dictionary(my_task.attributes, data)
            else:
                my_task.set_attribute(**data)
            return True
        else:
            LOG.debug("async_call.ready()=%s. TryFire for '%s' "
                    "returning False" % (my_task.async_call.ready(),
                            my_task.get_name()))
            return False
Ejemplo n.º 2
0
 def _do_join(self, my_task):
     # Merge all inputs (in order)
     for input_spec in self.inputs:
         tasks = [task for task in my_task.workflow.task_tree
                 if task.task_spec is input_spec]
         for task in tasks:
             LOG.debug("Merging %s (%s) into %s" % (task.get_name(),
                     task.get_state_name(), self.name),
                     extra=dict(data=task.data))
             _log_overwrites(my_task.data, task.data)
             merge_dictionary(my_task.data, task.data)
     return super(Merge, self)._do_join(my_task)
Ejemplo n.º 3
0
    def _do_join(self, my_task):
        tasks = [task for task in my_task.workflow.get_tasks()
                 if task.task_spec is self]

        extend_lists = self.get_property("extend_lists", False)
        # Merge all tasks
        for task in tasks:
            LOG.debug("Merging %s (%s) into %s" % (task.parent.get_name(),
                    task.parent.get_state_name(), self.name),
                    extra=dict(data=task.attributes))
            log_overwrites(my_task.attributes, task.attributes)
            merge_dictionary(my_task.attributes, task.attributes, extend_lists)
        return super(Merge, self)._do_join(my_task)
Ejemplo n.º 4
0
 def _do_join(self, my_task):
     extend_lists = self.get_property("extend_lists", False)
     # Merge all inputs (in order)
     for input_spec in self.inputs:
         tasks = [task for task in my_task.workflow.task_tree
                 if task.task_spec is input_spec]
         for task in tasks:
             LOG.debug("Merging %s (%s) into %s" % (task.get_name(),
                     task.get_state_name(), self.name),
                     extra=dict(data=task.attributes))
             log_overwrites(my_task.attributes, task.attributes)
             merge_dictionary(my_task.attributes, task.attributes, extend_lists)
     return super(Merge, self)._do_join(my_task)
 def my_code(self, my_task):
     fields = ['foo', 'bar']
     # Get fields from task and store them in spec
     for key in fields:
         if key in my_task.attributes:
             merge_dictionary(self.properties, dict(collect={
                     'my_%s' % key: my_task.attributes[key],
                     key: my_task.attributes[key]}))
     # Check if we have all fields
     collected = self.get_property('collect', {})
     if any(key for key in fields if key not in collected):
         return False
     my_task.set_attribute(**collected)
     return True
Ejemplo n.º 6
0
    def _do_join(self, my_task):
        tasks = [
            task for task in my_task.workflow.get_tasks()
            if task.task_spec is self
        ]

        extend_lists = self.get_property("extend_lists", False)
        # Merge all tasks
        for task in tasks:
            LOG.debug("Merging %s (%s) into %s" %
                      (task.parent.get_name(), task.parent.get_state_name(),
                       self.name),
                      extra=dict(data=task.attributes))
            log_overwrites(my_task.attributes, task.attributes)
            merge_dictionary(my_task.attributes, task.attributes, extend_lists)
        return super(Merge, self)._do_join(my_task)
Ejemplo n.º 7
0
 def _do_join(self, my_task):
     extend_lists = self.get_property("extend_lists", False)
     # Merge all inputs (in order)
     for input_spec in self.inputs:
         tasks = [
             task for task in my_task.workflow.task_tree
             if task.task_spec is input_spec
         ]
         for task in tasks:
             LOG.debug("Merging %s (%s) into %s" %
                       (task.get_name(), task.get_state_name(), self.name),
                       extra=dict(data=task.attributes))
             log_overwrites(my_task.attributes, task.attributes)
             merge_dictionary(my_task.attributes, task.attributes,
                              extend_lists)
     return super(Merge, self)._do_join(my_task)
Ejemplo n.º 8
0
 def my_code(self, my_task):
     fields = ['foo', 'bar']
     # Get fields from task and store them in spec
     for key in fields:
         if key in my_task.attributes:
             merge_dictionary(
                 self.properties,
                 dict(
                     collect={
                         'my_%s' % key: my_task.attributes[key],
                         key: my_task.attributes[key]
                     }))
     # Check if we have all fields
     collected = self.get_property('collect', {})
     if any(key for key in fields if key not in collected):
         return False
     my_task.set_attribute(**collected)
     return True
Ejemplo n.º 9
0
    def _try_fire(self, my_task, force=False):
        """Returns False when successfully fired, True otherwise"""

        # Deserialize async call if necessary
        if not 'async_call' in my_task.internal_data and \
                my_task.internal_data.get('task_id'):
            task_id = my_task.internal_data['task_id']
            my_task.internal_data['async_call'] = default_app.AsyncResult(
                task_id)
            my_task.internal_data['deserialized'] = True
            LOG.debug("Reanimate AsyncCall %s", task_id)

        # Make the call if not already done
        if 'async_call' not in my_task.internal_data:
            self._send_call(my_task)

        # Get call status (and manually refresh if deserialized)
        if my_task.internal_data.get('deserialized'):
            my_task.internal_data[
                'async_call'].state  # must manually refresh if deserialized
        async_call = my_task.internal_data['async_call']
        LOG.debug('AsyncCall.state: %s', async_call.state)
        if async_call.state == 'FAILURE':
            LOG.debug("Async Call for task '%s' failed: %s",
                      my_task.get_name(), async_call.result)
            my_task.internal_data['error'] = async_call.result
            if not isinstance(async_call.result, TaskError):
                raise TaskError(async_call.result)
            else:
                raise async_call.result

        elif async_call.state == 'PROGRESS':
            # currently the console outputs "WAITING" for this state
            # so we expect to see "WAITING, 50%"

            #try:
            progress = result["progress"]
            #except TypeError:
            #    # WTF, the task was clearly setting meta to dict:
            #    # progress = result["percentage"]
            #    # TypeError: string indices must be integers
            LOG.debug("Meta: %s", meta)
            LOG.debug("progress=%s, TryFire for '%s' returning False",
                      progress, my_task.get_name())
            my_task.progress = progress

        elif async_call.ready():
            result = async_call.result
            if isinstance(async_call.result, Exception):
                LOG.warn("Celery call %s failed: %s", self.call, result)
                my_task.data['error'] = result
                return False
            LOG.debug("Completed celery call %s with result=%s", self.call,
                      result)
            # Format result
            if self.call_result_key:
                data = data_i = {}
                path = self.call_result_key.split('.')
                for key in path[:-1]:
                    data_i[key] = {}
                    data_i = data_i[key]
                data_i[path[-1]] = result
            else:
                if isinstance(result, dict):
                    data = result
                else:
                    data = {'result': result}
            # Load formatted result into attributes
            if self.merge_results:
                merge_dictionary(my_task.data, data)
            else:
                my_task.set_data(**data)
            return True
        else:
            LOG.debug(
                "async_call.ready()=%s. TryFire for '%s' "
                "returning False", async_call.ready(), my_task.get_name())
            return False
Ejemplo n.º 10
0
    def try_fire(self, my_task, force=False):
        """Returns False when successfully fired, True otherwise"""

        # Deserialize async call if necessary
        if not hasattr(my_task, 'async_call') and \
                my_task._get_internal_attribute('task_id') is not None:
            task_id = my_task._get_internal_attribute('task_id')
            if 'eager_task_data' in my_task.internal_attributes:
                async_call = default_app.EagerResult(task_id)
                async_call.__dict__ = copy.copy(
                        my_task._get_internal_attribute('eager_task_data'))
                if async_call.state == 'FAILURE':
                    #Get the exception from the traceback
                    err_string = async_call.traceback.split("\n")[-2]
                    # Store it in the reult to be processed later
                    async_call._result = SpiffWorkflowCeleryException(
                        err_string)
                my_task.async_call = async_call
            else:
                my_task.async_call = default_app.AsyncResult(task_id)
            my_task.deserialized = True
            LOG.debug("Reanimate AsyncCall %s" % task_id)

        # Make the call if not already done
        if not hasattr(my_task, 'async_call'):
            self._send_call(my_task)

        # Get call status (and manually refresh if deserialized)
        if getattr(my_task, "deserialized", False):
            my_task.async_call.state  # must manually refresh if deserialized
        if my_task.async_call.state == 'FAILURE':
            LOG.debug("Async Call for task '%s' failed: %s" % (
                    my_task.get_name(), my_task.async_call.result))
            data = {}
            data['traceback'] = my_task.async_call.traceback
            data['info'] = Serializable(my_task.async_call.result)
            data['state'] = my_task.async_call.state
            my_task._set_internal_attribute(task_state=data)
            return False
        elif my_task.async_call.state == 'RETRY':
            data = {}
            data['traceback'] = my_task.async_call.traceback
            data['info'] = Serializable(my_task.async_call.result)
            data['state'] = my_task.async_call.state
            my_task._set_internal_attribute(task_state=data)
        elif my_task.async_call.ready():
            result = my_task.async_call.result
            if isinstance(result, Exception):
                LOG.warn("Celery call %s failed: %s" % (self.call, result))
                my_task._set_internal_attribute(error=Serializable(result))
                return False
            LOG.debug("Completed celery call %s with result=%s" % (self.call,
                    result))
            # Format result
            if self.result_key:
                data = {self.result_key: result}
            else:
                if isinstance(result, dict):
                    data = result
                else:
                    data = {'result': result}
            # Load formatted result into attributes
            if self.merge_results:
                merge_dictionary(my_task.attributes, data)
            else:
                my_task.set_attribute(**data)
            return True
        else:
            LOG.debug("async_call.ready()=%s. TryFire for '%s' "
                    "returning False" % (my_task.async_call.ready(),
                            my_task.get_name()))
            return False
Ejemplo n.º 11
0
    def try_fire(self, my_task, force=False):
        """Returns False when successfully fired, True otherwise"""

        # Deserialize async call if necessary
        if not hasattr(my_task, 'async_call') and \
                my_task._get_internal_attribute('task_id') is not None:
            task_id = my_task._get_internal_attribute('task_id')
            if 'eager_task_data' in my_task.internal_attributes:
                async_call = default_app.EagerResult(task_id)
                async_call.__dict__ = copy.copy(
                    my_task._get_internal_attribute('eager_task_data'))
                if async_call.state == 'FAILURE':
                    #Get the exception from the traceback
                    err_string = async_call.traceback.split("\n")[-2]
                    # Store it in the reult to be processed later
                    async_call._result = SpiffWorkflowCeleryException(
                        err_string)
                my_task.async_call = async_call
            else:
                my_task.async_call = default_app.AsyncResult(task_id)
            my_task.deserialized = True
            LOG.debug("Reanimate AsyncCall %s" % task_id)

        # Make the call if not already done
        if not hasattr(my_task, 'async_call'):
            self._send_call(my_task)

        # Get call status (and manually refresh if deserialized)
        if getattr(my_task, "deserialized", False):
            my_task.async_call.state  # must manually refresh if deserialized
        if my_task.async_call.state == 'FAILURE':
            LOG.debug("Async Call for task '%s' failed: %s" %
                      (my_task.get_name(), my_task.async_call.result))
            data = {}
            data['traceback'] = my_task.async_call.traceback
            data['info'] = Serializable(my_task.async_call.result)
            data['state'] = my_task.async_call.state
            my_task._set_internal_attribute(task_state=data)
            return False
        elif my_task.async_call.state == 'RETRY':
            data = {}
            data['traceback'] = my_task.async_call.traceback
            data['info'] = Serializable(my_task.async_call.result)
            data['state'] = my_task.async_call.state
            my_task._set_internal_attribute(task_state=data)
        elif my_task.async_call.ready():
            result = my_task.async_call.result
            if isinstance(result, Exception):
                LOG.warn("Celery call %s failed: %s" % (self.call, result))
                my_task._set_internal_attribute(error=Serializable(result))
                return False
            LOG.debug("Completed celery call %s with result=%s" %
                      (self.call, result))
            # Format result
            if self.result_key:
                data = {self.result_key: result}
            else:
                if isinstance(result, dict):
                    data = result
                else:
                    data = {'result': result}
            # Load formatted result into attributes
            if self.merge_results:
                merge_dictionary(my_task.attributes, data)
            else:
                my_task.set_attribute(**data)
            return True
        else:
            LOG.debug("async_call.ready()=%s. TryFire for '%s' "
                      "returning False" %
                      (my_task.async_call.ready(), my_task.get_name()))
            return False
Ejemplo n.º 12
0
    def _try_fire(self, my_task, force=False):
        """Returns False when successfully fired, True otherwise"""

        # Deserialize async call if necessary
        if not 'async_call' in my_task.internal_data and \
                my_task.internal_data.get('task_id'):
            task_id = my_task.internal_data['task_id']
            my_task.internal_data['async_call'] = default_app.AsyncResult(task_id)
            my_task.internal_data['deserialized'] = True
            LOG.debug("Reanimate AsyncCall %s", task_id)

        # Make the call if not already done
        if 'async_call' not in my_task.internal_data:
            self._send_call(my_task)

        # Get call status (and manually refresh if deserialized)
        if my_task.internal_data.get('deserialized'):
            my_task.internal_data['async_call'].state  # must manually refresh if deserialized
        async_call = my_task.internal_data['async_call']
        LOG.debug('AsyncCall.state: %s', async_call.state)
        if async_call.state == 'FAILURE':
            LOG.debug("Async Call for task '%s' failed: %s", 
                    my_task.get_name(), async_call.result)
            my_task.internal_data['error'] = async_call.result
            if not isinstance(async_call.result, TaskError):
                raise TaskError(async_call.result)
            else:
                raise async_call.result

        elif async_call.state == 'PROGRESS':
            # currently the console outputs "WAITING" for this state
            # so we expect to see "WAITING, 50%"

            #try:
            progress = result["progress"]
            #except TypeError:
            #    # WTF, the task was clearly setting meta to dict:
            #    # progress = result["percentage"]
            #    # TypeError: string indices must be integers
            LOG.debug("Meta: %s", meta)
            LOG.debug("progress=%s, TryFire for '%s' returning False", progress, my_task.get_name())
            my_task.progress = progress

        elif async_call.ready():
            result = async_call.result
            if isinstance(async_call.result, Exception):
                LOG.warn("Celery call %s failed: %s", self.call, result)
                my_task.data['error'] = result
                return False
            LOG.debug("Completed celery call %s with result=%s", self.call,
                    result)
            # Format result
            if self.call_result_key:
                data = data_i = {}
                path = self.call_result_key.split('.')
                for key in path[:-1]:
                    data_i[key] = {}
                    data_i = data_i[key]
                data_i[path[-1]] = result
            else:
                if isinstance(result, dict):
                    data = result
                else:
                    data = {'result': result}
            # Load formatted result into attributes
            if self.merge_results:
                merge_dictionary(my_task.data, data)
            else:
                my_task.set_data(**data)
            return True
        else:
            LOG.debug("async_call.ready()=%s. TryFire for '%s' "
                    "returning False", async_call.ready(), my_task.get_name())
            return False