Specifies a decision made by the decider. A decision can be one of these types:

  • CancelTimer cancels a previously started timer and records a TimerCanceled event in the history.
  • CancelWorkflowExecution closes the workflow execution and records a WorkflowExecutionCanceled event in the history.
  • CompleteWorkflowExecution closes the workflow execution and records a WorkflowExecutionCompleted event in the history .
  • ContinueAsNewWorkflowExecution closes the workflow execution and starts a new workflow execution of the same type using the same workflow id and a unique run Id. A WorkflowExecutionContinuedAsNew event is recorded in the history.
  • FailWorkflowExecution closes the workflow execution and records a WorkflowExecutionFailed event in the history.
  • RecordMarker records a MarkerRecorded event in the history. Markers can be used for adding custom information in the history for instance to let deciders know that they do not need to look at the history beyond the marker event.
  • RequestCancelActivityTask attempts to cancel a previously scheduled activity task. If the activity task was scheduled but has not been assigned to a worker, then it will be canceled. If the activity task was already assigned to a worker, then the worker will be informed that cancellation has been requested in the response to RecordActivityTaskHeartbeat.
  • RequestCancelExternalWorkflowExecution requests that a request be made to cancel the specified external workflow execution and records a RequestCancelExternalWorkflowExecutionInitiated event in the history.
  • ScheduleActivityTask schedules an activity task.
  • SignalExternalWorkflowExecution requests a signal to be delivered to the specified external workflow execution and records a SignalExternalWorkflowExecutionInitiated event in the history.
  • StartChildWorkflowExecution requests that a child workflow execution be started and records a StartChildWorkflowExecutionInitiated event in the history. The child workflow execution is a separate workflow execution with its own history.
  • StartTimer starts a timer for this workflow execution and records a TimerStarted event in the history. This timer will fire after the specified delay and record a TimerFired event.

Access Control

If you grant permission to use RespondDecisionTaskCompleted , you can use IAM policies to express permissions for the list of decisions returned by this action as if they were members of the API. Treating decisions as a pseudo API maintains a uniform conceptual model and helps keep policies readable. For details and example IAM policies, see Using IAM to Manage Access to Amazon SWF Workflows .

Decision Failure

Decisions can fail for several reasons

  • The ordering of decisions should follow a logical flow. Some decisions might not make sense in the current context of the workflow execution and will therefore fail.
  • A limit on your account was reached.
  • The decision lacks sufficient permissions.

One of the following events might be added to the history to indicate an error. The event attribute's cause parameter indicates the cause. If cause is set to OPERATION_NOT_PERMITTED, the decision failed because it lacked sufficient permissions.

  • ScheduleActivityTaskFailed a ScheduleActivityTask decision failed. This could happen if the activity type specified in the decision is not registered, is in a deprecated state, or the decision is not properly configured.
  • RequestCancelActivityTaskFailed a RequestCancelActivityTask decision failed. This could happen if there is no open activity task with the specified activityId.
  • StartTimerFailed a StartTimer decision failed. This could happen if there is another open timer with the same timerId.
  • CancelTimerFailed a CancelTimer decision failed. This could happen if there is no open timer with the specified timerId.
  • StartChildWorkflowExecutionFailed a StartChildWorkflowExecution decision failed. This could happen if the workflow type specified is not registered, is deprecated, or the decision is not properly configured.
  • SignalExternalWorkflowExecutionFailed a SignalExternalWorkflowExecution decision failed. This could happen if the workflowID specified in the decision was incorrect.
  • RequestCancelExternalWorkflowExecutionFailed a RequestCancelExternalWorkflowExecution decision failed. This could happen if the workflowID specified in the decision was incorrect.
  • CancelWorkflowExecutionFailed a CancelWorkflowExecution decision failed. This could happen if there is an unhandled decision task pending in the workflow execution.
  • CompleteWorkflowExecutionFailed a CompleteWorkflowExecution decision failed. This could happen if there is an unhandled decision task pending in the workflow execution.
  • ContinueAsNewWorkflowExecutionFailed a ContinueAsNewWorkflowExecution decision failed. This could happen if there is an unhandled decision task pending in the workflow execution or the ContinueAsNewWorkflowExecution decision was not configured correctly.
  • FailWorkflowExecutionFailed a FailWorkflowExecution decision failed. This could happen if there is an unhandled decision task pending in the workflow execution.

The preceding error events might occur due to an error in the decider logic, which might put the workflow execution in an unstable state The cause field in the event structure for the error event indicates the cause of the error.

NOTE: A workflow execution may be closed by the decider by returning one of the following decisions when completing a decision task: CompleteWorkflowExecution, FailWorkflowExecution, CancelWorkflowExecution and ContinueAsNewWorkflowExecution. An UnhandledDecision fault will be returned if a workflow closing decision is specified and a signal or activity event had been added to the history while the decision task was being performed by the decider. Unlike the above situations which are logic issues, this fault is always possible because of race conditions in a distributed system. The right action here is to call RespondDecisionTaskCompleted without any decisions. This would result in another decision task with these new events included in the history. The decider should handle the new events and may decide to close the workflow execution.

How to Code a Decision

You code a decision by first setting the decision type field to one of the above decision values, and then set the corresponding attributes field shown below:

  • ScheduleActivityTaskDecisionAttributes
  • RequestCancelActivityTaskDecisionAttributes
  • CompleteWorkflowExecutionDecisionAttributes
  • FailWorkflowExecutionDecisionAttributes
  • CancelWorkflowExecutionDecisionAttributes
  • ContinueAsNewWorkflowExecutionDecisionAttributes
  • RecordMarkerDecisionAttributes
  • StartTimerDecisionAttributes
  • CancelTimerDecisionAttributes
  • SignalExternalWorkflowExecutionDecisionAttributes
  • RequestCancelExternalWorkflowExecutionDecisionAttributes
  • StartChildWorkflowExecutionDecisionAttributes
예제 #1
0
파일: Program.cs 프로젝트: 40a/Samples
        // 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);
              }
        }
예제 #2
0
파일: Program.cs 프로젝트: 40a/Samples
 static void ScheduleActivity(string name, List<Decision> decisions)
 {
     Decision decision = new Decision()
       {
     DecisionType = DecisionType.ScheduleActivityTask,
     ScheduleActivityTaskDecisionAttributes =  // Uses DefaultTaskList
     new ScheduleActivityTaskDecisionAttributes() {
               ActivityType = new ActivityType()
               {
                 Name = name,
                 Version = "2.0"
               },
               ActivityId = name + "-" + System.Guid.NewGuid().ToString(),
               Input = "{\"activityInput1\":\"value1\"}"
             }
       };
       Console.WriteLine("Decider: ActivityId=" +
             decision.ScheduleActivityTaskDecisionAttributes.ActivityId);
       decisions.Add(decision);
 }
예제 #3
0
        /// <summary>
        /// Helper method to create a decision for completed workflow exeution. This happens once all the thumbnails have been created.
        /// </summary>
        /// <returns>Decision with ScheduleActivityTaskDecisionAttributes</returns>
        Decision CreateCompleteWorkflowExecutionDecision(List<Common.ActivityTaskCompletedResult> states)
        {
            // Create a string listing all the images create.
            StringBuilder sb = new StringBuilder();
            states.ForEach(x => sb.AppendFormat("\tRequestActionId: {0} \r\n", x.RequestActionId));

            Decision decision = new Decision()
            {
                DecisionType = DecisionType.CompleteWorkflowExecution,
                CompleteWorkflowExecutionDecisionAttributes = new CompleteWorkflowExecutionDecisionAttributes
                {
                    Result = sb.ToString()
                }
            };
            Console.WriteLine("Decision: Complete Workflow Execution");
            Console.WriteLine(sb.ToString());
            return decision;
        }
예제 #4
0
        /// <summary>
        /// Helper method to create a decision for scheduling an activity
        /// </summary>
        /// <returns>Decision with ScheduleActivityTaskDecisionAttributes</returns>
        Decision CreateActivityDecision(Common.WorkflowExecutionStartedInput startingInput, int RequestActionId)
        {
            // setup the input for the activity task.
            Common.ActivityTaskCompletedResult state = new Common.ActivityTaskCompletedResult
            {
                StartingInput = startingInput,
                RequestActionId = RequestActionId
            };

            Decision decision = new Decision()
            {
                DecisionType = DecisionType.ScheduleActivityTask,
                ScheduleActivityTaskDecisionAttributes = new ScheduleActivityTaskDecisionAttributes()
                {
                    ActivityType = new Amazon.SimpleWorkflow.Model.ActivityType()
                    {
                        Name = "MinimalWorkflowActivityType1",
                        Version = "5.0"
                    },
                    ActivityId = "MinimalWorkflowActivityType1" + DateTime.Now.TimeOfDay,
                    Input = Common.Utils.SerializeToJSON<Common.ActivityTaskCompletedResult>(state)
                }
            };
            Console.WriteLine(string.Format("Decision: Schedule Activity Task (RequestId {0} to RequestActionId {1})", state.StartingInput.RequestId, RequestActionId));
            return decision;
        }
예제 #5
0
        List<Decision> DecideHelper(Common.WorkflowExecutionStartedInput startingInput, List<Common.ActivityTaskCompletedResult> activityStates)
        {
            List<Decision> decisions = new List<Decision>();

            //the request should have a new state and actions...
            Request RequestCurrent = _tracker.GetRequestWithActions(startingInput.RequestId);

            IEnumerable<RequestAction> ActiveNotComplete = RequestCurrent.RequestActions.Where(x => (x.ActionTypeId == Common.Constants.AWSActivityTypeId || x.ActionTypeId == 2) && x.IsActive && !x.IsComplete);

            //if a request action was found that was "active" but not "complete" it must be run now
            if (ActiveNotComplete.Count() != 0)
            {

                //iterate through the active but not complete actions...
                foreach (RequestAction ra in ActiveNotComplete)
                {

                    if (ra.ActionTypeId == 1) //if state completed...
                    {
                        //code to tell SWF to fire activity for the new state
                        decisions.Add(CreateActivityDecision(startingInput, ra.RequestActionId ?? 0));
                    }
                    if (ra.ActionTypeId == 2) //if timer
                    {
                        // setup the input for the activity task.
                        Common.ActivityTaskCompletedResult state = new Common.ActivityTaskCompletedResult
                        {
                            StartingInput = startingInput,
                            RequestActionId = ra.RequestActionId ?? 0
                        };

                        Decision decision = new Decision()
                        {
                            DecisionType = DecisionType.StartTimer,
                            StartTimerDecisionAttributes = new StartTimerDecisionAttributes()
                            {
                                Control = Common.Utils.SerializeToJSON<Common.ActivityTaskCompletedResult>(state),
                                StartToFireTimeout = "3",
                                TimerId = DateTime.Now.Ticks.ToString()
                            }
                        };

                        decisions.Add(decision);
                    }

                }
            }
            else
            {
                Console.WriteLine("Workflow execution complete for " + startingInput.RequestId);
                //code to tell SWF to complete workflow execution
                decisions.Add(CreateCompleteWorkflowExecutionDecision(activityStates));
            }
            return decisions;
        }
        List<Decision> Decide(DecisionTask task)
        {
            try
            {
                List<Decision> decisions = new List<Decision>();
                var startEvent = task.Events.FirstOrDefault(x => x.EventType == EventType.WorkflowExecutionStarted);
                if (startEvent != null)
                {
                    var pollId = startEvent.WorkflowExecutionStartedEventAttributes.Input;
                    Logger.LogMessage("Processing decision task for poll id: " + pollId);

                    var poll = PollProcessor.Instance.GetPollAsync(pollId).Result;

                    if (poll != null)
                    {
                        if (poll.State == PollDefinition.POLL_STATE_UNSCHEDULE && DateTime.Now < poll.StartTime)
                        {
                            // Add second to compensate for the rounding
                            var timeDelay = (int)(new TimeSpan(poll.StartTime.Ticks - DateTime.Now.Ticks).TotalSeconds) + 1;
                            var decision = new Decision
                            {
                                DecisionType = DecisionType.StartTimer,
                                StartTimerDecisionAttributes = new StartTimerDecisionAttributes
                                {
                                    StartToFireTimeout = timeDelay.ToString(),
                                    TimerId = Guid.NewGuid().ToString()
                                }
                            };
                            decisions.Add(decision);
                            PollProcessor.Instance.UpdatePollStateAsync(pollId, PollDefinition.POLL_STATE_SCHEDULE).Wait();
                            Logger.LogMessage("Scheduled timer for {0} seconds till activating poll.", timeDelay);
                        }
                        else if (poll.State == PollDefinition.POLL_STATE_SCHEDULE)
                        {
                            Decision decision = new Decision()
                            {
                                DecisionType = DecisionType.ScheduleActivityTask,
                                ScheduleActivityTaskDecisionAttributes = new ScheduleActivityTaskDecisionAttributes()
                                {
                                    ActivityType = new ActivityType()
                                    {
                                        Name = Constants.SWF_ACTIVTY_START_TIMER_EXPIRED,
                                        Version = Constants.SWF_ACTIVTY_START_TIMER_EXPIRED_VERSION
                                    },
                                    TaskList = new TaskList
                                    {
                                        Name = Constants.SWF_ACTIVTY_START_TIMER_EXPIRED_TASKLIST
                                    },
                                    HeartbeatTimeout = Constants.SWF_TIMEOUT,
                                    ScheduleToCloseTimeout = Constants.SWF_TIMEOUT,
                                    ScheduleToStartTimeout = Constants.SWF_TIMEOUT,
                                    StartToCloseTimeout = Constants.SWF_TIMEOUT,
                                    ActivityId = string.Format("{0}:{1}", Constants.SWF_ACTIVTY_START_TIMER_EXPIRED, DateTime.Now.Ticks),
                                    Input = poll.Id
                                }
                            };
                            decisions.Add(decision);
                            Logger.LogMessage("Start timer complete now deciding to run the {0} activity to activate poll.", Constants.SWF_ACTIVTY_START_TIMER_EXPIRED);
                        }
                        else if (poll.State == PollDefinition.POLL_STATE_ACTIVE && DateTime.Now < poll.EndTime)
                        {
                            // Add second to compensate for the rounding
                            var timeDelay = (int)(new TimeSpan(poll.EndTime.Ticks - DateTime.Now.Ticks).TotalSeconds) + 1;
                            var decision = new Decision
                            {
                                DecisionType = DecisionType.StartTimer,
                                StartTimerDecisionAttributes = new StartTimerDecisionAttributes
                                {
                                    StartToFireTimeout = timeDelay.ToString(),
                                    TimerId = Guid.NewGuid().ToString()
                                }
                            };
                            decisions.Add(decision);
                            Logger.LogMessage("Scheduled timer for {0} seconds till poll expires.", timeDelay);
                        }
                        else if (poll.State == PollDefinition.POLL_STATE_ACTIVE)
                        {
                            Decision decision = new Decision()
                            {
                                DecisionType = DecisionType.ScheduleActivityTask,
                                ScheduleActivityTaskDecisionAttributes = new ScheduleActivityTaskDecisionAttributes()
                                {
                                    ActivityType = new ActivityType()
                                    {
                                        Name = Constants.SWF_ACTIVTY_END_TIMER_EXPIRED,
                                        Version = Constants.SWF_ACTIVTY_END_TIMER_EXPIRED_VERSION
                                    },
                                    TaskList = new TaskList
                                    {
                                        Name = Constants.SWF_ACTIVTY_END_TIMER_EXPIRED_TASKLIST
                                    },
                                    HeartbeatTimeout = Constants.SWF_TIMEOUT,
                                    ScheduleToCloseTimeout = Constants.SWF_TIMEOUT,
                                    ScheduleToStartTimeout = Constants.SWF_TIMEOUT,
                                    StartToCloseTimeout = Constants.SWF_TIMEOUT,
                                    ActivityId = string.Format("{0}:{1}", Constants.SWF_ACTIVTY_END_TIMER_EXPIRED, DateTime.Now.Ticks),
                                    Input = poll.Id
                                }
                            };
                            decisions.Add(decision);
                            Logger.LogMessage("End timer complete now deciding to run the {0} activity to expire poll.", Constants.SWF_ACTIVTY_END_TIMER_EXPIRED);
                        }
                    }
                }

                if (decisions.Count == 0)
                {
                    Decision decision = new Decision()
                    {
                        DecisionType = DecisionType.CompleteWorkflowExecution,
                        CompleteWorkflowExecutionDecisionAttributes = new CompleteWorkflowExecutionDecisionAttributes
                        {
                        }
                    };
                    decisions.Add(decision);
                    Logger.LogMessage("Workflow execution complete for {0}", task.WorkflowExecution.WorkflowId);
                }

                return decisions;
            }
            catch(Exception e)
            {
                string message = string.Format("Error processing work flow execution {0} and is being aborted: {1}\n", task.WorkflowExecution.WorkflowId, e.Message, e.StackTrace);
                Logger.LogMessage(message);
                Decision decision = new Decision()
                {
                    DecisionType = DecisionType.CompleteWorkflowExecution,
                    CompleteWorkflowExecutionDecisionAttributes = new CompleteWorkflowExecutionDecisionAttributes
                    {
                        Result = message
                    }
                };
                return new List<Decision> { decision };                
            }
        }