public static Task <JToken> ExecuteAsync(this InjectDataAction action, StateMachineContext context, JToken input) { action.CheckArgNull(nameof(action)); context.CheckArgNull(nameof(context)); input.CheckArgNull(nameof(input)); return(Task.FromResult(action.Expression.EvalExpr(input, context) ?? JValue.CreateNull())); }
public static async Task <JToken> ExecuteAsync(this ModelAction action, StateMachineContext context, JToken input) { action.CheckArgNull(nameof(action)); context.CheckArgNull(nameof(context)); input.CheckArgNull(nameof(input)); await context.RecordObservableActionAsync(ObservableAction.BeforeAction, () => new Dictionary <string, object> { { "actionName", action.Name }, { "actionType", action.GetType().FullName } }); Func <StateMachineContext, JToken, Task <JToken> > executeFunc = action switch { InjectDataAction inject => inject.ExecuteAsync, ParallelAction parallel => parallel.ExecuteAsync, SequenceAction sequence => sequence.ExecuteAsync, SendEventAction send => send.ExecuteAsync, DelayAction delay => delay.ExecuteAsync, InvokeSubflowAction subflow => subflow.ExecuteAsync, InvokeFunctionAction function => function.ExecuteAsync, ForEachAction forEach => forEach.ExecuteAsync, _ => throw new NotImplementedException("Action behavior not implemented: " + action.GetType().FullName) }; Debug.Assert(executeFunc != null); var attempts = 0; DateTimeOffset?start = null; while (true) { try { var result = await executeFunc(context, input); await context.RecordObservableActionAsync(ObservableAction.AfterAction, () => new Dictionary <string, object> { { "actionName", action.Name }, { "actionType", action.GetType().FullName } }); return(result); } catch (Exception ex) { if (action.TryHandleError(JObject.FromObject(ex), context, out RetryPolicy? retryPolicy)) { if (retryPolicy == null) { return(JValue.CreateNull()); } else { TimeSpan elapsedDelay; if (start == null) { start = DateTimeOffset.UtcNow; elapsedDelay = TimeSpan.Zero; } else { elapsedDelay = DateTimeOffset.UtcNow.Subtract(start.Value); } var retry = await retryPolicy.ShouldRetryAsync(context, ++attempts, elapsedDelay); if (retry) { continue; } } } throw; } } }