TaskMessage ProcessCreateSubOrchestrationInstanceDecision(
            CreateSubOrchestrationAction createSubOrchestrationAction,
            OrchestrationRuntimeState runtimeState,
            bool includeParameters)
        {
            var historyEvent = new SubOrchestrationInstanceCreatedEvent(createSubOrchestrationAction.Id)
            {
                Name       = createSubOrchestrationAction.Name,
                Version    = createSubOrchestrationAction.Version,
                InstanceId = createSubOrchestrationAction.InstanceId
            };

            if (includeParameters)
            {
                historyEvent.Input = createSubOrchestrationAction.Input;
            }

            runtimeState.AddEvent(historyEvent);

            var taskMessage = new TaskMessage();

            var startedEvent = new ExecutionStartedEvent(-1, createSubOrchestrationAction.Input)
            {
                Tags = OrchestrationTags.MergeTags(createSubOrchestrationAction.Tags, runtimeState.Tags),
                OrchestrationInstance = new OrchestrationInstance
                {
                    InstanceId  = createSubOrchestrationAction.InstanceId,
                    ExecutionId = Guid.NewGuid().ToString("N")
                },
                ParentInstance = new ParentInstance
                {
                    OrchestrationInstance = runtimeState.OrchestrationInstance,
                    Name           = runtimeState.Name,
                    Version        = runtimeState.Version,
                    TaskScheduleId = createSubOrchestrationAction.Id
                },
                Name    = createSubOrchestrationAction.Name,
                Version = createSubOrchestrationAction.Version
            };

            this.logHelper.SchedulingOrchestration(startedEvent);

            taskMessage.OrchestrationInstance = startedEvent.OrchestrationInstance;
            taskMessage.Event = startedEvent;

            return(taskMessage);
        }
        public void HandleSubOrchestrationCreatedEvent(SubOrchestrationInstanceCreatedEvent subOrchestrationCreateEvent)
        {
            int taskId = subOrchestrationCreateEvent.EventId;

            if (orchestratorActionsMap.ContainsKey(taskId))
            {
                orchestratorActionsMap.Remove(taskId);
            }
            else
            {
                throw new NonDeterministicOrchestrationException(subOrchestrationCreateEvent.EventId,
                                                                 string.Format("SubOrchestrationInstanceCreatedEvent: {0} {1} {2} {3} {4}",
                                                                               subOrchestrationCreateEvent.EventId,
                                                                               subOrchestrationCreateEvent.EventType,
                                                                               subOrchestrationCreateEvent.Name,
                                                                               subOrchestrationCreateEvent.Version,
                                                                               subOrchestrationCreateEvent.InstanceId));
            }
        }
Ejemplo n.º 3
0
        static TaskMessage ProcessCreateSubOrchestrationInstanceDecision(
            CreateSubOrchestrationAction createSubOrchestrationAction,
            OrchestrationRuntimeState runtimeState, bool includeParameters)
        {
            var historyEvent = new SubOrchestrationInstanceCreatedEvent(createSubOrchestrationAction.Id);

            historyEvent.Name       = createSubOrchestrationAction.Name;
            historyEvent.Version    = createSubOrchestrationAction.Version;
            historyEvent.InstanceId = createSubOrchestrationAction.InstanceId;
            if (includeParameters)
            {
                historyEvent.Input = createSubOrchestrationAction.Input;
            }
            runtimeState.AddEvent(historyEvent);

            var taskMessage  = new TaskMessage();
            var startedEvent = new ExecutionStartedEvent(-1, createSubOrchestrationAction.Input);

            startedEvent.Tags = runtimeState.Tags;
            startedEvent.OrchestrationInstance = new OrchestrationInstance
            {
                InstanceId  = createSubOrchestrationAction.InstanceId,
                ExecutionId = Guid.NewGuid().ToString("N")
            };
            startedEvent.ParentInstance = new ParentInstance
            {
                OrchestrationInstance = runtimeState.OrchestrationInstance,
                Name           = runtimeState.Name,
                Version        = runtimeState.Version,
                TaskScheduleId = createSubOrchestrationAction.Id
            };
            startedEvent.Name    = createSubOrchestrationAction.Name;
            startedEvent.Version = createSubOrchestrationAction.Version;

            taskMessage.OrchestrationInstance = startedEvent.OrchestrationInstance;
            taskMessage.Event = startedEvent;

            return(taskMessage);
        }
Ejemplo n.º 4
0
        public void HandleSubOrchestrationCreatedEvent(SubOrchestrationInstanceCreatedEvent subOrchestrationCreateEvent)
        {
            int taskId = subOrchestrationCreateEvent.EventId;

            if (!this.orchestratorActionsMap.ContainsKey(taskId))
            {
                throw new NonDeterministicOrchestrationException(subOrchestrationCreateEvent.EventId,
                                                                 $"A previous execution of this orchestration scheduled a sub-orchestration task with sequence ID {taskId} "
                                                                 + $"and name '{subOrchestrationCreateEvent.Name}' (version '{subOrchestrationCreateEvent.Version}', "
                                                                 + $"instance ID '{subOrchestrationCreateEvent.InstanceId}'), but the current replay execution hasn't (yet?) "
                                                                 + "scheduled this task. Was a change made to the orchestrator code after this instance had already started running?");
            }

            var orchestrationAction = this.orchestratorActionsMap[taskId];

            if (orchestrationAction is not CreateSubOrchestrationAction currentReplayAction)
            {
                throw new NonDeterministicOrchestrationException(subOrchestrationCreateEvent.EventId,
                                                                 $"A previous execution of this orchestration scheduled a sub-orchestration task with sequence ID {taskId} "
                                                                 + $"and name '{subOrchestrationCreateEvent.Name}' (version '{subOrchestrationCreateEvent.Version}', "
                                                                 + $"instance ID '{subOrchestrationCreateEvent.InstanceId}'), but the current orchestration replay instead "
                                                                 + $"produced a {orchestrationAction.GetType().Name} action at this sequence number. Was a change made to "
                                                                 + "the orchestrator code after this instance had already started running?");
            }

            if (!string.Equals(subOrchestrationCreateEvent.Name, currentReplayAction.Name, StringComparison.OrdinalIgnoreCase))
            {
                throw new NonDeterministicOrchestrationException(subOrchestrationCreateEvent.EventId,
                                                                 $"A previous execution of this orchestration scheduled a sub-orchestration task with sequence ID {taskId} "
                                                                 + $"and name '{subOrchestrationCreateEvent.Name}' (version '{subOrchestrationCreateEvent.Version}', "
                                                                 + $"instance ID '{subOrchestrationCreateEvent.InstanceId}'), but the current orchestration replay instead "
                                                                 + $"scheduled a sub-orchestration task with name {currentReplayAction.Name} at this sequence number. "
                                                                 + "Was a change made to the orchestrator code after this instance had already started running?");
            }

            this.orchestratorActionsMap.Remove(taskId);
        }
Ejemplo n.º 5
0
        public async Task <TResult> CallSubOrchestratorAsync <TResult>(string functionName, string instanceId, object input)
        {
            var createdEvent = new SubOrchestrationInstanceCreatedEvent(History.Count);

            History.Add(createdEvent);

            try
            {
                var subContext = new InMemoryOrchestrationContext(instanceId, _instanceId, _client);
                await subContext.Run(functionName, input);

                var result = (TResult)subContext._output;

                History.Add(new SubOrchestrationInstanceCompletedEvent(History.Count, createdEvent.EventId,
                                                                       JsonConvert.SerializeObject(result)));

                return(result);
            }
            catch (Exception ex)
            {
                History.Add(new SubOrchestrationInstanceFailedEvent(History.Count, createdEvent.EventId, ex.Message, ex.StackTrace));
                throw;
            }
        }
        HistoryEvent GenerateAbridgedEvent(HistoryEvent evt)
        {
            HistoryEvent returnedEvent = evt;

            if (evt is TaskScheduledEvent)
            {
                var sourceEvent = (TaskScheduledEvent) evt;
                returnedEvent = new TaskScheduledEvent(sourceEvent.EventId)
                {
                    Timestamp = sourceEvent.Timestamp,
                    IsPlayed = sourceEvent.IsPlayed,
                    Name = sourceEvent.Name,
                    Version = sourceEvent.Version,
                    Input = "[..snipped..]",
                };
            }
            else if (evt is TaskCompletedEvent)
            {
                var sourceEvent = (TaskCompletedEvent) evt;
                returnedEvent = new TaskCompletedEvent(sourceEvent.EventId, sourceEvent.TaskScheduledId, "[..snipped..]")
                {
                    Timestamp = sourceEvent.Timestamp,
                    IsPlayed = sourceEvent.IsPlayed,
                };
            }
            else if (evt is SubOrchestrationInstanceCreatedEvent)
            {
                var sourceEvent = (SubOrchestrationInstanceCreatedEvent) evt;
                returnedEvent = new SubOrchestrationInstanceCreatedEvent(sourceEvent.EventId)
                {
                    Timestamp = sourceEvent.Timestamp,
                    IsPlayed = sourceEvent.IsPlayed,
                    Name = sourceEvent.Name,
                    Version = sourceEvent.Version,
                    Input = "[..snipped..]",
                };
            }
            else if (evt is SubOrchestrationInstanceCompletedEvent)
            {
                var sourceEvent = (SubOrchestrationInstanceCompletedEvent) evt;
                returnedEvent = new SubOrchestrationInstanceCompletedEvent(sourceEvent.EventId,
                    sourceEvent.TaskScheduledId, "[..snipped..]")
                {
                    Timestamp = sourceEvent.Timestamp,
                    IsPlayed = sourceEvent.IsPlayed,
                };
            }
            else if (evt is TaskFailedEvent)
            {
                var sourceEvent = (TaskFailedEvent) evt;
                returnedEvent = new TaskFailedEvent(sourceEvent.EventId,
                    sourceEvent.TaskScheduledId, sourceEvent.Reason, "[..snipped..]")
                {
                    Timestamp = sourceEvent.Timestamp,
                    IsPlayed = sourceEvent.IsPlayed,
                };
            }
            else if (evt is SubOrchestrationInstanceFailedEvent)
            {
                var sourceEvent = (SubOrchestrationInstanceFailedEvent) evt;
                returnedEvent = new SubOrchestrationInstanceFailedEvent(sourceEvent.EventId,
                    sourceEvent.TaskScheduledId, sourceEvent.Reason, "[..snipped..]")
                {
                    Timestamp = sourceEvent.Timestamp,
                    IsPlayed = sourceEvent.IsPlayed,
                };
            }
            else if (evt is ExecutionStartedEvent)
            {
                var sourceEvent = (ExecutionStartedEvent) evt;
                returnedEvent = new ExecutionStartedEvent(sourceEvent.EventId, "[..snipped..]")
                {
                    Timestamp = sourceEvent.Timestamp,
                    IsPlayed = sourceEvent.IsPlayed,
                };
            }
            else if (evt is ExecutionCompletedEvent)
            {
                var sourceEvent = (ExecutionCompletedEvent) evt;
                returnedEvent = new ExecutionCompletedEvent(sourceEvent.EventId, "[..snipped..]",
                    sourceEvent.OrchestrationStatus)
                {
                    Timestamp = sourceEvent.Timestamp,
                    IsPlayed = sourceEvent.IsPlayed,
                };
            }
            else if (evt is ExecutionTerminatedEvent)
            {
                var sourceEvent = (ExecutionTerminatedEvent) evt;
                returnedEvent = new ExecutionTerminatedEvent(sourceEvent.EventId, "[..snipped..]")
                {
                    Timestamp = sourceEvent.Timestamp,
                    IsPlayed = sourceEvent.IsPlayed,
                };
            }
            // ContinueAsNewEvent is covered by the ExecutionCompletedEvent block

            return returnedEvent;
        }
        HistoryEvent GenerateAbridgedEvent(HistoryEvent evt)
        {
            HistoryEvent returnedEvent = evt;

            if (evt is TaskScheduledEvent taskScheduledEvent)
            {
                returnedEvent = new TaskScheduledEvent(taskScheduledEvent.EventId)
                {
                    Timestamp = taskScheduledEvent.Timestamp,
                    IsPlayed  = taskScheduledEvent.IsPlayed,
                    Name      = taskScheduledEvent.Name,
                    Version   = taskScheduledEvent.Version,
                    Input     = "[..snipped..]",
                };
            }
            else if (evt is TaskCompletedEvent taskCompletedEvent)
            {
                returnedEvent = new TaskCompletedEvent(taskCompletedEvent.EventId, taskCompletedEvent.TaskScheduledId, "[..snipped..]")
                {
                    Timestamp = taskCompletedEvent.Timestamp,
                    IsPlayed  = taskCompletedEvent.IsPlayed,
                };
            }
            else if (evt is SubOrchestrationInstanceCreatedEvent subOrchestrationInstanceCreatedEvent)
            {
                returnedEvent = new SubOrchestrationInstanceCreatedEvent(subOrchestrationInstanceCreatedEvent.EventId)
                {
                    Timestamp = subOrchestrationInstanceCreatedEvent.Timestamp,
                    IsPlayed  = subOrchestrationInstanceCreatedEvent.IsPlayed,
                    Name      = subOrchestrationInstanceCreatedEvent.Name,
                    Version   = subOrchestrationInstanceCreatedEvent.Version,
                    Input     = "[..snipped..]",
                };
            }
            else if (evt is SubOrchestrationInstanceCompletedEvent subOrchestrationInstanceCompletedEvent)
            {
                returnedEvent = new SubOrchestrationInstanceCompletedEvent(subOrchestrationInstanceCompletedEvent.EventId,
                                                                           subOrchestrationInstanceCompletedEvent.TaskScheduledId, "[..snipped..]")
                {
                    Timestamp = subOrchestrationInstanceCompletedEvent.Timestamp,
                    IsPlayed  = subOrchestrationInstanceCompletedEvent.IsPlayed,
                };
            }
            else if (evt is TaskFailedEvent taskFailedEvent)
            {
                returnedEvent = new TaskFailedEvent(taskFailedEvent.EventId,
                                                    taskFailedEvent.TaskScheduledId, taskFailedEvent.Reason, "[..snipped..]")
                {
                    Timestamp = taskFailedEvent.Timestamp,
                    IsPlayed  = taskFailedEvent.IsPlayed,
                };
            }
            else if (evt is SubOrchestrationInstanceFailedEvent subOrchestrationInstanceFailedEvent)
            {
                returnedEvent = new SubOrchestrationInstanceFailedEvent(subOrchestrationInstanceFailedEvent.EventId,
                                                                        subOrchestrationInstanceFailedEvent.TaskScheduledId, subOrchestrationInstanceFailedEvent.Reason, "[..snipped..]")
                {
                    Timestamp = subOrchestrationInstanceFailedEvent.Timestamp,
                    IsPlayed  = subOrchestrationInstanceFailedEvent.IsPlayed,
                };
            }
            else if (evt is ExecutionStartedEvent executionStartedEvent)
            {
                returnedEvent = new ExecutionStartedEvent(executionStartedEvent.EventId, "[..snipped..]")
                {
                    Timestamp = executionStartedEvent.Timestamp,
                    IsPlayed  = executionStartedEvent.IsPlayed,
                };
            }
            else if (evt is ExecutionCompletedEvent executionCompletedEvent)
            {
                returnedEvent = new ExecutionCompletedEvent(executionCompletedEvent.EventId, "[..snipped..]",
                                                            executionCompletedEvent.OrchestrationStatus)
                {
                    Timestamp = executionCompletedEvent.Timestamp,
                    IsPlayed  = executionCompletedEvent.IsPlayed,
                };
            }
            else if (evt is ExecutionTerminatedEvent executionTerminatedEvent)
            {
                returnedEvent = new ExecutionTerminatedEvent(executionTerminatedEvent.EventId, "[..snipped..]")
                {
                    Timestamp = executionTerminatedEvent.Timestamp,
                    IsPlayed  = executionTerminatedEvent.IsPlayed,
                };
            }
            // ContinueAsNewEvent is covered by the ExecutionCompletedEvent block

            return(returnedEvent);
        }
Ejemplo n.º 8
0
        public static HistoryEvent GetHistoryEvent(this DbDataReader reader, bool isOrchestrationHistory = false)
        {
            string eventTypeString = (string)reader["EventType"];

            if (!Enum.TryParse(eventTypeString, out EventType eventType))
            {
                throw new InvalidOperationException($"Unknown event type '{eventTypeString}'.");
            }

            int eventId = GetTaskId(reader);

            HistoryEvent historyEvent;

            switch (eventType)
            {
            case EventType.ContinueAsNew:
                historyEvent = new ContinueAsNewEvent(eventId, GetPayloadText(reader));
                break;

            case EventType.EventRaised:
                historyEvent = new EventRaisedEvent(eventId, GetPayloadText(reader))
                {
                    Name = GetName(reader),
                };
                break;

            case EventType.EventSent:
                historyEvent = new EventSentEvent(eventId)
                {
                    Input      = GetPayloadText(reader),
                    Name       = GetName(reader),
                    InstanceId = GetInstanceId(reader),
                };
                break;

            case EventType.ExecutionCompleted:
                historyEvent = new ExecutionCompletedEvent(
                    eventId,
                    result: GetPayloadText(reader),
                    orchestrationStatus: OrchestrationStatus.Completed);
                break;

            case EventType.ExecutionFailed:
                historyEvent = new ExecutionCompletedEvent(
                    eventId,
                    result: GetPayloadText(reader),
                    orchestrationStatus: OrchestrationStatus.Failed);
                break;

            case EventType.ExecutionStarted:
                historyEvent = new ExecutionStartedEvent(eventId, GetPayloadText(reader))
                {
                    Name = GetName(reader),
                    OrchestrationInstance = new OrchestrationInstance
                    {
                        InstanceId  = GetInstanceId(reader),
                        ExecutionId = GetExecutionId(reader),
                    },
                    Tags    = null,  // TODO
                    Version = GetVersion(reader),
                };
                string?parentInstanceId = GetParentInstanceId(reader);
                if (parentInstanceId != null)
                {
                    ((ExecutionStartedEvent)historyEvent).ParentInstance = new ParentInstance
                    {
                        OrchestrationInstance = new OrchestrationInstance
                        {
                            InstanceId = parentInstanceId
                        },
                        TaskScheduleId = GetTaskId(reader)
                    };
                }
                break;

            case EventType.ExecutionTerminated:
                historyEvent = new ExecutionTerminatedEvent(eventId, GetPayloadText(reader));
                break;

            case EventType.GenericEvent:
                historyEvent = new GenericEvent(eventId, GetPayloadText(reader));
                break;

            case EventType.OrchestratorCompleted:
                historyEvent = new OrchestratorCompletedEvent(eventId);
                break;

            case EventType.OrchestratorStarted:
                historyEvent = new OrchestratorStartedEvent(eventId);
                break;

            case EventType.SubOrchestrationInstanceCompleted:
                historyEvent = new SubOrchestrationInstanceCompletedEvent(eventId, GetTaskId(reader), GetPayloadText(reader));
                break;

            case EventType.SubOrchestrationInstanceCreated:
                historyEvent = new SubOrchestrationInstanceCreatedEvent(eventId)
                {
                    Input      = GetPayloadText(reader),
                    InstanceId = null,     // TODO
                    Name       = GetName(reader),
                    Version    = null,
                };
                break;

            case EventType.SubOrchestrationInstanceFailed:
                historyEvent = new SubOrchestrationInstanceFailedEvent(
                    eventId,
                    taskScheduledId: GetTaskId(reader),
                    reason: GetReason(reader),
                    details: GetPayloadText(reader));
                break;

            case EventType.TaskCompleted:
                historyEvent = new TaskCompletedEvent(
                    eventId,
                    taskScheduledId: GetTaskId(reader),
                    result: GetPayloadText(reader));
                break;

            case EventType.TaskFailed:
                historyEvent = new TaskFailedEvent(
                    eventId,
                    taskScheduledId: GetTaskId(reader),
                    reason: GetReason(reader),
                    details: GetPayloadText(reader));
                break;

            case EventType.TaskScheduled:
                historyEvent = new TaskScheduledEvent(eventId)
                {
                    Input   = GetPayloadText(reader),
                    Name    = GetName(reader),
                    Version = GetVersion(reader),
                };
                break;

            case EventType.TimerCreated:
                historyEvent = new TimerCreatedEvent(eventId)
                {
                    FireAt = GetVisibleTime(reader),
                };
                break;

            case EventType.TimerFired:
                historyEvent = new TimerFiredEvent(eventId)
                {
                    FireAt  = GetVisibleTime(reader),
                    TimerId = GetTaskId(reader),
                };
                break;

            default:
                throw new InvalidOperationException($"Don't know how to interpret '{eventType}'.");
            }

            historyEvent.Timestamp = GetTimestamp(reader);
            historyEvent.IsPlayed  = isOrchestrationHistory && (bool)reader["IsPlayed"];
            return(historyEvent);
        }