Skip to content

Child Workflow example

theburningmonk edited this page Feb 2, 2013 · 1 revision

You can kick off a child workflow from the main workflow, passing result from the last activity to the child workflow as input, and taking the result of the child workflow as input to the next activity.

open Amazon.SimpleWorkflow
open Amazon.SimpleWorkflow.Extensions
open Amazon.SimpleWorkflow.Extensions.Model

// sings the first part of the song and return the second part as result
let sing name = printfn "Old %s had a farm" name; "EE-I-EE-I-O"

// unlike the main workflow, child workflows MUST specify the timeouts and child
// policy on the workflow definition itself
let childWorkflow = 
    Workflow(domain = "theburningmonk.com", name = "sing_along", 
             description = "child workflow to start a song", 
             version = "1",
             execStartToCloseTimeout = 60, 
             taskStartToCloseTimeout = 30,
             childPolicy = ChildPolicy.Terminate)
    ++> Activity("sing", "sing a song", sing,
                 taskHeartbeatTimeout       = 60, 
                 taskScheduleToStartTimeout = 10,
                 taskStartToCloseTimeout    = 10, 
                 taskScheduleToCloseTimeout = 20)

// this is the main workflow which
let withChildWorkflow =
    Workflow(domain = "theburningmonk.com", name = "with_child_workflow", 
             description = "workflow which starts a child workflow in the middle", 
             version = "1")
    ++> Activity("greet", "say hello", greet "MacDonald",
                 taskHeartbeatTimeout       = 60, 
                 taskScheduleToStartTimeout = 10,
                 taskStartToCloseTimeout    = 10, 
                 taskScheduleToCloseTimeout = 20)
    ++> Activity("bye", "say good bye", bye "MacDonald",
                 taskHeartbeatTimeout       = 60, 
                 taskScheduleToStartTimeout = 10,
                 taskStartToCloseTimeout    = 10, 
                 taskScheduleToCloseTimeout = 20)
    ++> childWorkflow
    ++> Activity("echo", "echo the last part of the song", echo,
                 taskHeartbeatTimeout       = 60, 
                 taskScheduleToStartTimeout = 10,
                 taskStartToCloseTimeout    = 10, 
                 taskScheduleToCloseTimeout = 20)

The ++> operator attaches an activity or child workflow to the workflow. To start the workflow, call the Start method with an instance of AmazonSimpleWorkflowClient, the domain, workflow and activities will be registered as necessary and auto-generated decision and activity workers will be started too.

let awsKey      = "PUT_YOUR_AWS_KEY_HERE"
let awsSecret   = "PUT_YOUR_AWS_SECRET_HERE"
let client = new AmazonSimpleWorkflowClient(awsKey, awsSecret)

withChildWorkflow.Start(client)

It's worth noting that, whilst there's no easy way to rerun an activity independently, it's a relatively straight forward task to rerun a child workflow from within the SWF Amazon management console.

Clone this wiki locally