Skip to content

Instantly share code, notes, and snippets.

@laur3d
Created September 6, 2022 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laur3d/4257751462dbd9fa0067b80e5af4b043 to your computer and use it in GitHub Desktop.
Save laur3d/4257751462dbd9fa0067b80e5af4b043 to your computer and use it in GitHub Desktop.
{
"Name": "Complex Calculation",
"Version": "1.8",
"Steps": [
{
"Action": "Add",
"Param": 5
},
{
"Action": "Add",
"Param": 1
}
...
]
}
[FunctionName("Add")]
public static async Task<int> Add([ActivityTrigger] (int a, int b) numbers)
{
return numbers.a + numbers.b;
}
[FunctionName("Subtract")]
public static async Task<int> Subtract([ActivityTrigger] (int a, int b) numbers)
{
return numbers.a - numbers.b;
}
[FunctionName("Multiply")]
public static async Task<int> Multiply([ActivityTrigger] (int a, int b) numbers)
{
return numbers.a * numbers.b;
}
[FunctionName("Divide")]
public static async Task<int> Divide([ActivityTrigger] (int a, int b) numbers)
{
return numbers.a / numbers.b;
}
[FunctionName("FlowMaker")]
public static async Task<int> Run([OrchestrationTrigger] IDurableOrchestrationContext context)
{
// imagine this comes from an api call or cosmos or something
var steps = new DynamicFlowContext
{
Steps = new List<DynamicStep<int>>
{
new DynamicStep<int>(ActionName.Add, 1),
new DynamicStep<int>(ActionName.Add, 1),
new DynamicStep<int>(ActionName.Subtract, 1),
new DynamicStep<int>(ActionName.Multiply, 10)
}
};
var result = await context.CallSubOrchestratorAsync<DynamicResult<int>>("DynamicOrchestrator", steps);
return result.Result;
}
public class DynamicStep<T>
{
public string Action { get; private set; }
public T param { get; private set; }
[JsonConstructor]
public DynamicStep(string action, T param)
{
Action = action;
this.param = param;
}
}
public class DynamicResult<T>
{
public T Result { get; set; }
}
public static class ActionName
{
public const string Add = "Add";
public const string Subtract = "Subtract";
public const string Multiply = "Multiply";
public const string Divide = "Divide";
}
[FunctionName("DynamicOrchestrator")]
public static async Task<DynamicResult<int>> RunInnerOrchestrator([OrchestrationTrigger] IDurableOrchestrationContext ctx)
{
var input = ctx.GetInput<DynamicFlowContext>();
int state = 0;
foreach (var step in input.Steps)
{
state = await ctx.CallActivityAsync<int>(step.Action, (state, step.param));
}
return new DynamicResult<int>
{
Result = state
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment