forked from temporalio/samples-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDslWorkflow.workflow.cs
More file actions
52 lines (46 loc) · 1.86 KB
/
Copy pathDslWorkflow.workflow.cs
File metadata and controls
52 lines (46 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace TemporalioSamples.Dsl;
using Microsoft.Extensions.Logging;
using Temporalio.Workflows;
[Workflow]
public class DslWorkflow
{
private readonly Dictionary<string, object> variables;
[WorkflowInit]
public DslWorkflow(DslInput input) => variables = input.Variables;
[WorkflowRun]
public async Task<Dictionary<string, object>> RunAsync(DslInput input)
{
Workflow.Logger.LogInformation("Running DSL workflow");
await ExecuteStatementAsync(input.Root);
Workflow.Logger.LogInformation("DSL workflow completed");
return variables;
}
private async Task ExecuteStatementAsync(DslInput.Statement statement)
{
switch (statement)
{
case DslInput.ActivityStatement stmt:
// Invoke activity loading arguments from variables and optionally storing result as a variable
var result = await Workflow.ExecuteActivityAsync<object>(
stmt.Activity.Name,
stmt.Activity.Arguments.Select(arg => variables[arg]).ToArray(),
new ActivityOptions { StartToCloseTimeout = TimeSpan.FromMinutes(1) });
if (!string.IsNullOrEmpty(stmt.Activity.Result))
{
variables[stmt.Activity.Result] = result;
}
break;
case DslInput.SequenceStatement stmt:
foreach (var element in stmt.Sequence.Elements)
{
await ExecuteStatementAsync(element);
}
break;
case DslInput.ParallelStatement stmt:
await Workflow.WhenAllAsync(stmt.Parallel.Branches.Select(ExecuteStatementAsync));
break;
default:
throw new InvalidOperationException($"Unknown statement type: {statement.GetType().Name}");
}
}
}