public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            PollForDecisionTaskResponse response = new PollForDecisionTaskResponse();

            UnmarshallResult(context, response);

            return(response);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            PollForDecisionTaskResponse response = new PollForDecisionTaskResponse();

            response.DecisionTask = DecisionTaskUnmarshaller.Instance.Unmarshall(context);

            return(response);
        }
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            PollForDecisionTaskResponse response = new PollForDecisionTaskResponse();

            context.Read();
            response.PollForDecisionTaskResult = PollForDecisionTaskResultUnmarshaller.GetInstance().Unmarshall(context);

            return(response);
        }
            private async Task <DecisionTask> GetDecisionTask(string domain, string taskList, string workerId)
            {
                PollForDecisionTaskResponse response = null;

                do
                {
                    response = await PollForDecisionTaskAsync(domain, taskList, workerId);
                }while (_isRunning && string.IsNullOrEmpty(response.DecisionTask.TaskToken));
                await AccumulateAdditionalPages(domain, taskList, workerId, response.DecisionTask);

                return(response.DecisionTask);
            }
Ejemplo n.º 5
0
        DecisionTask Poll()
        {
            Logger.LogMessage("Polling for Decision task ...");
            PollForDecisionTaskRequest request = new PollForDecisionTaskRequest()
            {
                Domain   = Constants.SWF_DOMAIN,
                TaskList = new TaskList()
                {
                    Name = Constants.SWF_DECIDER_TASKLIST
                }
            };
            PollForDecisionTaskResponse response = _swfClient.PollForDecisionTaskAsync(request).Result;

            return(response.DecisionTask);
        }
Ejemplo n.º 6
0
        private async Task <DecisionTask> Poll()
        {
            this.EmrJobLogger.PrintInfo(SwfResources.Info_PollingDecisionTask);
            PollForDecisionTaskRequest request = new PollForDecisionTaskRequest()
            {
                Domain   = this.SwfConfiguration.DomainName,
                TaskList = new TaskList()
                {
                    Name = this.SwfConfiguration.DecisionTasksList
                }
            };

            PollForDecisionTaskResponse response = await this.SwfClient.PollForDecisionTaskAsync(request);

            return(response.DecisionTask);
        }
Ejemplo n.º 7
0
 private static void UnmarshallResult(JsonUnmarshallerContext context, PollForDecisionTaskResponse response)
 {
     response.DecisionTask = DecisionTaskUnmarshaller.GetInstance().Unmarshall(context);
     return;
 }
Ejemplo n.º 8
0
        // Simple logic
        //  Creates four activities at the begining
        //  Waits for them to complete and completes the workflow
        static void Decider()
        {
            int activityCount = 0; // This refers to total number of activities per workflow
            IAmazonSimpleWorkflow swfClient = AWSClientFactory.CreateAmazonSimpleWorkflowClient();

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

                PollForDecisionTaskResponse response = swfClient.PollForDecisionTask(request);
                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) // Create this only at the begining
                {
                    ScheduleActivity("Activity1A", decisions);
                    ScheduleActivity("Activity1B", decisions);
                    ScheduleActivity("Activity2", decisions);
                    ScheduleActivity("Activity2", 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("Decider: WORKFLOW COMPLETE!!!!!!!!!!!!!!!!!!!!!!");
                }
                RespondDecisionTaskCompletedRequest respondDecisionTaskCompletedRequest =
                    new RespondDecisionTaskCompletedRequest()
                {
                    Decisions = decisions,
                    TaskToken = response.DecisionTask.TaskToken
                };
                swfClient.RespondDecisionTaskCompleted(respondDecisionTaskCompletedRequest);
            }
        }
Ejemplo n.º 9
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.º 10
0
        /// <summary>
        /// Polls for descision tasks and decides what decisions to make.
        /// </summary>
        internal void PollAndDecide()
        {
            Debug.Assert(_workflow != null);

            while (!_cancellationToken.IsCancellationRequested)
            {
                var taskTolken = "";
                try
                {
                    var decisionTaskRequest = new PollForDecisionTaskRequest
                    {
                        Domain = _workflow.Options.Domain,
                        //Identity = _workflow.WorkflowId,
                        TaskList = _workflow.TaskList                         // This could be a specific TaskList instead of default value from context.
                    };

                    Logger.Debug("TaskList: {0}", _workflow.TaskList.Name);

                    PollForDecisionTaskResponse decisionTaskResponse = _swfClient.PollForDecisionTaskAsync(decisionTaskRequest).Result;
                    DecisionTask decisionTask = decisionTaskResponse.DecisionTask;

                    taskTolken = decisionTask.TaskToken;

                    if (!string.IsNullOrEmpty(decisionTask.TaskToken))
                    {
                        Logger.Debug("Get Decision.");

                        // Define a new WorkflowEventsProcessor object and let it make the decision!
                        var workflowProcessor = new WorkflowEventsProcessor(decisionTask, _workflow, decisionTaskRequest, _swfClient, _storageClient);
                        var decisionRequest   = workflowProcessor.Decide();

                        //var decisionRequest = _workflow.Decide(decisionTask, decisionTaskRequest);

                        Logger.Debug("RespondDecisionTaskCompleted.");

                        // We have our decision, send it away and do something more productive with the response
                        _swfClient.RespondDecisionTaskCompletedAsync(decisionRequest).Wait();
                    }

                    //Sleep to avoid aggressive polling
                    Thread.Sleep(200);
                }

                /*catch (AmazonSimpleWorkflowException ex)
                 * {
                 *      Logger.Error(ex, "");
                 *
                 *      //if (_workflow != null)
                 *      //	_workflow.StopWorkers();
                 *
                 *      //if (!string.IsNullOrEmpty(taskTolken))
                 *      //{
                 *      //	var respond = _workflow.FailWorkflowRespond(ex.Message, "");
                 *      //	respond.TaskToken = taskTolken;
                 *      //	try
                 *      //	{
                 *      //		// Just try to stop workflow.
                 *      //		_swfClient.RespondDecisionTaskCompleted(respond);
                 *      //	}
                 *      //	catch
                 *      //	{
                 *      //	}
                 *      //}
                 *      //Console.WriteLine("Caught Exception: " + ex.Message);
                 *      //Console.WriteLine("Response Status Code: " + ex.StatusCode);
                 *      //Console.WriteLine("Error Code: " + ex.ErrorCode);
                 *      //Console.WriteLine("Error Type: " + ex.ErrorType);
                 *      //Console.WriteLine("Request ID: " + ex.RequestId);
                 *      //Console.WriteLine("Data: " + ex.Data);
                 *      //Console.WriteLine("Stacktrace: " + ex.StackTrace);
                 * }*/
                catch (Exception e)
                {
                    Logger.Error(e, "");

                    //if (_workflow != null)
                    //	_workflow.StopWorkers();

                    //if (!string.IsNullOrEmpty(taskTolken))
                    //{
                    //	var respond = _workflow.FailWorkflowRespond(e.Message, "");
                    //	respond.TaskToken = taskTolken;
                    //	try
                    //	{
                    //		// Just try to stop workflow.
                    //		_swfClient.RespondDecisionTaskCompleted(respond);
                    //	}
                    //	catch
                    //	{
                    //	}
                    //}
                }
            }

            Logger.Info("Exit from Workflow Worker.");
        }
Ejemplo n.º 11
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);
        }