Exemplo n.º 1
0
        /// <summary>
        /// Dispatch a standard <see cref="SagaTimeout" /> message to current
        /// saga.
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="timeOutKey">key related to this timeOut, it can be null</param>
        public void DispatchTimeout(DateTime dateTime, string timeOutKey = null)
        {
            var timeout = new SagaTimeout(this.Id, timeOutKey);
            var message = new SagaDeferredMessage(timeout, dateTime);

            Dispatch(message);
        }
        public async Task Dispatch(IChunk commit)
        {
            var changeset = commit.Payload as Changeset;

            if (changeset != null)
            {
                var reactions = changeset.Events.OfType <MessageReaction>();
                foreach (var reaction in reactions)
                {
                    foreach (var message in reaction.MessagesOut)
                    {
                        try
                        {
                            if (message is IScheduledAt scheduledAt)
                            {
                                TimeSpan?at = null;

                                if (scheduledAt.At.ToUniversalTime() > DateTime.UtcNow)
                                {
                                    at = scheduledAt.At.ToUniversalTime().Subtract(DateTime.UtcNow);
                                }
                                await Dispatch(scheduledAt.Payload, at).ConfigureAwait(false);
                            }
                            else if (message is IMessageAndTimeout messageAndTimeout)
                            {
                                //1) send the message if needed
                                if (messageAndTimeout.SendMessageOut)
                                {
                                    await Dispatch(messageAndTimeout.Message, null).ConfigureAwait(false);
                                }

                                //2) send the timeout
                                String sagaId;
                                if (messageAndTimeout.SentToSelf)
                                {
                                    sagaId = commit.PartitionId;
                                }
                                else
                                {
                                    sagaId = messageAndTimeout.Target;
                                }
                                SagaTimeout timeout = new SagaTimeout(sagaId, GetCorrelationTimeoutKey(messageAndTimeout));
                                await Dispatch(timeout, messageAndTimeout.Delay).ConfigureAwait(false);
                            }
                            else
                            {
                                await Dispatch(message, null).ConfigureAwait(false);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.ErrorFormat(ex, "Error dispatching commit Id {0} Error {1}", commit.Position, ex.Message);
                        }
                    }
                }
            }

            _currentCheckpoint.LastDispatchedPosition = commit.Position;
            _checkpointCollection.Save(_currentCheckpoint, _currentCheckpoint.Id);
        }