Starting a Workflow Execution

Once you have defined your workflows and activities, have registered them with Amazon SWF and have started activity and workflow workers, your workflow is ready to run. However, until you start a workflow execution, Amazon SWF will not begin delivering tasks to your workers.

You have a number of options when starting a workflow with the AWS Flow Framework for Ruby:

  1. If you are running a single activity only, use AWS::Flow#start.
  2. If you are running multiple activities, use AWS::Flow#start_workflow.
  3. Use a workflow client to start a workflow execution with AWS::Flow::WorkflowClient#start_execution.

Since most workflows use multiple activities, this topic will focus on the final two methods. For information about using the start method to run a single activity, see Hello World and Running Activities.

Starting a Workflow Execution with start_workflow#

To start a workflow execution, the preferred method is to use start_workflow, which can be run simply by passing it the workflow name (class.method), input data, and a hash of StartWorkflowOptions:

require 'aws/decider'

AWS::Flow::start_workflow(
  "BookingWorkflow.make_booking",
  {
    request_id: "1234567890", customer_id: "1234567890",
    reserve_car: true, reserve_air: true,
  },
  {
    domain: "Booking",
    version: "1.0a"
  }
)

Important

The domain and version options are required.

You can also pass the workflow class as the first argument, and specify the method to run in the passed-in options. This is equivalent to the preceding method:

require 'aws/decider'

AWS::Flow::start_workflow(
  "BookingWorkflow",
  {
    request_id: "1234567890", customer_id: "1234567890",
    reserve_car: true, reserve_air: true,
  },
  {
    domain: "Booking",
    execution_method: "make_booking",
    version: "1.0a"
  }
)

Use the from_class option to use options set in the workflow class:

require 'aws/decider'
require_relative 'flow/workflows.rb'

AWS::Flow::start_workflow(
  "BookingWorkflow",
  {
    request_id: "1234567890", customer_id: "1234567890",
    reserve_car: true, reserve_air: true,
  },
  {
    domain: "Booking",
    from_class: "BookingWorkflow"
  }
)

Starting a Workflow Execution with a WorkflowClient#

In addition to using start_workflow, you can start a workflow execution by using a WorkflowClient object.

To start a workflow execution:

  1. Get a WorkflowClient object by calling workflow_client and providing it with an AWS::SimpleWorkflow object, a AWS::SimpleWorkflow::Domain object, and an optional block of StartWorkflowOptions:

    require 'aws/decider'
    
    swf = AWS::SimpleWorkflow.new
    domain = swf.domains['HelloWorldDomain']
    
    workflow_client = AWS::Flow::workflow_client(swf.client, domain) {
      { task_list: "hello_world_decision_tasks" }
    }
    
  2. Use the WorkflowClient#start_execution method, passing it optional input data for the workflow, and a hash of WorkflowOptions:

    workflow_input = "Amazon SWF"
    workflow_client.start_execution(
      workflow_input, { {  workflow_name: 'my_workflow_execution_name' } } )
    

Once you've started the workflow execution, your workflow and activity pollers will begin receiving events on their respective task lists.

For More Information#