Exemple #1
0
        public IServiceBus Start()
        {
            if (Started)
            {
                throw new ApplicationException(Resources.ServiceBusInstanceAlreadyStarted);
            }

            ConfigurationInvariant();

            var startupPipeline = _pipelineFactory.GetPipeline <StartupPipeline>();

            Started = true; // required for using ServiceBus in OnStarted event

            try
            {
                startupPipeline.Execute();

                _inboxThreadPool           = startupPipeline.State.Get <IProcessorThreadPool>("InboxThreadPool");
                _controlThreadPool         = startupPipeline.State.Get <IProcessorThreadPool>("ControlInboxThreadPool");
                _outboxThreadPool          = startupPipeline.State.Get <IProcessorThreadPool>("OutboxThreadPool");
                _deferredMessageThreadPool = startupPipeline.State.Get <IProcessorThreadPool>("DeferredMessageThreadPool");
            }
            catch
            {
                Started = false;
                throw;
            }

            return(this);
        }
Exemple #2
0
        public virtual void Execute(CancellationToken cancellationToken)
        {
            var availableWorker = _workerAvailabilityManager.GetAvailableWorker();

            if (_configuration.Inbox.Distribute && availableWorker == null)
            {
                _threadActivity.Waiting(cancellationToken);

                return;
            }

            var messagePipeline = availableWorker == null
                ? _pipelineFactory.GetPipeline <InboxMessagePipeline>()
                : (IPipeline)_pipelineFactory.GetPipeline <DistributorPipeline>();

            try
            {
                messagePipeline.State.SetAvailableWorker(availableWorker);
                messagePipeline.State.ResetWorking();
                messagePipeline.State.SetCancellationToken(cancellationToken);
                messagePipeline.State.SetTransportMessage(null);
                messagePipeline.State.SetReceivedMessage(null);

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                messagePipeline.Execute();

                if (messagePipeline.State.GetWorking())
                {
                    _events.OnThreadWorking(this, new ThreadStateEventArgs(typeof(InboxMessagePipeline)));

                    _threadActivity.Working();
                }
                else
                {
                    _events.OnThreadWaiting(this, new ThreadStateEventArgs(typeof(InboxMessagePipeline)));

                    _threadActivity.Waiting(cancellationToken);
                }
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(messagePipeline);
            }
        }
        public void Execute(OnGetStreamEvents pipelineEvent)
        {
            var state    = pipelineEvent.Pipeline.State;
            var events   = new List <object>();
            var pipeline = _pipelineFactory.GetPipeline <GetEventEnvelopePipeline>();

            try
            {
                var version = 0;

                foreach (var primitiveEvent in _primitiveEventRepository.Get(state.GetId()))
                {
                    if (primitiveEvent.Version < version)
                    {
                        throw new InvalidOperationException(string.Format(Resources.InvalidEventOrderingException,
                                                                          primitiveEvent.Version, version));
                    }

                    pipeline.Execute(primitiveEvent);

                    events.Add(pipeline.State.GetEvent());

                    version = primitiveEvent.Version;
                }

                state.SetEvents(events);
                state.SetVersion(version);
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(pipeline);
            }
        }
Exemple #4
0
        public void Execute(OnGetProjectionEventEnvelope pipelineEvent)
        {
            var state           = pipelineEvent.Pipeline.State;
            var projectionEvent = state.GetProjectionEvent();

            Guard.AgainstNull(projectionEvent, nameof(projectionEvent));

            if (!projectionEvent.HasPrimitiveEvent)
            {
                return;
            }

            var pipeline = _pipelineFactory.GetPipeline <GetEventEnvelopePipeline>();

            try
            {
                pipeline.Execute(projectionEvent.PrimitiveEvent);

                state.SetEventEnvelope(pipeline.State.GetEventEnvelope());
                state.SetEvent(pipeline.State.GetEvent());
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(pipeline);
            }
        }
Exemple #5
0
        public async Task MessageReceived(SlackMessage message)
        {
            Console.WriteLine("[[[Message started]]]");

            IMiddleware pipeline        = _pipelineFactory.GetPipeline();
            var         incomingMessage = new IncomingMessage
            {
                RawText        = message.Text,
                FullText       = message.Text,
                UserId         = message.User.Id,
                Username       = GetUsername(message),
                Channel        = message.ChatHub.Id,
                ChannelType    = message.ChatHub.Type == SlackChatHubType.DM ? ResponseType.DirectMessage : ResponseType.Channel,
                UserChannel    = await GetUserChannel(message),
                BotName        = _connection.Self.Name,
                BotId          = _connection.Self.Id,
                BotIsMentioned = message.MentionsBot
            };

            incomingMessage.TargetedText = incomingMessage.GetTargetedText();

            try
            {
                foreach (ResponseMessage responseMessage in pipeline.Invoke(incomingMessage))
                {
                    await SendMessage(responseMessage);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR WHILE PROCESSING MESSAGE: {0}", ex);
            }

            Console.WriteLine("[[[Message ended]]]");
        }
        public void Execute(IThreadState state)
        {
            var pipeline = _pipelineFactory.GetPipeline <EventProcessingPipeline>();

            while (state.Active)
            {
                var projection = _eventProcessor.GetProjection();
                var waiting    = true;

                if (projection != null)
                {
                    pipeline.State.Clear();
                    pipeline.State.SetProjection(projection);
                    pipeline.State.SetThreadState(state);

                    pipeline.Execute();

                    if (pipeline.State.GetWorking())
                    {
                        _threadActivity.Working();
                        waiting = false;
                    }

                    _eventProcessor.ReleaseProjection(projection.Name);
                }

                if (!waiting)
                {
                    _threadActivity.Waiting(state);
                }
            }

            _pipelineFactory.ReleasePipeline(pipeline);
        }
        public virtual void Execute(IThreadState state)
        {
            var messagePipeline = _pipelineFactory.GetPipeline <TPipeline>();

            try
            {
                messagePipeline.State.ResetWorking();
                messagePipeline.State.Replace(StateKeys.ActiveState, state);

                messagePipeline.Execute();

                if (messagePipeline.State.GetWorking())
                {
                    _events.OnThreadWorking(this, new ThreadStateEventArgs(typeof(TPipeline)));

                    _threadActivity.Working();
                }
                else
                {
                    _events.OnThreadWaiting(this, new ThreadStateEventArgs(typeof(TPipeline)));

                    _threadActivity.Waiting(state);
                }
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(messagePipeline);
            }
        }
Exemple #8
0
        public TransportMessage Create(object message, Action <TransportMessageConfigurator> configure,
                                       TransportMessage transportMessageReceived)
        {
            Guard.AgainstNull(message, nameof(message));

            var transportMessagePipeline = _pipelineFactory.GetPipeline <TransportMessagePipeline>();

            try
            {
                var transportMessageConfigurator = new TransportMessageConfigurator(message);

                if (transportMessageReceived != null)
                {
                    transportMessageConfigurator.TransportMessageReceived(transportMessageReceived);
                }

                if (configure != null)
                {
                    configure(transportMessageConfigurator);
                }

                if (!transportMessagePipeline.Execute(transportMessageConfigurator))
                {
                    throw new PipelineException(string.Format(EsbResources.PipelineExecutionException,
                                                              "TransportMessagePipeline", transportMessagePipeline.Exception.AllMessages()));
                }

                return(transportMessagePipeline.State.GetTransportMessage());
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(transportMessagePipeline);
            }
        }
Exemple #9
0
        public void Execute(CancellationToken cancellationToken)
        {
            var pipeline = _pipelineFactory.GetPipeline <EventProcessingPipeline>();

            while (!cancellationToken.IsCancellationRequested)
            {
                var projection = _eventProcessor.GetProjection();
                var waiting    = true;

                if (projection != null)
                {
                    pipeline.State.Clear();
                    pipeline.State.SetProjection(projection);
                    pipeline.State.SetCancellationToken(cancellationToken);

                    pipeline.Execute();

                    if (pipeline.State.GetWorking())
                    {
                        _threadActivity.Working();
                        waiting = false;
                    }

                    _eventProcessor.ReleaseProjection(projection);
                }

                if (!waiting)
                {
                    _threadActivity.Waiting(cancellationToken);
                }
            }

            _pipelineFactory.ReleasePipeline(pipeline);
        }
        public void Execute(OnAssembleEventEnvelopes pipelineEvent)
        {
            var state          = pipelineEvent.Pipeline.State;
            var eventStream    = state.GetEventStream();
            var configurator   = state.GetEventEnvelopeConfigurator();
            var eventEnvelopes = new List <EventEnvelope>();

            Guard.AgainstNull(eventStream, nameof(eventStream));
            Guard.AgainstNull(configurator, nameof(configurator));

            var pipeline = _pipelineFactory.GetPipeline <AssembleEventEnvelopePipeline>();

            pipeline.State.SetEventEnvelopeConfigurator(configurator);

            try
            {
                foreach (var appendedEvent in eventStream.GetEvents())
                {
                    eventEnvelopes.Add(pipeline.Execute(appendedEvent));
                }

                state.SetEventEnvelopes(eventEnvelopes);
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(pipeline);
            }

            state.SetEventEnvelopes(eventEnvelopes);
        }
Exemple #11
0
        public virtual void Execute(IThreadState state)
        {
            var availableWorker = _workerAvailabilityManager.GetAvailableWorker();

            if (_configuration.Inbox.Distribute && availableWorker == null)
            {
                _threadActivity.Waiting(state);

                return;
            }

            var messagePipeline = availableWorker == null
                ? _pipelineFactory.GetPipeline <InboxMessagePipeline>()
                : (IPipeline)_pipelineFactory.GetPipeline <DistributorPipeline>();

            try
            {
                messagePipeline.State.SetAvailableWorker(availableWorker);
                messagePipeline.State.ResetWorking();
                messagePipeline.State.SetActiveState(state);

                if (!state.Active)
                {
                    return;
                }

                messagePipeline.Execute();

                if (messagePipeline.State.GetWorking())
                {
                    _events.OnThreadWorking(this, new ThreadStateEventArgs(typeof(InboxMessagePipeline)));

                    _threadActivity.Working();
                }
                else
                {
                    _events.OnThreadWaiting(this, new ThreadStateEventArgs(typeof(InboxMessagePipeline)));

                    _threadActivity.Waiting(state);
                }
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(messagePipeline);
            }
        }
        public void Execute(IThreadState state)
        {
            if (!ShouldProcessDeferred())
            {
                ThreadSleep.While(1000, state);

                return;
            }

            var pipeline = _pipelineFactory.GetPipeline<DeferredMessagePipeline>();

            try
            {
                pipeline.State.ResetWorking();

                pipeline.Execute();

                if (pipeline.State.GetWorking())
                {
                    var transportMessage = pipeline.State.GetTransportMessage();

                    if (transportMessage.IgnoreTillDate < _ignoreTillDate)
                    {
                        _ignoreTillDate = transportMessage.IgnoreTillDate;
                    }

                    if (!_checkpointMessageId.Equals(transportMessage.MessageId))
                    {
                        if (!Guid.Empty.Equals(_checkpointMessageId))
                        {
                            return;
                        }

                        _checkpointMessageId = transportMessage.MessageId;

                        _log.Trace(string.Format(Resources.TraceDeferredCheckpointMessageId,
                            transportMessage.MessageId));

                        return;
                    }
                }

                _nextDeferredProcessDate = _ignoreTillDate;
                _ignoreTillDate = DateTime.MaxValue;
                _checkpointMessageId = Guid.Empty;

                _log.Trace(_nextDeferredProcessDate.Equals(DateTime.MaxValue)
                    ? Resources.TraceDeferredProcessingHalted
                    : string.Format(Resources.TraceDeferredProcessingReset, _nextDeferredProcessDate.ToString("O")));
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(pipeline);
            }
        }
Exemple #13
0
        public EventStream Get(Guid id)
        {
            Guard.AgainstNull(id, nameof(id));

            if (id.Equals(Guid.Empty))
            {
                return(CreateEventStream());
            }

            var pipeline = _pipelineFactory.GetPipeline <GetEventStreamPipeline>();

            try
            {
                return(pipeline.Execute(id));
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(pipeline);
            }
        }
Exemple #14
0
        public void Dispatch(TransportMessage transportMessage)
        {
            Guard.AgainstNull(transportMessage, nameof(transportMessage));

            var messagePipeline = _pipelineFactory.GetPipeline <DispatchTransportMessagePipeline>();

            try
            {
                messagePipeline.Execute(transportMessage, _transportMessageReceived);
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(messagePipeline);
            }
        }
Exemple #15
0
        public void Execute(CancellationToken cancellationToken)
        {
            var pipeline = _pipelineFactory.GetPipeline <ServerPipeline>();

            try
            {
                pipeline.State.ResetWorking();
                pipeline.State.Replace(Pipelines.StateKeys.CancellationToken, cancellationToken);

                pipeline.Execute();
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(pipeline);
            }
        }
        public void Execute(OnSendDeferred pipelineEvent)
        {
            var state = pipelineEvent.Pipeline.State;

            if (state.GetProcessingStatus() == ProcessingStatus.Ignore)
            {
                return;
            }

            var transportMessage = state.GetTransportMessage();

            try
            {
                foreach (var stream in _idempotenceService.GetDeferredMessages(transportMessage))
                {
                    var deferredTransportMessage =
                        (TransportMessage)_serializer.Deserialize(typeof(TransportMessage), stream);

                    var messagePipeline = _pipelineFactory.GetPipeline <DispatchTransportMessagePipeline>();

                    try
                    {
                        messagePipeline.Execute(deferredTransportMessage, null);
                    }
                    finally
                    {
                        _pipelineFactory.ReleasePipeline(messagePipeline);
                    }

                    _idempotenceService.DeferredMessageSent(transportMessage, deferredTransportMessage);
                }
            }
            catch (Exception ex)
            {
                _idempotenceService.AccessException(_log, ex, pipelineEvent.Pipeline);
            }
        }
Exemple #17
0
        public void Execute(CancellationToken cancellationToken)
        {
            if (!ShouldProcessDeferred())
            {
                ThreadSleep.While(1000, cancellationToken);

                return;
            }

            var pipeline = _pipelineFactory.GetPipeline <DeferredMessagePipeline>();

            try
            {
                pipeline.State.ResetWorking();
                pipeline.State.SetDeferredMessageReturned(false);
                pipeline.State.SetTransportMessage(null);

                pipeline.Execute();

                var transportMessage = pipeline.State.GetTransportMessage();

                if (pipeline.State.GetDeferredMessageReturned())
                {
                    if (transportMessage != null &&
                        transportMessage.MessageId.Equals(_checkpointMessageId))
                    {
                        _checkpointMessageId = Guid.Empty;
                    }

                    return;
                }

                if (pipeline.State.GetWorking() && transportMessage != null)
                {
                    if (transportMessage.IgnoreTillDate < _ignoreTillDate)
                    {
                        _ignoreTillDate = transportMessage.IgnoreTillDate;
                    }

                    if (!_checkpointMessageId.Equals(transportMessage.MessageId))
                    {
                        if (!_checkpointMessageId.Equals(Guid.Empty))
                        {
                            return;
                        }

                        _checkpointMessageId = transportMessage.MessageId;

                        _log.Trace(string.Format(Resources.TraceDeferredCheckpointMessageId,
                                                 transportMessage.MessageId));

                        return;
                    }
                }

                lock (_messageDeferredLock)
                {
                    _nextDeferredProcessDate = _ignoreTillDate;
                }

                _ignoreTillDate      = DateTime.MaxValue;
                _checkpointMessageId = Guid.Empty;

                _log.Trace(_nextDeferredProcessDate.Equals(DateTime.MaxValue)
                    ? Resources.TraceDeferredProcessingHalted
                    : string.Format(Resources.TraceDeferredProcessingReset, _nextDeferredProcessDate.ToString("O")));
            }
            finally
            {
                _pipelineFactory.ReleasePipeline(pipeline);
            }
        }