Esempio n. 1
0
 def list_markers(self, all=False):
     if all:
         return [
             Marker(m['name'], json_loads_or_raw(m['details']))
             for ml in self._history.markers.values() for m in ml
         ]
     rc = []
     for ml in self._history.markers.values():
         m = ml[-1]
         if m['state'] == 'recorded':
             rc.append(Marker(m['name'], json_loads_or_raw(m['details'])))
     return rc
Esempio n. 2
0
    def _get_future_from_child_workflow_event(self, event):
        """Maps a child workflow event to a Future with the corresponding
        state.

        :param event: child workflow event
        :type  event: dict[str, Any]
        """
        future = futures.Future()
        state = event['state']

        if state == 'start_initiated':
            pass  # future._state = futures.PENDING
        elif state == 'start_failed':
            if event['cause'] == 'WORKFLOW_TYPE_DOES_NOT_EXIST':
                workflow_type = swf.models.WorkflowType(
                    self.domain,
                    name=event['name'],
                    version=event['version'],
                )
                logger.info('Creating workflow type {} in domain {}'.format(
                    workflow_type.name,
                    self.domain.name,
                ))
                try:
                    workflow_type.save()
                except swf.exceptions.AlreadyExistsError:
                    # Could have be created by a concurrent workflow execution.
                    pass
                return None
            future.set_exception(
                exceptions.TaskFailed(
                    name=event['id'],
                    reason=event['cause'],
                    details=event.get('details'),
                ))
        elif state == 'started':
            future.set_running()
        elif state == 'completed':
            future.set_finished(json_loads_or_raw(event['result']))
        elif state == 'failed':
            future.set_exception(
                exceptions.TaskFailed(
                    name=event['id'],
                    reason=event['reason'],
                    details=event.get('details'),
                ))
        elif state == 'timed_out':
            future.set_exception(
                exceptions.TimeoutError(
                    event['timeout_type'],
                    None,
                ))
        elif state == 'canceled':
            future.set_exception(
                exceptions.TaskCanceled(event.get('details'), ))
        elif state == 'terminated':
            future.set_exception(exceptions.TaskTerminated())

        return future
Esempio n. 3
0
 def task_error(self):
     if self._task_error is None:
         from simpleflow.exceptions import TaskFailed
         from simpleflow.utils import json_loads_or_raw
         self._task_error = ()  # falsy value different from None
         if isinstance(self.exception, TaskFailed) and self.exception.details:
             details = json_loads_or_raw(self.exception.details)
             if isinstance(details, dict) and 'error' in details:
                 self._task_error = details['error']
     return self._task_error
Esempio n. 4
0
 def task_error(self):
     if self._task_error is None:
         from simpleflow.exceptions import TaskFailed
         from simpleflow.utils import json_loads_or_raw
         self._task_error = ()  # falsy value different from None
         if isinstance(self.exception, TaskFailed) and self.exception.details:
             details = json_loads_or_raw(self.exception.details)
             if isinstance(details, dict) and 'error' in details:
                 self._task_error = details['error']
     return self._task_error
Esempio n. 5
0
    def _get_future_from_activity_event(self, event):
        """Maps an activity event to a Future with the corresponding state.

        :param event: activity event
        :type  event: dict[str, Any]
        :rtype: futures.Future

        """
        future = futures.Future()  # state is PENDING.
        state = event['state']

        if state == 'scheduled':
            pass
        elif state == 'schedule_failed':
            if event['cause'] == 'ACTIVITY_TYPE_DOES_NOT_EXIST':
                activity_type = swf.models.ActivityType(
                    self.domain,
                    name=event['activity_type']['name'],
                    version=event['activity_type']['version'])
                logger.info('creating activity type {} in domain {}'.format(
                    activity_type.name,
                    self.domain.name))
                try:
                    activity_type.save()
                except swf.exceptions.AlreadyExistsError:
                    logger.info(
                        'oops: Activity type {} in domain {} already exists, creation failed, continuing...'.format(
                            activity_type.name,
                            self.domain.name))
                return None
            logger.info('failed to schedule {}: {}'.format(
                event['activity_type']['name'],
                event['cause'],
            ))
            return None
        elif state == 'started':
            future.set_running()
        elif state == 'completed':
            result = event['result']
            future.set_finished(json_loads_or_raw(result))
        elif state == 'canceled':
            future.set_cancelled()
        elif state == 'failed':
            exception = exceptions.TaskFailed(
                name=event['id'],
                reason=event['reason'],
                details=event.get('details'))
            future.set_exception(exception)
        elif state == 'timed_out':
            exception = exceptions.TimeoutError(
                event['timeout_type'],
                event['timeout_value'])
            future.set_exception(exception)

        return future
Esempio n. 6
0
    def _cache_error(self):
        from simpleflow.exceptions import TaskFailed
        from simpleflow.utils import import_from_module, json_loads_or_raw

        self._task_error = ""  # falsy value different from None
        if isinstance(self.exception, TaskFailed) and self.exception.details:
            details = json_loads_or_raw(self.exception.details)
            if isinstance(details, dict):
                if "error" in details:
                    self._task_error = details["error"]
                if "error_type" in details:
                    try:
                        self._task_error_type = import_from_module(
                            details["error_type"])
                    except Exception:
                        pass
Esempio n. 7
0
def decode(content, parse_json=True, use_proxy=True):
    if content is None:
        return content
    if content.startswith(constants.JUMBO_FIELDS_PREFIX):

        def unwrap():
            location, _size = content.split()
            value = _pull_jumbo_field(location)
            if parse_json:
                return json_loads_or_raw(value)
            return value

        if use_proxy:
            return lazy_object_proxy.Proxy(unwrap)
        return unwrap()

    if parse_json:
        return json_loads_or_raw(content)

    return content
Esempio n. 8
0
def decode(content, parse_json=True, use_proxy=True):
    if content is None:
        return content
    if content.startswith(constants.JUMBO_FIELDS_PREFIX):

        def unwrap():
            location, _size = content.split()
            value = _pull_jumbo_field(location)
            if parse_json:
                return json_loads_or_raw(value)
            return value

        if use_proxy:
            return lazy_object_proxy.Proxy(unwrap)
        return unwrap()

    if parse_json:
        return json_loads_or_raw(content)

    return content
Esempio n. 9
0
 def unwrap():
     location, _size = content.split()
     value = _pull_jumbo_field(location)
     if parse_json:
         return json_loads_or_raw(value)
     return value
Esempio n. 10
0
 def unwrap():
     location, _size = content.split()
     value = _pull_jumbo_field(location)
     if parse_json:
         return json_loads_or_raw(value)
     return value