Ejemplo n.º 1
0
        public void Decider()
        {
            int activityCount = 0;

            while (true)
            {
                Console.WriteLine("Decider: Polling for decision task ...");
                PollForDecisionTaskRequest request = new PollForDecisionTaskRequest()
                {
                    Domain   = domainName,
                    TaskList = new TaskList()
                    {
                        Name = deciderPollingKey
                    }
                };

                PollForDecisionTaskResponse response = swfClient.PollForDecisionTaskAsync(request).Result;
                if (response.DecisionTask.TaskToken == null)
                {
                    Console.WriteLine("Decider: NULL");
                    continue;
                }

                int completedActivityTaskCount = 0, totalActivityTaskCount = 0;
                foreach (HistoryEvent e in response.DecisionTask.Events)
                {
                    Console.WriteLine("Decider: EventType - " + e.EventType +
                                      ", EventId - " + e.EventId);
                    if (e.EventType == "ActivityTaskCompleted")
                    {
                        completedActivityTaskCount++;
                    }
                    if (e.EventType.Value.StartsWith("Activity"))
                    {
                        totalActivityTaskCount++;
                    }
                }
                Console.WriteLine(".... completedCount=" + completedActivityTaskCount);

                List <Decision> decisions = new List <Decision>();
                if (totalActivityTaskCount == 0)
                {
                    ScheduleActivity("Activity1", decisions);
                    activityCount = 4;
                }
                else if (completedActivityTaskCount == activityCount)
                {
                    Decision decision = new Decision()
                    {
                        DecisionType = DecisionType.CompleteWorkflowExecution,
                        CompleteWorkflowExecutionDecisionAttributes =
                            new CompleteWorkflowExecutionDecisionAttributes
                        {
                            Result = "{\"Result\":\"WF Complete!\"}"
                        }
                    };
                    decisions.Add(decision);

                    Console.WriteLine("Worflow Complete");
                }
                RespondDecisionTaskCompletedRequest respondDecisionTaskCompletedRequest =
                    new RespondDecisionTaskCompletedRequest()
                {
                    Decisions = decisions,
                    TaskToken = response.DecisionTask.TaskToken
                };
                swfClient.RespondDecisionTaskCompletedAsync(respondDecisionTaskCompletedRequest);
            }
        }
Ejemplo n.º 2
0
        protected override async Task <Workflow> GetCurrentContext()
        {
            Workflow                    workflow             = null;
            IList <Activity>            activityList         = new List <Activity>();
            PollForDecisionTaskResponse decisionTaskResponse = null;
            List <HistoryEvent>         historyEvents        = new List <HistoryEvent>();
            string taskToken     = null;
            string nextPageToken = null;

            do
            {
                var decisionTaskRequest = new PollForDecisionTaskRequest
                {
                    Domain   = Constants.LAMBDA_BIZ_DOMAIN,
                    Identity = Guid.NewGuid().ToString(),
                    TaskList = new TaskList
                    {
                        Name = Constants.LAMBDA_BIZ_TASK_LIST + _orchestrationId
                    },
                    NextPageToken = nextPageToken
                };

                decisionTaskResponse = await _amazonSimpleWorkflowClient.PollForDecisionTaskAsync(decisionTaskRequest);

                taskToken = decisionTaskResponse.DecisionTask.TaskToken;

                if (!string.IsNullOrEmpty(taskToken))
                {
                    var decisionTask = decisionTaskResponse.DecisionTask;

                    foreach (HistoryEvent historyEvent in decisionTask.Events)
                    {
                        historyEvents.Add(historyEvent);
                    }
                }
            }while (!string.IsNullOrEmpty(nextPageToken = decisionTaskResponse.DecisionTask.NextPageToken));

            if (historyEvents.Count > 0)
            {
                if (workflow == null)
                {
                    workflow = new Workflow();
                    workflow.ReferenceToken = taskToken;

                    if (_store != null)
                    {
                        if (!await _store.StoreExistsAsync())
                        {
                            await _store.CreateStoreAsync();
                        }
                    }
                }

                foreach (HistoryEvent historyEvent in historyEvents)
                {
                    if (historyEvent.EventType == EventType.WorkflowExecutionSignaled)
                    {
                        var activity = FindActivity(Model.ActivityType.Event, historyEvent.WorkflowExecutionSignaledEventAttributes.SignalName, activityList);

                        if (activity == null)
                        {
                            activity = new Activity
                            {
                                Result            = historyEvent.WorkflowExecutionSignaledEventAttributes.Input,
                                Status            = Status.SUCCEEDED,
                                ActivityType      = Model.ActivityType.Event,
                                Name              = historyEvent.WorkflowExecutionSignaledEventAttributes.SignalName,
                                ScheduledId       = historyEvent.WorkflowExecutionSignaledEventAttributes.SignalName,
                                UniqueId          = historyEvent.WorkflowExecutionSignaledEventAttributes.SignalName,
                                SucceededDateTime = historyEvent.EventTimestamp
                            };

                            activityList.Add(activity);
                        }
                    }
                    if (historyEvent.EventType == EventType.WorkflowExecutionStarted)
                    {
                        workflow.Status          = Status.STARTED;
                        workflow.OrchestrationId =
                            historyEvent.WorkflowExecutionStartedEventAttributes.TaskList.Name.Replace(Constants.LAMBDA_BIZ_TASK_LIST, string.Empty);
                        workflow.StartedDateTime = historyEvent.EventTimestamp;
                    }
                    if (historyEvent.EventType == EventType.WorkflowExecutionFailed)
                    {
                        workflow.Status         = Status.FAILED;
                        workflow.FailedDateTime = historyEvent.EventTimestamp;
                    }
                    if (historyEvent.EventType == EventType.WorkflowExecutionCompleted)
                    {
                        workflow.Status            = Status.SUCCEEDED;
                        workflow.SucceededDateTime = historyEvent.EventTimestamp;
                    }
                    if (historyEvent.EventType == EventType.LambdaFunctionScheduled)
                    {
                        var activity = FindActivity(Model.ActivityType.Task, historyEvent.LambdaFunctionScheduledEventAttributes.Id.ToString(), activityList);

                        if (activity == null)
                        {
                            activity = new Activity
                            {
                                ActivityType      = Model.ActivityType.Task,
                                Name              = historyEvent.LambdaFunctionScheduledEventAttributes.Name,
                                ScheduledId       = historyEvent.EventId.ToString(),
                                UniqueId          = historyEvent.LambdaFunctionScheduledEventAttributes.Control,
                                Status            = Status.SCHEDULED,
                                ScheduledDateTime = historyEvent.EventTimestamp
                            };

                            activityList.Add(activity);
                        }
                    }
                    if (historyEvent.EventType == EventType.StartLambdaFunctionFailed)
                    {
                        var activity = FindActivity(Model.ActivityType.Task, historyEvent.StartLambdaFunctionFailedEventAttributes.ScheduledEventId.ToString(), activityList);
                        activity.Result         = historyEvent.StartLambdaFunctionFailedEventAttributes.Message;
                        activity.FailureDetails = historyEvent.StartLambdaFunctionFailedEventAttributes.Cause;
                        activity.Status         = Status.FAILED;
                        activity.FailedDateTime = historyEvent.EventTimestamp;
                    }
                    if (historyEvent.EventType == EventType.LambdaFunctionStarted)
                    {
                        var activity = FindActivity(Model.ActivityType.Task, historyEvent.LambdaFunctionStartedEventAttributes.ScheduledEventId.ToString(), activityList);

                        activity.Status          = Status.STARTED;
                        activity.StartedDateTime = historyEvent.EventTimestamp;
                    }
                    if (historyEvent.EventType == EventType.LambdaFunctionCompleted)
                    {
                        var activity = FindActivity(Model.ActivityType.Task, historyEvent.LambdaFunctionCompletedEventAttributes.ScheduledEventId.ToString(), activityList);
                        activity.Result            = historyEvent.LambdaFunctionCompletedEventAttributes.Result;
                        activity.Status            = Status.SUCCEEDED;
                        activity.SucceededDateTime = historyEvent.EventTimestamp;
                    }
                    if (historyEvent.EventType == EventType.LambdaFunctionFailed)
                    {
                        var activity = FindActivity(Model.ActivityType.Task, historyEvent.LambdaFunctionFailedEventAttributes.ScheduledEventId.ToString(), activityList);
                        activity.Result         = historyEvent.LambdaFunctionFailedEventAttributes.Details;
                        activity.FailureDetails = historyEvent.LambdaFunctionFailedEventAttributes.Reason;
                        activity.Status         = Status.FAILED;
                        activity.FailedDateTime = historyEvent.EventTimestamp;
                    }
                    if (historyEvent.EventType == EventType.LambdaFunctionTimedOut)
                    {
                        var activity = FindActivity(Model.ActivityType.Task, historyEvent.LambdaFunctionTimedOutEventAttributes.ScheduledEventId.ToString(), activityList);
                        activity.Status          = Status.TIMEOUT;
                        activity.TmedOutDateTime = historyEvent.EventTimestamp;
                    }

                    if (historyEvent.EventType == EventType.TimerStarted)
                    {
                        var activity = new Activity
                        {
                            ActivityType    = Model.ActivityType.Timer,
                            Name            = historyEvent.TimerStartedEventAttributes.TimerId,
                            ScheduledId     = historyEvent.EventId.ToString(),
                            UniqueId        = historyEvent.TimerStartedEventAttributes.TimerId,
                            Status          = Status.STARTED,
                            StartedDateTime = historyEvent.EventTimestamp
                        };

                        activityList.Add(activity);
                    }
                    if (historyEvent.EventType == EventType.TimerFired)
                    {
                        var activity = FindActivity(Model.ActivityType.Timer, historyEvent.TimerFiredEventAttributes.StartedEventId.ToString(), activityList);
                        activity.Status            = Status.SUCCEEDED;
                        activity.SucceededDateTime = historyEvent.EventTimestamp;
                    }
                    if (historyEvent.EventType == EventType.ActivityTaskScheduled)
                    {
                        var activity = FindActivity(Model.ActivityType.Task, historyEvent.ActivityTaskScheduledEventAttributes.ActivityId, activityList);
                        if (activity == null)
                        {
                            activity = new Activity
                            {
                                ActivityType      = Model.ActivityType.Task,
                                Name              = historyEvent.ActivityTaskScheduledEventAttributes.ActivityType.Name,
                                ScheduledId       = historyEvent.EventId.ToString(),
                                UniqueId          = historyEvent.ActivityTaskScheduledEventAttributes.Control,
                                Status            = Status.SCHEDULED,
                                ScheduledDateTime = historyEvent.EventTimestamp
                            };

                            activityList.Add(activity);
                        }
                    }
                    if (historyEvent.EventType == EventType.ActivityTaskStarted)
                    {
                        var activity = FindActivity(Model.ActivityType.Task, historyEvent.ActivityTaskStartedEventAttributes.ScheduledEventId.ToString(), activityList);
                        activity.Status          = Status.STARTED;
                        activity.StartedDateTime = historyEvent.EventTimestamp;
                    }

                    if (historyEvent.EventType == EventType.ActivityTaskFailed)
                    {
                        var activity = FindActivity(Model.ActivityType.Task, historyEvent.ActivityTaskFailedEventAttributes.ScheduledEventId.ToString(), activityList);
                        activity.Result         = historyEvent.ActivityTaskFailedEventAttributes.Details;
                        activity.Status         = Status.FAILED;
                        activity.FailedDateTime = historyEvent.EventTimestamp;
                    }
                    if (historyEvent.EventType == EventType.ActivityTaskCompleted)
                    {
                        var activity = FindActivity(Model.ActivityType.Task, historyEvent.ActivityTaskCompletedEventAttributes.ScheduledEventId.ToString(), activityList);
                        activity.Result            = historyEvent.ActivityTaskCompletedEventAttributes.Result;
                        activity.Status            = Status.SUCCEEDED;
                        activity.SucceededDateTime = historyEvent.EventTimestamp;
                    }
                    if (historyEvent.EventType == EventType.ActivityTaskTimedOut)
                    {
                        var activity = FindActivity(Model.ActivityType.Task, historyEvent.ActivityTaskTimedOutEventAttributes.ScheduledEventId.ToString(), activityList);

                        activity.Status          = Status.TIMEOUT;
                        activity.TmedOutDateTime = historyEvent.EventTimestamp;
                    }
                }
                workflow.Activities = activityList;
                Task loggingTask = _store.LogStateAsync(workflow);
            }

            return(workflow);
        }