Skip to content

Parallel Activities example

theburningmonk edited this page Feb 12, 2013 · 1 revision

You can specify a number of activities to be run in parallel, the next stage of the workflow is triggered when all the parallel activities had completed successfully and their results are aggregated into a single input using the supplied reducer

open Amazon.SimpleWorkflow
open Amazon.SimpleWorkflow.Extensions

let echo str = printfn "%s" str; str
let greet me you = printfn "%s: hello %s!" me you; you
let bye me you = printfn "%s: good bye, %s!" me you; me

let echoActivity = Activity("echo", "echo", echo,
                            taskHeartbeatTimeout       = 60, 
                            taskScheduleToStartTimeout = 10,
                            taskStartToCloseTimeout    = 10, 
                            taskScheduleToCloseTimeout = 20)

let greetActivity = Activity("greet", "greet", greet "Yan",
                             taskHeartbeatTimeout       = 60, 
                             taskScheduleToStartTimeout = 10,
                             taskStartToCloseTimeout    = 10, 
                             taskScheduleToCloseTimeout = 20)

let byeActivity = Activity("bye", "bye", bye "Yan",
                           taskHeartbeatTimeout       = 60, 
                           taskScheduleToStartTimeout = 10,
                           taskStartToCloseTimeout    = 10, 
                           taskScheduleToCloseTimeout = 20)

// function to aggregate the results from all the parallel activities into a single result
let reducer (results : Dictionary<int, string>) =
    results |> Seq.map (fun kvp -> sprintf "Activity %d says [%s]" kvp.Key kvp.Value)
            |> (fun arr -> String.Join(";", arr))

let activities = 
    [| 
        echoActivity  :> ISchedulable
        greetActivity :> ISchedulable
        byeActivity   :> ISchedulable 
    |]

let parallelActivitiesWorkflow = 
    Workflow(domain = "theburningmonk.com", name = "parallel_activities", 
             description = "this workflow runs several activities in parallel", 
             version = "1")
    ++> (activities, reducer)
    ++> echoActivity

The ++> operator attaches an activity, child workflow or an array of activities/child workflows with associated reducer for their results 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)

parallelActivitiesWorkflow.Start(client)