Starting Workflow and Activity Workers

You can start workflow and activity workers easily using the aws-flow-ruby utility, or in code.

About Workers#

Workflows and Activities are run by workflow and activity workers. A worker is responsible for polling for tasks from Amazon SWF on a task list, then starting the appropriate workflow or activity based on the message in the task event.

The AWS Flow Framework for Ruby takes care of managing the workers for you. All you need to do is instantiate and start the workers, passing optional configuration data to control how the workers operate.

You can start the workers in your Ruby code, or start them by using the aws-flow-ruby utility. If you are planning on deploying fleets of workers using AWS OpsWorks, you should use aws-flow-ruby. For more information about using Amazon SWF with AWS OpsWorks, see Tutorial: Hello AWS OpsWorks!.

Using aws-flow-ruby to Start Workers#

You can begin both activity and workflow workers by providing a small JSON configuration file to the aws-flow-ruby utility, also referred to as the runner. Here is an example configuration, worker.json, that configures a small fleet of workers for the Booking sample:

{
  "domain":
    {
      "name": "Booking",
      "retention_in_days": 10
    },
  "workflow_workers": [
    {
      "number_of_workers": 5,
      "task_list": "booking_tasklist"
    }
  ],
  "activity_workers": [
    {
      "number_of_workers": 5,
      "number_of_forks_per_worker": 10,
      "task_list": "booking_activity_tasklist"
    }
  ]
}

The runner interprets this file and creates a set of workers as you specify, using the activities and workflows that are defined in activities.rb and workflows.rb files within the flow directory where the configuration file exists. See aws-flow-ruby for a complete description of how to configure the runner.

Starting Activity Workers in Code#

To start activity workers in your code:

  1. Create a new ActivityWorker object, providing it with a AWS::SimpleWorkflow::Client object, the domain, the task list name to poll for activity tasks on, and an Activities-based class to use to access its activity implementations. For example:

    require 'aws/decider'
    
    swf = AWS::SimpleWorkflow.new
    
    activity_worker = AWS::Flow::ActivityWorker.new(
      swf.client, "HelloWorldDomain", "hello_world_activity_tasks",
      HelloWorldActivities)
    
  2. Call start on the ActivityWorker. You can set whether or not the activities should be registered first by using the should_register parameter.

    activity_worker.start(true)
    

Starting Workflow Workers in Code#

To start workflow workers in your code:

  1. Create a new WorkflowWorker object, providing it with similar options as with an ActivityWorker: a AWS::SimpleWorkflow::Client object, the domain, the task list name to poll for workflow tasks on, and a Workflows-based class to use to access its activity implementations. For example:

    require 'aws/decider'
    
    swf = AWS::SimpleWorkflow.new
    
    workflow_worker = AWS::Flow::WorkflowWorker.new(
      swf.client, "HelloWorldDomain", "hello_world_decision_tasks",
      HelloWorldWorkflow)
    
  2. Call start on the WorkflowWorker. You can set whether or not the workflows should be registered first by using the should_register parameter.

    workflow_worker.start(true)
    

For More Information#

For more information about implementing workers, refer to the following topics and resources:

  • aws-flow-ruby – Provides information about how to set up and spawn workers for your activities and workflows with a simple configuration file and the aws-flow-ruby utility.
  • Tutorial: Hello AWS OpsWorks! – A tutorial that leads you through the process of spawning SWF worker fleets with AWS OpsWorks.
  • awslabs/aws-flow-ruby-samples – A GitHub repository with examples and recipes that provide code examples of activity and workflow implementations using the AWS Flow Framework for Ruby.