public Commit Select(Commit committed)
        {
            // return null if the user isn't authorized to see this commit

            var loggedInUser = _container.Resolve<ITenantContext>().GetTenantId();
            var loggedInUserIsAdmin = _container.Resolve<ITenantContext>().IsTenantAdmin();

            if (string.IsNullOrWhiteSpace(loggedInUser))
                return null;

            var tenant = committed.Headers.ContainsKey("Tenant") ?
                committed.Headers["Tenant"].ToString() : null;

            if(string.IsNullOrEmpty(tenant))
            {
                if (loggedInUserIsAdmin)
                    return committed;

                _logger.Warn(string.Format("{0} is trying to access a document with no tenant but is not allowed.", loggedInUser));
                return null;
            }
            else
            {
                if (loggedInUser == tenant)
                    return committed;

                _logger.Warn(string.Format("{0} is trying to access document with tenant id {1} but is not allowed.", loggedInUser, tenant));
                return null;
            }
        }
 public void Dispatch(Commit commit)
 {
     foreach (var eventMessage in commit.Events)
     {
         _domainEventRouter.Dispatch(eventMessage.Body);
     }
 }
 public void PostCommit(Commit committed)
 {
     // 1.0 = 100 %
     // 0.001 = 0.01 %
     if (_random.NextDouble() > 0.001)
         Persistence.MarkCommitAsDispatched(committed);
 }
        public virtual void Commit(Commit attempt)
        {
            if (!attempt.IsValid() || attempt.IsEmpty())
            {
                Logger.Debug(Resources.CommitAttemptFailedIntegrityChecks);
                return;
            }

            foreach (var hook in this.pipelineHooks)
            {
                Logger.Debug(Resources.InvokingPreCommitHooks, attempt.CommitId, hook.GetType());
                if (hook.PreCommit(attempt))
                    continue;

                Logger.Info(Resources.CommitRejectedByPipelineHook, hook.GetType(), attempt.CommitId);
                return;
            }

            Logger.Info(Resources.CommittingAttempt, attempt.CommitId, attempt.Events.Count);
            this.persistence.Commit(attempt);

            foreach (var hook in this.pipelineHooks)
            {
                Logger.Debug(Resources.InvokingPostCommitPipelineHooks, attempt.CommitId, hook.GetType());
                hook.PostCommit(attempt);
            }
        }
        public bool PreCommit(Commit attempt)
        {
            var @event = new PreCommit(attempt.StreamId, attempt.StreamRevision, attempt.Events.Count);

            observer.Notify(@event);

            return true;
        }
 private void DeleteCommit(Commit attempt)
 {
     this.ExecuteCommand<int>(attempt.StreamId, (Func<IDbStatement, int>)(cmd =>
     {
         cmd.AddParameter(this.dialect.CommitId, (object)attempt.CommitId);
         return cmd.ExecuteNonQuery(DeleteStmt);
     }));
 }
 /// <summary>
 /// Dispatches the commit specified to the messaging infrastructure.
 /// </summary>
 /// <param name="commit">The commmit to be dispatched.</param>
 public void Dispatch(Commit commit)
 {
     m_logger.InfoFormat("EventStore: sending events to event queue.");
     foreach (var eventMessage in commit.Events)
     {
         m_bus.Publish(eventMessage.Body);
     }
 }
        private static void DispatchCommit(ILifetimeScope scope, Commit commit)
        {
            var sender = scope.Resolve<IMessageSender>();

            var events = commit.Events.Select(e=>e.Body).ToArray();

            sender.SendAsBatch(events);
        }
        void ICommitEvents.Commit(Commit attempt)
        {
            if (!attempt.IsValid() || attempt.IsEmpty())
                return;

            this.ThrowOnDuplicateOrConcurrentWrites(attempt);
            this.PersistAndDispatch(attempt);
        }
        public Commit Select(Commit committed)
        {
            var @event = new CommitSelected(committed.StreamId, committed.StreamRevision, committed.Events.Count);

            observer.Notify(@event);

            return committed;
        }
 private void AppendToSequence(Commit attempt)
 {
     ExecuteCommand(attempt.StreamId,
                    command =>
                        {
                            command.AddParameter(_dialect.CommitId, attempt.CommitId);
                            command.Execute(_dialect.RegisterSequentialId);
                        });
 }
        public Commit Select(Commit committed)
        {
            var eventConversionRunner = _eventConversionRunnerFactory();

            foreach (var eventMessage in committed.Events)
            {
                eventMessage.Body = eventConversionRunner.Run(eventMessage.Body);
            }

            return committed;
        }
示例#13
0
 public void Dispatch(Commit commit)
 {
     for (var i = 0; i < commit.Events.Count; i++)
     {
         var eventMessage = commit.Events[i];
         _bus.Extensions().Publish(eventMessage.Body, new Dictionary<string, string>
                                                          {
                                                              {"aggregate", commit.StreamId.ToString()},
                                                              {"feed", "realtime"}
                                                          });
     }
 }
 private void UpdateCommit(Commit attempt)
 {
     this.ExecuteCommand<int>(attempt.StreamId, (Func<IDbStatement, int>)(cmd =>
         {
             cmd.AddParameter(this.dialect.StreamId, (object)attempt.StreamId);
             cmd.AddParameter(this.dialect.Items, (object)attempt.Events.Count);
             cmd.AddParameter(this.dialect.CommitSequence, (object)attempt.CommitSequence);
             cmd.AddParameter(this.dialect.Headers, (object)SerializationExtensions.Serialize<Dictionary<string, object>>(this.serializer, attempt.Headers));
             cmd.AddParameter(this.dialect.Payload, (object)SerializationExtensions.Serialize<List<EventMessage>>(this.serializer, attempt.Events));
             return cmd.ExecuteNonQuery(UpdateStmt);
         }));
 }
        public void Pipeline_state_is_persisted()
        {
            string pipelineName = Guid.NewGuid().ToString();
            var emptyState = _sut.GetLastProcessedSequentialNumber(pipelineName);

            emptyState.Should().Be(0);
            var headers = new Dictionary<string, object>();
            var commit = new Commit(Guid.NewGuid(), 1, Guid.NewGuid(), 1, DateTime.UtcNow, headers, new List<EventMessage>(){new EventMessage()});
            _sut.Commit(commit);
            _sut.MarkLastProcessed(pipelineName, commit.StreamId, commit.CommitId);
            var setState = _sut.GetLastProcessedSequentialNumber(pipelineName);
            setState.Should().NotBe(0);
        }
        private void DispatchCommit(Commit commit)
        {
            try
            {
                foreach (var @event in commit.Events)
                    m_eventBus.Publish((IEvent<IIdentity>) ((SavedRecord)@event.Body).Content);

            }
            catch (Exception e)
            {
                m_logger.LogError(e);
            }
        }
 private void AppendToSequence(Commit attempt)
 {
     using (var session = _store.OpenSession())
     {
         session.Store(new RavenCommitSequence
                           {
                               Commit = new RavenCommitReference {Id = attempt.ToRavenCommitId()},
                               Sequence = GetNextSequenceNumber(),
                               Id = GetSequenceId(attempt.CommitId)
                           });
         session.SaveChanges();
     }
 }
        void ICommitEvents.Commit(Commit attempt)
        {
            if (!attempt.IsValid() || attempt.IsEmpty())
                return;

            if (this.pipelineHooks.Any(x => !x.PreCommit(attempt)))
                return;

            this.persistence.Commit(attempt);

            foreach (var hook in this.pipelineHooks)
                hook.PostCommit(attempt);
        }
 public override void Commit(Commit attempt)
 {
     if (attempt.Events.Count == 0)
     {
         DeleteCommit(attempt);
     }
     else
     {
         UpdateCommit(attempt);      
     }
   
     
     
 }
 /// <summary>
 /// dispatches the saved events (in the commit) to the bus
 /// so that they can be proceeded to the corresponding event handler
 /// </summary>
 /// <param name="commit"></param>
 public void Dispatch(Commit commit)
 {
     try
     {
         foreach (var ev in commit.Events.Select(@event => Converter.ChangeTo(@event.Body, @event.Body.GetType())))
         {
             this._bus.RaiseEvent(ev);
         }
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message);
     }
 }
示例#21
0
 public static void DispatchCommit(Commit commit, IServiceBus bus)
 {
     try
     {
         foreach (var @event in commit.Events)
         {
             Console.WriteLine("Notifying of {0}", @event.Body);
             bus.Notify((Event)@event.Body);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
     }
 }
 public void PostCommit(Commit committed)
 {
     foreach (var @event in committed.Events)
     {
         Debug.WriteLine(@event.Body.ToString());
         PersistedDomainEvent evt = new PersistedDomainEvent();
         evt.CommitSequence = committed.CommitSequence;
         evt.Timestamp = committed.CommitStamp;
         evt.EventType = @event.Body.GetType().FullName;
         evt.DomainEvent = (DomainEvent) @event.Body;
         evt.StreamId = committed.StreamId;
         evt.CommitId = committed.CommitId;
         _rawEventStore.SaveEvent(evt);
     }
 }
示例#23
0
 private static void DispatchCommit(Commit commit)
 {
     // This is where we'd hook into our messaging infrastructure, such as NServiceBus,
     // MassTransit, WCF, or some other communications infrastructure.
     // This can be a class as well--just implement IDispatchCommits.
     try
     {
         foreach (var @event in commit.Events)
             Console.WriteLine("*************************");
     }
     catch (Exception)
     {
         Console.WriteLine("*************************");
     }
 }
示例#24
0
 private static void DispatchCommit(Commit commit)
 {
     // This is where we'd hook into our messaging infrastructure, such as NServiceBus,
     // MassTransit, WCF, or some other communications infrastructure.
     // This can be a class as well--just implement IDispatchCommits.
     try
     {
         foreach (EventMessage @event in commit.Events)
             Console.WriteLine(Resources.MessagesDispatched + ((SomeDomainEvent) @event.Body).Value);
     }
     catch (Exception)
     {
         Console.WriteLine(Resources.UnableToDispatch);
     }
 }
        private static void DispatchCommit(ILifetimeScope container, Commit commit)
        {
            using (var scope = container.BeginLifetimeScope())
            {
                NanoMessageBus.IPublishMessages publisher = scope.Resolve<NanoMessageBus.IPublishMessages>();

                publisher.Publish(commit.Events.Select(e => e.Body).ToArray());

                // need to complete and dispose the uow to do the actual publishing since
                // the IHandleUnitOfWork is registered as ExternalyOwned
                using (IHandleUnitOfWork uow = scope.Resolve<IHandleUnitOfWork>())
                {
                    uow.Complete();
                }
            }
        }
示例#26
0
        public void HandleCommit(Commit commit)
        {
            foreach (var eventMessage in commit.Events)
            {
                object @event = eventMessage.Body;

                var openHandlerType = typeof(IEventHandler<>);
                var closedHandlerType = openHandlerType.MakeGenericType(@event.GetType());

                var instances = _container.GetAllInstances(closedHandlerType);

                foreach (dynamic instance in instances)
                {
                    instance.Handle((dynamic)@event);
                }
            }
        }
        private static void DispatchCommit(ILifetimeScope container, Commit commit)
        {
            // TODO: the process might crash after the commit has been writen to the eventstore
            // but before we have a chance to publish the messages to the bus.
            using (var scope = container.BeginLifetimeScope())
            {
                NanoMessageBus.IPublishMessages publisher = scope.Resolve<NanoMessageBus.IPublishMessages>();

                publisher.Publish(commit.Events.Select(e => e.Body).ToArray());

                // need to complete and dispose the uow to do the actual publishing since
                // the IHandleUnitOfWork is registered as ExternalyOwned
                using (IHandleUnitOfWork uow = scope.Resolve<IHandleUnitOfWork>())
                {
                    uow.Complete();
                }
            }
        }
        public virtual bool PreCommit(Commit attempt)
        {
            Logger.Debug(Resources.OptimisticConcurrencyCheck, attempt.StreamId);

            var head = this.GetStreamHead(attempt.StreamId);
            if (head == null)
                return true;

            if (head.CommitSequence >= attempt.CommitSequence)
                throw new ConcurrencyException();

            if (head.StreamRevision >= attempt.StreamRevision)
                throw new ConcurrencyException();

            if (head.CommitSequence < attempt.CommitSequence - 1)
                throw new StorageException(); // beyond the end of the stream

            if (head.StreamRevision < attempt.StreamRevision - attempt.Events.Count)
                throw new StorageException(); // beyond the end of the stream

            Logger.Debug(Resources.NoConflicts, attempt.StreamId);
            return true;
        }
        public virtual bool PreCommit(Commit attempt)
        {
            if (this.Contains(attempt))
                throw new DuplicateCommitException();

            var head = this.GetStreamHead(attempt.StreamId);
            if (head == null)
                return true;

            if (head.CommitSequence >= attempt.CommitSequence)
                throw new ConcurrencyException();

            if (head.StreamRevision >= attempt.StreamRevision)
                throw new ConcurrencyException();

            if (head.CommitSequence < attempt.CommitSequence - 1)
                throw new StorageException(); // beyond the end of the stream

            if (head.StreamRevision < attempt.StreamRevision - attempt.Events.Count)
                throw new StorageException(); // beyond the end of the stream

            return true;
        }
 public Commit Select(Commit committed)
 {
     return committed;
 }