public IDisposable SubscribeToStreamFrom(
     string stream,
     int? lastCheckpoint,
     bool resolveLinkTos,
     Action<Message> eventAppeared,
     Action liveProcessingStarted = null,
     Action<SubscriptionDropReason, Exception> subscriptionDropped = null,
     UserCredentials userCredentials = null,
     int readBatchSize = 500)
 {
     var listener = new AdHocHandler<Message>(eventAppeared);
     _bus.Subscribe<Message>(listener);
     if (stream.StartsWith(EventStoreClientUtils.CategoryStreamNamePrefix)) //Category Stream
     {
         stream = stream.Split('-')[1];
         foreach (var tupple in _history)
         {
             if (tupple.Item1.StartsWith(stream, StringComparison.Ordinal))
                 eventAppeared(tupple.Item2);
         }
         liveProcessingStarted?.Invoke();
     }
     else //standard stream
     {
         foreach (var evnt in _store[stream])
         {
             eventAppeared((Message)DeserializeEvent(evnt.Metadata, evnt.Data));
         }
         liveProcessingStarted?.Invoke();
     }
     return new SubscriptionDisposer(() =>
     {
         _bus.Unsubscribe(listener); return Unit.Default;
     });
 }
示例#2
0
        public async Task Start()
        {
            StartingTime.Start();
            Node.MainBus.Subscribe(
                new AdHocHandler <SystemMessage.BecomeLeader>(m => {
                _started.TrySetResult(true);
            }));

            AdHocHandler <StorageMessage.EventCommitted> waitForAdminUser = null;

            waitForAdminUser = new AdHocHandler <StorageMessage.EventCommitted>(WaitForAdminUser);
            Node.MainBus.Subscribe(waitForAdminUser);

            void WaitForAdminUser(StorageMessage.EventCommitted m)
            {
                if (m.Event.EventStreamId != "$user-admin")
                {
                    return;
                }

                _adminUserCreated.TrySetResult(true);
                Node.MainBus.Unsubscribe(waitForAdminUser);
            }

            await Node.StartAsync(true).WithTimeout(TimeSpan.FromSeconds(60))
            .ConfigureAwait(false);                     //starts the node

            StartingTime.Stop();
            Log.Information("MiniNode successfully started!");
        }
        internal static async Task StartAndWaitUntilInitialized(this ClusterVNode node)
        {
            var tcs = new TaskCompletionSource<int>();
            var handler = new AdHocHandler<UserManagementMessage.UserManagementServiceInitialized>(_ => tcs.TrySetResult(0));
            node.MainBus.Subscribe(handler);

            node.Start();

            await tcs.Task;

            node.MainBus.Unsubscribe(handler);
        } 
示例#4
0
        internal static async Task StartAndWaitUntilInitialized(this ClusterVNode node)
        {
            var tcs     = new TaskCompletionSource <int>();
            var handler = new AdHocHandler <UserManagementMessage.UserManagementServiceInitialized>(_ => tcs.TrySetResult(0));

            node.MainBus.Subscribe(handler);

            node.Start();

            await tcs.Task;

            node.MainBus.Unsubscribe(handler);
        }
        public void publish_publishes_command_as_message()
        {
            var  bus   = new CommandBus("temp");
            long gotIt = 0;
            var  hndl  =
                new AdHocHandler <TestCommands.TestCommand>(
                    cmd => Interlocked.Exchange(ref gotIt, 1));

            bus.Subscribe(hndl);

            bus.Publish(new TestCommands.TestCommand(Guid.NewGuid(), null));

            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotIt) == 1);
        }
示例#6
0
        public void Start()
        {
            StartingTime.Start();

            Node.MainBus.Subscribe(
                new AdHocHandler <SystemMessage.StateChangeMessage>(m => {
                NodeState = _isReadOnlyReplica ? VNodeState.ReadOnlyLeaderless : VNodeState.Unknown;
            }));
            if (!_isReadOnlyReplica)
            {
                Node.MainBus.Subscribe(
                    new AdHocHandler <SystemMessage.BecomeLeader>(m => {
                    NodeState = VNodeState.Leader;
                    _started.TrySetResult(true);
                }));
                Node.MainBus.Subscribe(
                    new AdHocHandler <SystemMessage.BecomeFollower>(m => {
                    NodeState = VNodeState.Follower;
                    _started.TrySetResult(true);
                }));
            }
            else
            {
                Node.MainBus.Subscribe(
                    new AdHocHandler <SystemMessage.BecomeReadOnlyReplica>(m => {
                    NodeState = VNodeState.ReadOnlyReplica;
                    _started.TrySetResult(true);
                }));
            }

            AdHocHandler <StorageMessage.EventCommitted> waitForAdminUser = null;

            waitForAdminUser = new AdHocHandler <StorageMessage.EventCommitted>(WaitForAdminUser);
            Node.MainBus.Subscribe(waitForAdminUser);

            void WaitForAdminUser(StorageMessage.EventCommitted m)
            {
                if (m.Event.EventStreamId != "$user-admin")
                {
                    return;
                }

                _adminUserCreated.TrySetResult(true);
                Node.MainBus.Unsubscribe(waitForAdminUser);
            }

            _host.Start();
            Node.Start();
        }
        public void command_handler_acks_command_message()
        {
            var  bus    = new CommandBus("temp");
            long gotCmd = 0;
            long gotAck = 0;
            var  hndl   =
                new AdHocCommandHandler <TestCommands.TestCommand>(
                    cmd => Interlocked.Exchange(ref gotCmd, 1) == 0);

            bus.Subscribe(hndl);
            var ackHndl =
                new AdHocHandler <AckCommand>(
                    cmd => Interlocked.Exchange(ref gotAck, 1));

            bus.Subscribe(ackHndl);
            bus.Fire(new TestCommands.TestCommand(Guid.NewGuid(), null));
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotAck) == 1);
            Assert.IsOrBecomesTrue(() => Interlocked.Read(ref gotCmd) == 1);
        }