Ejemplo n.º 1
0
def start_state_machine(machine_name,
                        initial_context,
                        correlation_id=None,
                        current_state=STATE.PSEUDO_INIT,
                        current_event=STATE.PSEUDO_INIT):
    """
    Insert a AWS Kinesis message that will kick off a state machine.

    :param machine_name: a str name for the machine to start.
    :param initial_context: a dict of initial data for the state machine.
    :param correlation_id: the guid for the fsm, or None if the system should
      define it automatically.
    :param current_state: the state to start the machine in.
    :param current_event: the event to start the machine with.

    """
    correlation_id = correlation_id or uuid.uuid4().hex
    system_context = {
        SYSTEM_CONTEXT.STARTED_AT: int(time.time()),
        SYSTEM_CONTEXT.MACHINE_NAME: machine_name,
        SYSTEM_CONTEXT.CURRENT_STATE: current_state,
        SYSTEM_CONTEXT.CURRENT_EVENT: current_event,
        SYSTEM_CONTEXT.STEPS: 0,
        SYSTEM_CONTEXT.RETRIES: 0,
        SYSTEM_CONTEXT.CORRELATION_ID: correlation_id,
    }
    payload = {
        PAYLOAD.VERSION: PAYLOAD.DEFAULT_VERSION,
        PAYLOAD.SYSTEM_CONTEXT: system_context,
        PAYLOAD.USER_CONTEXT: initial_context
    }
    send_next_event_for_dispatch(None, json.dumps(payload, sort_keys=True),
                                 correlation_id)
Ejemplo n.º 2
0
    def _send_next_event_for_dispatch(self, serialized, obj, recovering=False):
        """
        Send the next event for dispatch to the primary/secondary stream systems.

        :param serialized: a str serialized message.
        :param obj: a dict.
        :param recovering: indicate this dispatch is in an error path
        """
        for primary in [True, False]:
            try:
                return send_next_event_for_dispatch(
                    self,
                    serialized,
                    self.correlation_id,
                    delay=obj.get(OBJ.DELAY, 0),
                    primary=primary,
                    recovering=recovering
                )
            except ClientError:
                self._queue_error(
                    ERRORS.ERROR,
                    'Unable to send next event (primary=%s).' % primary,
                    exc_info=True)
                if not primary and recovering:
                    raise
Ejemplo n.º 3
0
def start_state_machine(machine_name,
                        initial_context,
                        correlation_id=None,
                        current_state=STATE.PSEUDO_INIT,
                        current_event=STATE.PSEUDO_INIT,
                        additional_delay_seconds=0):
    """
    Insert an AWS SQS/Kinesis/SNS/DynamoDB/... message that will kick off a state machine.

    :param machine_name: a str name for the machine to start.
    :param initial_context: a dict of initial data for the state machine.
    :param correlation_id: the guid for the fsm, or None if the system should
      define it automatically.
    :param current_state: the state to start the machine in.
    :param current_event: the event to start the machine with.
    :param additional_delay_seconds: number of seconds to insert between state transitions
      (for streams that support delay)
    """
    correlation_id = correlation_id or uuid.uuid4().hex
    system_context = {
        SYSTEM_CONTEXT.STARTED_AT: int(time.time()),
        SYSTEM_CONTEXT.MACHINE_NAME: machine_name,
        SYSTEM_CONTEXT.CURRENT_STATE: current_state,
        SYSTEM_CONTEXT.CURRENT_EVENT: current_event,
        SYSTEM_CONTEXT.STEPS: 0,
        SYSTEM_CONTEXT.RETRIES: 0,
        SYSTEM_CONTEXT.CORRELATION_ID: correlation_id,
        SYSTEM_CONTEXT.ADDITIONAL_DELAY_SECONDS: additional_delay_seconds
    }
    payload = {
        PAYLOAD.VERSION: PAYLOAD.DEFAULT_VERSION,
        PAYLOAD.SYSTEM_CONTEXT: system_context,
        PAYLOAD.USER_CONTEXT: initial_context
    }
    send_next_event_for_dispatch(
        None, json.dumps(payload, **json_dumps_additional_kwargs()),
        correlation_id)
Ejemplo n.º 4
0
    def _send_next_event_for_dispatch(self, serialized, obj):
        """
        Send the next event for dispatch to the primary/secondary stream systems.

        :param serialized: a str serialized message.
        :param obj: a dict.
        """
        for primary in [True, False]:
            try:
                return send_next_event_for_dispatch(self,
                                                    serialized,
                                                    self.correlation_id,
                                                    delay=obj.get(
                                                        OBJ.DELAY, 0),
                                                    primary=primary)
            except ClientError:
                self._queue_error(ERRORS.ERROR,
                                  'Unable to send next event.',
                                  exc_info=True)
                if not primary:
                    raise
Ejemplo n.º 5
0
                                        volumes=DOCKER_SOCK_PATH + ':' +
                                        DOCKER_SOCK_PATH)
    client.start(container=container)
    stdout = client.logs(container, stdout=True, stream=True)
    for line in stdout:
        sys.stdout.write(line)
    stderr = client.logs(container, stderr=True, stream=True)
    for line in stderr:
        sys.stderr.write(line)
    return_code = client.wait(container)

except Exception:
    logging.exception('')
    raise

finally:

    if not environment:
        sys.stderr.write(FATAL_ENVIRONMENT_ERROR)
        sys.exit(1)

    # FSM_CONTEXT is the environment variable used by aws_lambda_fsm.utils.ECSTaskEntryAction
    event = DONE_EVENT if return_code == 0 else FAIL_EVENT
    payload_encoded = environment[ENVIRONMENT.FSM_CONTEXT]
    payload = json.loads(base64.b64decode(payload_encoded))
    payload[PAYLOAD.SYSTEM_CONTEXT][SYSTEM_CONTEXT.CURRENT_EVENT] = event
    serialized = json.dumps(payload)
    send_next_event_for_dispatch(
        None, serialized,
        payload[PAYLOAD.SYSTEM_CONTEXT][SYSTEM_CONTEXT.CORRELATION_ID])