static async Task <string> Wait(string arn, TimeSpan wait, TimeSpan max) { var startTime = DateTimeOffset.Now; var client = new Amazon.StepFunctions.AmazonStepFunctionsClient(); bool shouldContinue = true; while (shouldContinue) { var result = await client.DescribeExecutionAsync(new DescribeExecutionRequest() { ExecutionArn = arn }); Console.WriteLine(result.Status.Value); if (result.Status == ExecutionStatus.SUCCEEDED) { Console.WriteLine("::set-output name=statemachineexecutionarn::" + result.Output); Console.WriteLine("::set-output name=result::" + result.Output); return(result.Output); } else if (result.Status == ExecutionStatus.RUNNING) { if (DateTimeOffset.Now > startTime.Add(max)) { //timed out throw new Exception("The state machine retried the max time"); } await Task.Delay(wait); } else if (result.Status == ExecutionStatus.ABORTED) { throw new Exception("State machine aborted "); } else if (result.Status == ExecutionStatus.FAILED) { var history = await client.GetExecutionHistoryAsync(new GetExecutionHistoryRequest() { ExecutionArn = arn, ReverseOrder = true, IncludeExecutionData = true }); var reason = history.Events .FirstOrDefault(x => x.ExecutionFailedEventDetails != null)?.ExecutionFailedEventDetails .Cause ?? "Unknown"; throw new Exception("State machine failed. Reason: " + reason); } else if (result.Status == ExecutionStatus.TIMED_OUT) { throw new Exception("State machine timed out "); } } return(string.Empty); }
static Task <StartExecutionResponse> Run(string arn, string name, string input) { var client = new Amazon.StepFunctions.AmazonStepFunctionsClient(); return(client.StartExecutionAsync(new StartExecutionRequest() { Input = input, Name = name, StateMachineArn = arn } )); }