AWS SWF

Concepts

  • workflow. A workflow is a set of activities that carry out some objective, together with logic that coordinates the activities.
  • Each workflow runs in an AWS resource called a domain, which controls the workflow's scope.
  • Workflow history The progress of every workflow execution is recorded in its workflow history, which Amazon SWF maintains. The workflow history contains every event that causes the execution state of the workflow execution to change, such as scheduled and completed activities, task timeouts, and signals.
  • Each event has the following:
    • A type, such as WorkflowExecutionStarted or ActivityTaskCompleted
    • A timestamp in Unix time format
    • An ID that uniquely identifies the event
  • When designing an Amazon SWF workflow, you precisely define each of the required activities. You then register each activity with Amazon SWF as an activity type. When you register the activity, you provide information such as a name and version, and some timeout values based on how long you expect the activity to take.
  • An activity worker is a program that receives activity tasks, performs them, and provides results back.
  • activity task that represents one invocation of an activity
  • The coordination logic in a workflow is contained in a software program called a decider. The decider schedules activity tasks, provides input data to the activity workers, processes events that arrive while the workflow is in progress, and ultimately ends (or closes) the workflow when the objective has been completed.
  • The role of the Amazon SWF service is to function as a reliable central hub through which data is exchanged between the decider, the activity workers, and other relevant entities such as the person administering the workflow. Amazon SWF also maintains the state of each workflow execution, which saves your application from having to store the state in a durable way.

Key concepts list

  • Actor

    • activity worker <-> perform task
    • decider <-> make decision
    • workflow starter <-> start workflow
    • AWS SWF service <-> central hub, maintain workflow history
  • what we need to implement:

    • decider <- contain workflow logic
    • activity worker <- execute task
    • workflow starter
  • Event
    Everything that change the status of workflow is called an event. Workflow History will contains all the event that happened. Each event has a event type, which is predefined by SWF.

Each type of event has a distinct set of descriptive attributes that are appropriate to that type. For example, the ActivityTaskCompleted event has attributes that contain the IDs for the events that correspond to the time that the activity task was scheduled and when it was started, as well as an attribute that holds result data.

List of event type:
http://docs.aws.amazon.com/amazonswf/latest/apireference/API_HistoryEvent.html

Note: event type is defined by SWF. However, workflow type and activity type is defined by us.

  • task
    Amazon SWF interacts with activity workers and deciders by providing them with work assignments known as tasks.

  • task list
    You can think of task lists as similar to dynamic queues. When a task is scheduled in Amazon SWF, you can specify a queue (task list) to put it in. Similarly, when you poll Amazon SWF for a task you say which queue (task list) to get the task from.

    • decision task list
    • activity task list

** initially, workflow starter will initialize decision task list. After that, it is decider's responsibility to put task into both lists. **

  • easy to confused:
    • task
    • event
    • activity

Check this webpage, contains example of task, event, action and activity:
http://docs.aws.amazon.com/amazonswf/latest/developerguide/swf-dev-workflow-exec-lifecycle.html

Screenshot 2016-07-23 20.05.44.png
Screenshot 2016-07-23 20.05.54.png

Data exchange

Because Amazon SWF maintains the complete execution state of each workflow execution, including the inputs and the results of tasks, all actors can be stateless. As a result, workflow processing is highly scalable. As the load on your system grows, you can simply add more actors to increase capacity.

If a task is available on the specified task list, Amazon SWF returns it immediately in the response. If no task is available, Amazon SWF holds the TCP connection open for up to 60 seconds so that, if a task becomes available during that time, it can be returned in the same connection. If no task becomes available within 60 seconds, it returns an empty response and closes the connection. (An empty response is a Task structure in which the value of taskToken is an empty string.) If this happens, the decider or activity worker should poll again.

Workflow Execution

Bringing together the ideas discussed in the preceding sections, here is an overview of the steps to develop and run a workflow in Amazon SWF:

  • Write activity workers that implement the processing steps in your workflow.
  • Write a decider to implement the coordination logic of your workflow.
  • Register your activities and workflow with Amazon SWF.

You can do this step programmatically or by using the AWS Management Console.

  • Start your activity workers and decider.
    These actors can run on any computing device that can access an Amazon SWF endpoint. For example, you could use compute instances in the cloud, such as Amazon Elastic Compute Cloud (Amazon EC2); servers in your data center; or even a mobile device, to host a decider or activity worker. Once started, the decider and activity workers should start polling Amazon SWF for tasks.
  • Start one or more executions of your workflow.
  • View workflow executions using the AWS Management Console.

Developing an Activate Work

http://docs.aws.amazon.com/amazonswf/latest/developerguide/swf-dg-develop-activity.html

An activity worker provides the implementation of one or more activity types. An activity worker communicates with Amazon SWF to receive activity tasks and perform them. You can have a fleet of multiple activity workers performing activity tasks of the same activity type.

Amazon SWF makes an activity task available to activity workers when the decider schedules the activity task. When a decider schedules an activity task, it provides the data (which you determine) that the activity worker needs to perform the activity task. Amazon SWF inserts this data into the activity task before sending it to the activity worker.

Activity workers are managed by you. They can be written in any language. A worker can be run anywhere, as long as it can communicate with Amazon SWF through the API. Because Amazon SWF provides all the information needed to perform an activity task, all activity workers can be stateless. Statelessness enables your workflows to be highly scalable; to handle increased capacity requirements, simply add more activity workers.

This section explains how to implement an activity worker. The activity workers should repeatedly do the following.

  • Poll Amazon SWF for an activity task.
  • Begin performing the task.
  • Periodically report a heartbeat to Amazon SWF if the task is long-lived.
  • Report that the task completed or failed and return the results to Amazon SWF.

Developing Decider

http://docs.aws.amazon.com/amazonswf/latest/developerguide/swf-dg-dev-deciders.html

A decider is an implementation of the coordination logic of your workflow type that runs during the execution of your workflow. You can run multiple deciders for a single workflow type.

Because the execution state for a workflow execution is stored in its workflow history, deciders can be stateless. Amazon SWF maintains the workflow execution history and provides it to a decider with each decision task. This enables you to dynamically add and remove deciders as necessary, which makes the processing of your workflows highly scalable. As the load on your system grows, you simply add more deciders to handle the increased capacity. Note, however, that there can be only one decision task open at any time for a given workflow execution.

Every time a state change occurs for a workflow execution, Amazon SWF schedules a decision task. Each time a decider receives a decision task, it does the following:

  • Interprets the workflow execution history provided with the decision task
  • Applies the coordination logic based on the workflow execution history and makes decisions on what to do next. Each decision is represented by a Decision structure
  • Completes the decision task and provides a list of decisions to Amazon SWF.

This section describes how to develop a decider, which involves:

  • Programming your decider to poll for decision tasks
  • Programming your decider to interpret the workflow execution history and make decisions
  • Programming your decider to respond to a decision task.
  • The examples in this section show how you might program a decider for the e-commerce example workflow.

You can implement the decider in any language that you like and run it anywhere, as long as it can communicate with Amazon SWF through its service API.

Launch decider

Once launched, your deciders should start polling Amazon SWF for tasks. Until you start workflow executions and Amazon SWF schedules decision tasks, these polls will time out and get empty responses. An empty response is a Task structure in which the value of taskToken is an empty string. Your deciders should simply continue to poll.

Amazon SWF ensures that only one decision task can be active for a workflow execution at any time. This prevents issues such as conflicting decisions. Additionally, Amazon SWF ensures that a single decision task is assigned to a single decider, regardless of the number of deciders that are running.

In order for workflow executions to progress, one or more deciders must be running. You can launch as many deciders as you like. Amazon SWF supports multiple deciders polling on the same task list.


Important actions

swf = boto3.client('swf')
from botocore.exceptions import ClientError

Initialize

register_domain()

register_activity_type()

register_workflow_type()

  swf.register_workflow_type(
    domain=DOMAIN,
    name=WORKFLOW,
    version=VERSION,
    description="Test workflow",
    defaultExecutionStartToCloseTimeout="250",
    defaultTaskStartToCloseTimeout="NONE",
    defaultChildPolicy="TERMINATE",
    defaultTaskList={"name": TASKLIST}
  )

Action for workflow starter

start_workflow_execution()

response = swf.start_workflow_execution(
  domain=DOMAIN,
  workflowId='test-1001',
  workflowType={
    "name": WORKFLOW,
    "version": VERSION
  },
  taskList={
      'name': TASKLIST
  },
  input=''
)

Action for activity worker

record_activity_task_heartbeat()

poll_for_activity_task()

  task = swf.poll_for_activity_task(
    domain=DOMAIN,
    taskList={'name': TASKLIST},
    identity='worker-1')

Return value syntax:

{
    'taskToken': 'string',
    'activityId': 'string',
    'startedEventId': 123,
    'workflowExecution': {
        'workflowId': 'string',
        'runId': 'string'
    },
    'activityType': {
        'name': 'string',
        'version': 'string'
    },
    'input': 'string'
}

respond_activity_task_completed()

    swf.respond_activity_task_completed(
        taskToken=task['taskToken'],
        result='success'
    )

Action for decider

poll_for_decision_task()

response = client.poll_for_decision_task(
    domain='string',
    taskList={
        'name': 'string'
    },
    identity='string',
    nextPageToken='string',
    maximumPageSize=123,
    reverseOrder=True|False
)
  newTask = swf.poll_for_decision_task(
    domain=DOMAIN,
    taskList={'name': TASKLIST},
    identity='decider-1',
    reverseOrder=False)

  if 'taskToken' not in newTask:
    print "Poll timed out, no new task.  Repoll"

  elif 'events' in newTask:
    eventHistory = [evt for evt in newTask['events'] if not evt['eventType'].startswith('Decision')]
    lastEvent = eventHistory[-1]

return value syntax:

{
    'taskToken': 'string',
    'startedEventId': 123,
    'workflowExecution': {
        'workflowId': 'string',
        'runId': 'string'
    },
    'workflowType': {
        'name': 'string',
        'version': 'string'
    },
    'events': [
        {
            'eventTimestamp': datetime(2015, 1, 1),
            'eventType': 'WorkflowExecutionStarted', # or some other event type
            'eventId': 123,
            'workflowExecutionStartedEventAttributes': {
                # detail of event attribute
            }
- OR
            'activityTaskCompletedEventAttributes': {
                'result': 'string',
                'scheduledEventId': 123,
                'startedEventId': 123
            },
-
        },
    ],
    'nextPageToken': 'string',
    'previousStartedEventId': 123
}

respond_decision_task_completed()

syntax:

respond_decision_task_completed(
    taskToken='string',
    decisions=[
        {
            'decisionType': 'ScheduleActivityTask'|'RequestCancelActivityTask'|'CompleteWorkflowExecution'|'FailWorkflowExecution'|'CancelWorkflowExecution'|'ContinueAsNewWorkflowExecution'|'RecordMarker'|'StartTimer'|'CancelTimer'|'SignalExternalWorkflowExecution'|'RequestCancelExternalWorkflowExecution'|'StartChildWorkflowExecution'|'ScheduleLambdaFunction',
            'scheduleActivityTaskDecisionAttributes': {
                'activityType': {
                    'name': 'string',
                    'version': 'string'
                },
                'activityId': 'string',
                'control': 'string',
                'input': 'string',
                'scheduleToCloseTimeout': 'string',
                'taskList': {
                    'name': 'string'
                },
                'taskPriority': 'string',
                'scheduleToStartTimeout': 'string',
                'startToCloseTimeout': 'string',
                'heartbeatTimeout': 'string'
            }
      ],
      executionContext='string'
)
swf.respond_decision_task_completed(
        taskToken=newTask['taskToken'],
        decisions=[
          {
            'decisionType': 'ScheduleActivityTask', # pre-defined by SWF
            'scheduleActivityTaskDecisionAttributes': {
                'activityType':{
                    'name': TASKNAME,
                    'version': VERSION
                    },
                'activityId': 'activityid-' + str(uuid.uuid4()),
                'input': '',
                'scheduleToCloseTimeout': 'NONE',
                'scheduleToStartTimeout': 'NONE',
                'startToCloseTimeout': 'NONE',
                'heartbeatTimeout': 'NONE',
                'taskList': {'name': TASKLIST},
            }
          }
        ]
      )
swf.respond_decision_task_completed(
        taskToken=newTask['taskToken'],
        decisions=[
          {
            'decisionType': 'CompleteWorkflowExecution',
            'completeWorkflowExecutionDecisionAttributes': {
              'result': 'success'
            }
          }
        ]
      )

Collections of Attributes/ event type or whatever mentioned in the examples

  • defaultTaskScheduleToStartTimeoutSeconds specifies how long the tasks can be queued in the activities task list, and is set to 300 seconds (5 minutes).
  • defaultTaskStartToCloseTimeoutSeconds specifies the maximum time the activity can take to perform the task and is set to 10 seconds.

These timeouts ensure that the activity completes its task in a reasonable amount of time. If either timeout is exceeded, the framework generates an error and the workflow worker must decide how to handle the issue. For a discussion of how to handle such error, see Error Handling.


client.poll_for_decision_task(**kwargs)
Used by deciders to get a DecisionTask from the specified decision taskList . A decision task may be returned for any open workflow execution that is using the specified task list.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,588评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,456评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,146评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,387评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,481评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,510评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,522评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,296评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,745评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,039评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,202评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,901评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,538评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,165评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,415评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,081评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,085评论 2 352

推荐阅读更多精彩内容

  • 最近在开发微信服务号时碰到一个问题:阿里ECS的80端口被IIS占用,而微信服务号中使用JSSDK必须设置安全域名...
    _gmyboy阅读 5,394评论 0 3
  • 渴望能够走向远方 像风儿一样飞翔 天黑时 把所有的灯点亮 就这样去祈福幸福的时光 既然有意 就结伴而行 彼此照应 ...
    江城妖怪阅读 186评论 0 0
  • 跟年纪相仿的同事聊天,才发现,将近三十,大家都好焦虑。 眼睁睁看着周围的人一个个都成功了:在大公司上班的,混成了小...
    三三未立阅读 1,324评论 6 3
  • 常常有家长问我,孩子不喜欢读书咋办?怎样让孩子喜欢上读书?我有上、中、下三策。 上策。家长先喜欢读...
    河南张鹏阅读 243评论 0 2
  • 都说鱼的记忆只有60秒,如果换成人呢?那是不是就不会因为失去而不快乐了……
    落羽杉1阅读 177评论 0 0