Example #1
0
 public static void List(ProjectionsManager manager, string[] commandArgs)
 {
     if (commandArgs.Length != 1 && commandArgs.Length != 3)
     {
         Log("Invalid argument value for list mode");
         return;
     }
     var userCredentials = commandArgs.Length == 3 ? new UserCredentials(commandArgs[1], commandArgs[2]) : null;
     var mode = commandArgs[0].Trim().ToLower();
     switch (mode)
     {
         case "all":
             Log("Listing all projections...");
             LogUnformatted(manager.ListAllAsync(userCredentials).Result);
             Log("All projections listed");
             break;
         case "onetime":
             Log("Listing onetime projections...");
             LogUnformatted(manager.ListOneTimeAsync(userCredentials).Result);
             Log("Onetime projections listed");
             break;
         case "continuous":
             Log("Listing continuous projections...");
             LogUnformatted(manager.ListContinuousAsync(userCredentials).Result);
             Log("Continuous projections listed");
             break;
         default:
             Log("List mode not recognized");
             break;
     }
 }
Example #2
0
 public static void Disable(ProjectionsManager manager, string[] commandArgs)
 {
     var nameAndCredentials = GetProjectionNameAndCredentials(commandArgs);
     var name = nameAndCredentials.Item1;
     Log("Disabling {0}...", nameAndCredentials.Item1);
     manager.DisableAsync(name, nameAndCredentials.Item2).Wait();
     Log("{0} disabled", name);
 }
        public override void TestFixtureSetUp()
        {
            base.TestFixtureSetUp();
            _credentials = new UserCredentials(SystemUsers.Admin, SystemUsers.DefaultAdminPassword);
            var createdMiniNode = false;
            _timeout = TimeSpan.FromSeconds(10);
            // Check if a node is running in ProjectionsManagerTestSuiteMarkerBase
            if (SetUpFixture.Connection != null && SetUpFixture.Node != null)
            {
                _tag = "_" + (++SetUpFixture.Counter);
                _node = SetUpFixture.Node;
                _connection = SetUpFixture.Connection;
            }
            else
            {
                createdMiniNode = true;
                _tag = "_1";

                _node = CreateNode();
                _node.Start();

                _connection = TestConnection.Create(_node.TcpEndPoint);
                _connection.ConnectAsync().Wait();
            }

            try
            {
                _projManager = new ProjectionsManager(new ConsoleLogger(), _node.ExtHttpEndPoint, _timeout);
                Given();
                When();
            }
            catch
            {
                if (createdMiniNode)
                {
                    if (_connection != null)
                    {
                        try {
                            _connection.Close();
                        } catch { 
                        }
                    }
                    if (_node != null)
                    {
                        try { 
                        	_node.Shutdown();
                        } catch {
                        }
                    }
                }
                throw;
            }
        }
        public override void TestFixtureSetUp()
        {
            base.TestFixtureSetUp();
#if (!DEBUG)
            Assert.Ignore("These tests require DEBUG conditional");
#else 
            QueueStatsCollector.InitializeIdleDetection();
            CreateNode();
            try
            {
                _conn = EventStoreConnection.Create(_node.TcpEndPoint);
                _conn.ConnectAsync().Wait();

                _manager = new ProjectionsManager(
                    new ConsoleLogger(),
                    _node.IntHttpEndPoint,
                    TimeSpan.FromMilliseconds(10000));
                WaitIdle();
                if (GivenStandardProjectionsRunning())
                    EnableStandardProjections();
                QueueStatsCollector.WaitIdle();
                Given();
                When();
            }
            catch
            {
                try
                {
                    if (_conn != null)
                        _conn.Close();
                }
                catch
                {
                }
                try
                {
                    if (_node != null)
                        _node.Shutdown();
                }
                catch
                {
                }

                throw;
            }
#endif
        }
        public void Should_throw_if_http_error()
        {
            var projectionName = Guid.NewGuid().ToString();
            var projectionContent = Guid.NewGuid().ToString();

            var response = new HttpResponse(HttpStatusCode.InternalServerError, String.Empty, String.Empty);

            var httpClient = new FakeHttpClient(u => response, (u, d) => response, (u, d) => response);
            var counter = new CountdownEvent(1);
            httpClient.SetCounter(counter);

            var manager = new ProjectionsManager(EventStoreAddress, httpClient);

            manager.GetConfigAsync(projectionName);

            Assert.IsTrue(counter.Wait(3000));
        }
Example #6
0
        private static bool TryExecute(Dictionary<string, string> config, string[] args)
        {
            try
            {
                Log("Loading config values...");

                var ip = IPAddress.Parse(config["ip"]);
                var port = int.Parse(config["http-port"]);
                var endPoint = new IPEndPoint(ip, port);

                var manager = new ProjectionsManager(new ConsoleLogger(), endPoint, TimeSpan.FromMilliseconds(5000));
                Execute(manager, args);
                return true;
            }
            catch (Exception e)
            {
                Log("padmin execution failed : {0}", e);
                return false;
            }
        }
        public void Should_update_existing_projection()
        {
            var projectionName = Guid.NewGuid().ToString();
            var projectionContent = Guid.NewGuid().ToString();

            var httpClient = new FakeHttpClient(u => new HttpResponse(HttpStatusCode.OK, String.Empty, String.Empty), null, null);
            var manager = new ProjectionsManager(EventStoreAddress, httpClient);

            var counter = new CountdownEvent(1);
            httpClient.SetCounter(counter);

            manager.UpdateProjectionQueryAsync(projectionName, projectionContent);

            Assert.IsTrue(counter.Wait(3000));

            CollectionAssert.IsEmpty(httpClient.PostRequests);
            CollectionAssert.IsEmpty(httpClient.GetRequests);
            CollectionAssert.IsNotEmpty(httpClient.PutRequests);

            StringAssert.Contains(projectionName, httpClient.PutRequests[0].Url);
        }
Example #8
0
        public static void Create(ProjectionsManager manager, string[] commandArgs)
        {
            if (commandArgs.Length < 2)
            {
                Log("Invalid argument value (projection type)");
                return;
            }

            var type = commandArgs[0].Trim().ToLower();
            var queryInfo = GetQueryAndCredentials(commandArgs.Skip(1).ToArray());
            if (queryInfo == null || (type != "onetime" && string.IsNullOrEmpty(queryInfo.Item1)))
            {
                Log("Invalid arguments");
                return;
            }
            var pname = queryInfo.Item1;
            var query = queryInfo.Item2;
            var userCredentials = queryInfo.Item3;

            switch (type)
            {
                case "onetime":
                    Log("Creating onetime projection...");
                    manager.CreateOneTimeAsync(query, userCredentials).Wait();
                    Log("Created");
                    break;
                case "continuous":
                    Log("Creating continuous projection {0}...", pname);
                    manager.CreateContinuousAsync(pname, query, userCredentials).Wait();
                    Log("Created");
                    break;
                default:
                    Log("projection type not recognized");
                    break;
            }
        }
 public JSProjectionsFactory(ILogger <LoggerWrapper> logger)
 {
     Projections       = new Dictionary <string, string>();
     ProjectionManager = new ProjectionsManager(new LoggerWrapper(logger), ESConnectionConfig.HttpEndpoint, TimeSpan.FromSeconds(10));
 }
Example #10
0
        static void Main(string[] args)
        {
            string eventStoreHost     = "asadoDev.internal.noesislabs.com";
            string eventStoreUsername = "******";
            string eventStorePassword = "******";
            int    eventStoreTcpPort  = 1113;
            int    eventStoreHttpPort = 2113;

            var addresses = Dns.GetHostAddressesAsync(eventStoreHost).Result;

            var projectionManager = new ProjectionsManager(new ConsoleLogger(), new IPEndPoint(addresses.First(), eventStoreHttpPort), TimeSpan.FromSeconds(5));

            var testProjectionQuery = String.Format(@"
                    fromAll()
                    .when({{
                        DummyEventA: function(s, e) {{
                            linkTo('{0}', e);
                        }},
                        DummyEventB: function(s, e) {{
                            linkTo('{0}', e);
                        }}
                    }});
                ", PROJECTION_DEST_STREAM_NAME);

            projectionManager.CreateContinuousAsync(TEST_PROJECTION_NAME, testProjectionQuery, new UserCredentials(eventStoreUsername, eventStorePassword)).Wait();

            Thread.Sleep(3000);

            var connectionSettings = ConnectionSettings.Create()
                                     .SetReconnectionDelayTo(TimeSpan.FromSeconds(1))
                                     .KeepReconnecting();

            var uriBuilder = new UriBuilder
            {
                Scheme   = "tcp",
                UserName = eventStoreUsername,
                Password = eventStorePassword,
                Host     = eventStoreHost,
                Port     = eventStoreTcpPort
            };

            var eventStoreConnection = EventStoreConnection.Create(connectionSettings, uriBuilder.Uri);

            eventStoreConnection.ConnectAsync().Wait();

            List <object> events = new List <object>
            {
                new DummyEventA(Guid.NewGuid()),
                new DummyEventA(Guid.NewGuid()),
                new DummyEventA(Guid.NewGuid()),
                new DummyEventA(Guid.NewGuid()),
                new DummyEventA(Guid.NewGuid()),
                new DummyEventB(Guid.NewGuid()),
                new DummyEventB(Guid.NewGuid()),
                new DummyEventB(Guid.NewGuid()),
                new DummyEventB(Guid.NewGuid()),
                new DummyEventB(Guid.NewGuid())
            };

            int expectedVersion = -1;

            foreach (var e in events)
            {
                var eventData = ToEventData(Guid.NewGuid(), e, new Dictionary <string, string>());
                var result    = eventStoreConnection.AppendToStreamAsync(PROJECTION_SOURCE_STREAM_NAME, expectedVersion, new EventData[] { eventData }).Result;
                expectedVersion = result.NextExpectedVersion;
            }
        }
Example #11
0
 public static void UpdateQuery(ProjectionsManager manager, string[] commandArgs)
 {
     var queryInfo = GetQueryAndCredentials(commandArgs);
     if (queryInfo == null)
     {
         Log("Invalid arguments for command update query");
         return;
     }
     var pname = queryInfo.Item1;
     var query = queryInfo.Item2;
     var userCredentials = queryInfo.Item3;
     Log("Updating query of {0}...", pname);
     manager.UpdateQueryAsync(pname, query, userCredentials).Wait();
     Log("Query updated");
 }
Example #12
0
 public static void Delete(ProjectionsManager manager, string[] commandArgs)
 {
     var nameAndCredentials = GetProjectionNameAndCredentials(commandArgs);
     if (nameAndCredentials == null)
     {
         Log("Invalid arguments, should be: <projection-name> [<login> <password>].");
         return;
     }
     var name = nameAndCredentials.Item1;
     Log("Deleting {0}...", name);
     manager.DeleteAsync(name, nameAndCredentials.Item2).Wait();
     Log("{0} deleted", name);
 }
Example #13
0
        private static void Execute(ProjectionsManager manager, string[] args)
        {
            var command = args[0].Trim().ToLower();
            var commandArgs = args.Skip(1).ToArray();

            Action<ProjectionsManager, string[]> executor;

            if(Commands.TryGetValue(command, out executor))
                executor(manager, commandArgs);
            else
                Log("{0} is not a recognized command", args[0]);
        }
Example #14
0
 public DefaultProjectionsManager(ProjectionsManager projectionsManager, UserCredentials userCredentials)
 {
     this.projectionsManager = projectionsManager;
     this.userCredentials    = userCredentials;
 }
        public override void TestFixtureSetUp()
        {
            base.TestFixtureSetUp();
#if (!DEBUG)
            Assert.Ignore("These tests require DEBUG conditional");
#else
            QueueStatsCollector.InitializeIdleDetection();
            CreateNode();
            try
            {
                _conn = EventStoreConnection.Create(_node.TcpEndPoint);
                _conn.ConnectAsync().Wait();

                _manager = new ProjectionsManager(
                    new ConsoleLogger(),
                    _node.ExtHttpEndPoint,
                    TimeSpan.FromMilliseconds(10000));

                _queryManager = new QueryManager(
                    new ConsoleLogger(),
                    _node.ExtHttpEndPoint,
                    TimeSpan.FromMilliseconds(10000),
                    TimeSpan.FromMilliseconds(10000));

                WaitIdle();

                if (GivenStandardProjectionsRunning())
                {
                    EnableStandardProjections();
                }

                QueueStatsCollector.WaitIdle();
                Given();
                When();
            }
            catch
            {
                try
                {
                    if (_conn != null)
                    {
                        _conn.Close();
                    }
                }
                catch
                {
                }
                try
                {
                    if (_node != null)
                    {
                        _node.Shutdown();
                    }
                }
                catch
                {
                }

                throw;
            }
#endif
        }
        public override void TestFixtureSetUp()
        {
            base.TestFixtureSetUp();
#if (!DEBUG)
            throw new NotSupportedException("These tests require DEBUG conditional");
#else
            QueueStatsCollector.InitializeIdleDetection();
            _nodeEndpoints[0] = new Endpoints(
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback));
            _nodeEndpoints[1] = new Endpoints(
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback));
            _nodeEndpoints[2] = new Endpoints(
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback));

            PortsHelper.GetAvailablePort(IPAddress.Loopback);

            _nodes[0] = CreateNode(0,
                _nodeEndpoints[0], new IPEndPoint[] { _nodeEndpoints[1].InternalHttp, _nodeEndpoints[2].InternalHttp });
            _nodes[1] = CreateNode(1,
                _nodeEndpoints[1], new IPEndPoint[] { _nodeEndpoints[0].InternalHttp, _nodeEndpoints[2].InternalHttp });

            _nodes[2] = CreateNode(2,
                _nodeEndpoints[2], new IPEndPoint[] { _nodeEndpoints[0].InternalHttp, _nodeEndpoints[1].InternalHttp });


            _nodes[0].Start();
            _nodes[1].Start();
            _nodes[2].Start();

            WaitHandle.WaitAll(new[] { _nodes[0].StartedEvent, _nodes[1].StartedEvent, _nodes[2].StartedEvent });
            QueueStatsCollector.WaitIdle(waitForNonEmptyTf: true);
            _conn = EventStoreConnection.Create(_nodes[0].ExternalTcpEndPoint);
            _conn.ConnectAsync().Wait();

            _manager = new ProjectionsManager(
                new ConsoleLogger(),
                _nodes[0].ExternalHttpEndPoint,
                TimeSpan.FromMilliseconds(10000));

            if (GivenStandardProjectionsRunning())
                EnableStandardProjections();
            QueueStatsCollector.WaitIdle();
            Given();
            When();
#endif
        }
Example #17
0
        public static async Task <string> CreateProjectionAsync(this IEventStoreConnection connection, ProjectionsManager projectionsManager, string prefix, string streamFilter = null)
        {
            streamFilter = streamFilter ?? ".*";

            var streamName = ParseFilter(prefix, streamFilter);

            if (SubscriptionsCreated.TryAdd(streamName, true))
            {
                var projectionConfig =
                    $@"fromAll()
                        .when({{
                            $any: function (s, e) {{
                                if (e.streamId.indexOf('{prefix}') === 0 && /{streamFilter}/.test(e.streamId.substring({prefix.Length + 1}))) {{
                                    linkTo('{streamName}', e);
                                }}
                            }}
                        }});";

                try
                {
                    var credentials = connection.Settings.DefaultUserCredentials;

                    await projectionsManager.CreateContinuousAsync($"${streamName}", projectionConfig, credentials);
                }
                catch (Exception ex)
                {
                    if (!ex.Is <ProjectionCommandConflictException>())
                    {
                        throw;
                    }
                }
            }

            return(streamName);
        }
Example #18
0
        public void It_can_receive_subscribed_messages()
        {
            var publisher1Address = new Address("pub1", "node1");
            var publisher2Address = new Address("pub2", "node1");

            var projectionsManager = new ProjectionsManager(new NoopLogger(), HttpEndPoint);

            projectionsManager.Enable("$by_category", AdminCredentials);

            var sinkProjectionCreator = new ReceiverSinkProjectionCreator
            {
                ConnectionManager = new DefaultConnectionManager(ConnectionConfiguration)
            };

            sinkProjectionCreator.RegisterProjectionsFor(ReceiverAddress, "");

            var transactionalModeRouterProjectionCreator = new TransactionalModeRouterProjectionCreator()
            {
                ConnectionManager = new DefaultConnectionManager(ConnectionConfiguration)
            };

            transactionalModeRouterProjectionCreator.RegisterProjectionsFor(publisher1Address, "");
            transactionalModeRouterProjectionCreator.RegisterProjectionsFor(publisher2Address, "");

            var subscriptionManager = new SubscriptionManager(new DefaultConnectionManager(ConnectionConfiguration))
            {
                EndpointAddress = ReceiverAddress
            };

            var publisher1 = CreatePublisher(publisher1Address);
            var publisher2 = CreatePublisher(publisher2Address);

            subscriptionManager.Subscribe(typeof(EventA), publisher1.EndpointAddress);

            PublishMessages(publisher1, 1, typeof(EventA));

            if (!ExpectReceive(1, TimeSpan.FromSeconds(5)))
            {
                Assert.Fail("Received {0} messages out of 1", Count);
            }

            subscriptionManager.Subscribe(typeof(EventB), publisher2.EndpointAddress);

            PublishMessages(publisher2, 1, typeof(EventB));

            if (!ExpectReceive(1, TimeSpan.FromSeconds(5)))
            {
                Assert.Fail("Received {0} messages out of 1", Count);
            }

            subscriptionManager.Subscribe(typeof(EventC), publisher2.EndpointAddress);

            PublishMessages(publisher2, 1, typeof(EventC));

            if (!ExpectReceive(1, TimeSpan.FromSeconds(5)))
            {
                Assert.Fail("Received {0} messages out of 1", Count);
            }

            subscriptionManager.Unsubscribe(typeof(EventC), publisher2.EndpointAddress);

            PublishMessages(publisher2, 1, typeof(EventB));
            PublishMessages(publisher2, 1, typeof(EventC));

            if (!ExpectReceive(1, TimeSpan.FromSeconds(5)))
            {
                Assert.Fail("Received {0} messages out of 1", Count);
            }
        }
        public async Task <List <Account> > GetAllAccounts(GetAllAccountsQuery query, CancellationToken cancellationToken)
        {
            var conn = await Connect();

            List <Account>     _accounts = new List <Account>();
            ProjectionsManager pm        = new ProjectionsManager(new ConsoleLogger(), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2113), TimeSpan.FromMilliseconds(5000));

            CreateProjection(pm);

            var streams = await pm.GetStateAsync("getAllStreams", _adminCredentials);

            var streamNames = JsonConvert.DeserializeObject <Streams>(streams);

            foreach (var streamName in streamNames.StreamNames)
            {
                Account _account     = new Account(AccountId.With(streamName));
                var     streamEvents = conn.ReadStreamEventsForwardAsync(streamName, 0, 100, true, _adminCredentials).Result;
                foreach (var evt in streamEvents.Events)
                {
                    var json    = Encoding.UTF8.GetString(evt.Event.Data);
                    var account = JsonConvert.DeserializeObject <AccountReadModel>(json);
                    if (!string.IsNullOrEmpty(account.HolderName))
                    {
                        _account.HolderName = account.HolderName;
                        _account.Balance    = account.Balance;
                    }

                    if (!string.IsNullOrEmpty(account.AccountState))
                    {
                        _account.AccountState = account.AccountState;
                    }

                    if (!account.Amount.Equals(default(float)))
                    {
                        if (evt.Event.EventType.ToLower().Contains("withdraw"))
                        {
                            _account.Balance -= account.Amount;
                        }

                        if (evt.Event.EventType.ToLower().Contains("deposit"))
                        {
                            if (evt.Event.EventType.ToLower().Contains("cash"))
                            {
                                _account.Balance += account.Amount;
                            }

                            if (evt.Event.EventType.ToLower().Contains("check"))
                            {
                                var date        = evt.Event.Created;
                                var depositDate = date.AddDays(1);
                                while (!(depositDate.DayOfWeek >= DayOfWeek.Monday && depositDate.DayOfWeek <= DayOfWeek.Friday))
                                {
                                    depositDate = depositDate.AddDays(1);
                                }
                                if (DateTime.UtcNow.Date >= depositDate.Date)
                                {
                                    _account.Balance += account.Amount;
                                }
                            }
                        }
                    }

                    if (!account.DailyWireTransferLimit.Equals(default(float)))
                    {
                        _account.DailyWireTransferLimit = account.DailyWireTransferLimit;
                    }

                    if (!account.OverDraftLimit.Equals(default(float)))
                    {
                        _account.OverDraftLimit = account.OverDraftLimit;
                    }
                }
                _accounts.Add(_account);
            }
            conn.Close();
            return(_accounts);
        }
Example #20
0
        static void Main(string[] args)
        {
            var es = "localhost:1113";

            if (args.Length > 0)
            {
                es = args[0];
            }
            var uri  = new Uri($"tcp://{es}");
            var conn = EventStoreConnection.Create(GetConnectionBuilder(), uri, "es-test-app");
            var projectionsManager = new ProjectionsManager(new TestLogger(), new IPEndPoint(IPAddress.Loopback, 2113),
                                                            TimeSpan.FromSeconds(5));
            var projectionName            = "ProjectionTest";
            var projectionMultistreamName = "ProjectionMultistreamTest";
            var streamName  = StreamName;
            var stream2Name = "domainTest2Stream";

            try
            {
                conn.Reconnecting  += Conn_Reconnecting;
                conn.ErrorOccurred += Conn_ErrorOccurred;
                conn.ConnectAsync();
                do
                {
                    Console.Clear();
                    Console.WriteLine($"Connected to {uri}");
                    Console.WriteLine($"Press P to create projection {projectionName} ({streamName})");
                    Console.WriteLine($"Press M to create Multi Streams projection {projectionMultistreamName} ({streamName}, {stream2Name})");
                    Console.WriteLine($"Press S to send 1000 events to stream {streamName}");
                    Console.WriteLine($"Press X to send 1000 events to stream {stream2Name}");
                    Console.WriteLine($"Press O to send 1 event to stream {streamName}");
                    Console.WriteLine($"Press D to  Disable projection {projectionName}");
                    Console.WriteLine($"Press F to  Disable Multi Streams projection {projectionMultistreamName}");
                    Console.WriteLine($"Press E to  Enable projection {projectionName}");
                    Console.WriteLine($"Press R to  Enable Multi Streams projection {projectionMultistreamName}");
                    Console.WriteLine($"Press A to set $maxAge=2 to streams {streamName}, {stream2Name}");
                    Console.WriteLine($"Press C to set $maxCount=2 to streams {streamName}, {stream2Name}");
                    var key = Console.ReadKey();
                    if (key.Key == ConsoleKey.O || key.Key == ConsoleKey.NumPad0)
                    {
                        AppendToStreamAsync(conn, streamName, 1);
                    }
                    if (key.Key == ConsoleKey.S)
                    {
                        AppendToStreamAsync(conn, streamName, 1000);
                    }
                    if (key.Key == ConsoleKey.X)
                    {
                        AppendToStreamAsync(conn, stream2Name, 1000);
                    }
                    if (key.Key == ConsoleKey.P)
                    {
                        projectionsManager.CreateContinuousAsync(projectionName,
                                                                 $"fromStream('{streamName}').when({{'$any': function(state, evnt) {{linkTo('OurTargetStreamName', evnt);  }}}});",
                                                                 new UserCredentials("admin", "changeit"));
                    }
                    if (key.Key == ConsoleKey.M)
                    {
                        projectionsManager.CreateContinuousAsync(projectionMultistreamName,
                                                                 $"fromStreams('{streamName}', '{stream2Name}').when({{'$any': function(state, evnt) {{linkTo('OurTargetStreamName', evnt);  }}}});",
                                                                 new UserCredentials("admin", "changeit"));
                    }
                    if (key.Key == ConsoleKey.D)
                    {
                        projectionsManager.DisableAsync(projectionName, new UserCredentials("admin", "changeit")).Wait();
                    }
                    if (key.Key == ConsoleKey.F)
                    {
                        projectionsManager.DisableAsync(projectionMultistreamName, new UserCredentials("admin", "changeit")).Wait();
                    }
                    if (key.Key == ConsoleKey.E)
                    {
                        projectionsManager.EnableAsync(projectionName, new UserCredentials("admin", "changeit")).Wait();
                    }
                    if (key.Key == ConsoleKey.R)
                    {
                        projectionsManager.EnableAsync(projectionMultistreamName, new UserCredentials("admin", "changeit")).Wait();
                    }
                    if (key.Key == ConsoleKey.A)
                    {
                        SetStreamMetadata(conn, streamName, new Dictionary <string, int> {
                            { "$maxAge", 2 }
                        });
                        SetStreamMetadata(conn, stream2Name, new Dictionary <string, int> {
                            { "$maxAge", 2 }
                        });
                    }
                    if (key.Key == ConsoleKey.C)
                    {
                        SetStreamMetadata(conn, streamName, new Dictionary <string, int> {
                            { "$maxCount", 2 }
                        });
                        SetStreamMetadata(conn, stream2Name, new Dictionary <string, int> {
                            { "$maxCount", 2 }
                        });
                    }
                } while (true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }
 public ProjectionSynchronizer()
 {
     _logger             = new ConsoleLogger();
     _projectionsManager = new ProjectionsManager(_logger, new DnsEndPoint("127.0.0.1", 2113), new TimeSpan(0, 1, 0));
 }
Example #22
0
        public static void Status(ProjectionsManager manager, string[] commandArgs)
        {
            var nameAndCredentials = GetProjectionNameAndCredentials(commandArgs);
            if (nameAndCredentials == null)
            {
                Log("Invalid arguments, should be: <projection-name> [<login> <password>].");
                return;
            }

            var name = nameAndCredentials.Item1;
            Log("{0} is '{1}'", name, manager.GetStatusAsync(name, nameAndCredentials.Item2).Result);
        }
Example #23
0
 public ResumeProjection(AreaMap area, ProjectionsManager manager, UserCredentials credentials)
 {
     _area        = area;
     _manager     = manager;
     _credentials = credentials;
 }
Example #24
0
 public EventStore(IEventStoreConnection connection, ProjectionsManager projectionManager)
 {
     this.connection        = connection;
     this.projectionManager = projectionManager;
 }
Example #25
0
 public static void ShowQuery(ProjectionsManager manager, string[] commandArgs)
 {
     var nameAndCredentials = GetProjectionNameAndCredentials(commandArgs);
     if (nameAndCredentials == null)
     {
         Log("Invalid arguments, should be: <projection-name> [<login> <password>].");
         return;
     }
     var name = nameAndCredentials.Item1;
     Log("{0}'s query :", name);
     LogUnformatted(manager.GetQueryAsync(name, nameAndCredentials.Item2).Result);
 }
Example #26
0
        public override async Task TestFixtureSetUp()
        {
            await base.TestFixtureSetUp();

#if (!DEBUG)
            Assert.Ignore("These tests require DEBUG conditional");
#else
            var projectionWorkerThreadCount = GivenWorkerThreadCount();
            var configuration = new ProjectionSubsystemOptions(
                projectionWorkerThreadCount,
                ProjectionType.All,
                false,
                TimeSpan.FromMinutes(Opts.ProjectionsQueryExpiryDefault),
                Opts.FaultOutOfOrderProjectionsDefault,
                500,
                250);
            _projections = new ProjectionsSubsystem(configuration);
            _node        = new MiniNode <TLogFormat, TStreamId>(
                PathName, inMemDb: true,
                subsystems: new ISubsystem[] { _projections });
            _projectionsCreated = SystemProjections.Created(_projections.LeaderMainBus);

            await _node.Start();

            _conn = EventStoreConnection.Create(new ConnectionSettingsBuilder()
                                                .DisableServerCertificateValidation()
                                                .Build(), _node.TcpEndPoint);
            await _conn.ConnectAsync();

            _manager = new ProjectionsManager(
                new ConsoleLogger(),
                _node.HttpEndPoint,
                TimeSpan.FromMilliseconds(20000),
                _node.HttpMessageHandler);

            _queryManager = new QueryManager(
                new ConsoleLogger(),
                _node.HttpEndPoint,
                TimeSpan.FromMilliseconds(20000),
                TimeSpan.FromMilliseconds(20000),
                _node.HttpMessageHandler);

            WaitIdle();

            if (GivenStandardProjectionsRunning())
            {
                await EnableStandardProjections();
            }

            WaitIdle();
            try {
                await Given().WithTimeout(TimeSpan.FromSeconds(10));
            } catch (Exception ex) {
                throw new Exception("Given Failed", ex);
            }

            try {
                await When().WithTimeout(TimeSpan.FromSeconds(10));
            } catch (Exception ex) {
                throw new Exception("When Failed", ex);
            }
#endif
        }
Example #27
0
        public async Task Setup(string endpoint, int readsize, bool extraStats)
        {
            _endpoint   = endpoint;
            _readsize   = readsize;
            _extraStats = extraStats;

            foreach (var client in _clients)
            {
                if (client.Settings.GossipSeeds == null || !client.Settings.GossipSeeds.Any())
                {
                    throw new ArgumentException(
                              "Eventstore connection settings does not contain gossip seeds (even if single host call SetGossipSeedEndPoints and SetClusterGossipPort)");
                }

                var manager = new ProjectionsManager(client.Settings.Log,
                                                     new IPEndPoint(client.Settings.GossipSeeds[0].EndPoint.Address,
                                                                    client.Settings.ExternalGossipPort), TimeSpan.FromSeconds(5));

                await manager.EnableAsync("$by_category", client.Settings.DefaultUserCredentials).ConfigureAwait(false);

                var discoveredEvents =
                    _registry.GetMessageTypes().Where(x => typeof(IEvent).IsAssignableFrom(x)).ToList();

                // Dont use - we dont need category projection projecting our projection
                var stream = $"{_endpoint}.{Assembly.GetEntryAssembly().GetName().Version}".Replace("-", "");

                // Link all events we are subscribing to to a stream
                var functions =
                    discoveredEvents
                    .Select(
                        eventType => $"'{eventType.AssemblyQualifiedName}': processEvent")
                    .Aggregate((cur, next) => $"{cur},\n{next}");

                // Don't tab this '@' will create tabs in projection definition
                var definition = @"
function processEvent(s,e) {{
    linkTo('{1}.{0}', e);
}}
fromCategory('{0}').
when({{
{2}
}});";

                var appDefinition = string.Format(definition, StreamTypes.Domain, stream, functions).Replace(Environment.NewLine, "\n");
                var oobDefinition = string.Format(definition, StreamTypes.OOB, stream, functions).Replace(Environment.NewLine, "\n");

                // Create a projection for domain events and one for OOB events, later we'll subscribe as PINNED to domain events
                // and ROUNDROBIN for OOB events.
                // OOB events by definition don't need ordering, so theres no reason to overload a single PINNED consumer
                // if the user uses a ton of OOB events
                try
                {
                    var existing = await manager.GetQueryAsync($"{stream}.app.projection").ConfigureAwait(false);

                    if (existing != appDefinition)
                    {
                        Logger.Fatal(
                            $"Projection [{stream}] already exists and is a different version!  If you've upgraded your code don't forget to bump your app's version!\nExisting:\n{existing}\nDesired:\n{appDefinition}");
                        throw new EndpointVersionException(
                                  $"Projection [{stream}] already exists and is a different version!  If you've upgraded your code don't forget to bump your app's version!");
                    }
                }
                catch (ProjectionCommandFailedException)
                {
                    try
                    {
                        // Projection doesn't exist
                        await
                        manager.CreateContinuousAsync($"{stream}.app.projection", appDefinition, false,
                                                      client.Settings.DefaultUserCredentials)
                        .ConfigureAwait(false);
                    }
                    catch (ProjectionCommandFailedException)
                    {
                    }
                }
                try
                {
                    var existing = await manager.GetQueryAsync($"{stream}.oob.projection").ConfigureAwait(false);

                    if (existing != oobDefinition)
                    {
                        Logger.Fatal(
                            $"Projection [{stream}] already exists and is a different version!  If you've upgraded your code don't forget to bump your app's version!");
                        throw new EndpointVersionException(
                                  $"Projection [{stream}] already exists and is a different version!  If you've upgraded your code don't forget to bump your app's version!");
                    }
                }
                catch (ProjectionCommandFailedException)
                {
                    try
                    {
                        // Projection doesn't exist
                        await
                        manager.CreateContinuousAsync($"{stream}.oob.projection", oobDefinition, false,
                                                      client.Settings.DefaultUserCredentials)
                        .ConfigureAwait(false);
                    }
                    catch (ProjectionCommandFailedException)
                    {
                    }
                }
            }
        }
Example #28
0
        static void Main(string[] args)
        {
            var ciccio = EmbeddedVNodeBuilder.AsSingleNode().RunProjections(ProjectionType.All).Build();

            ciccio.Start();

            var conn = EventStoreConnection.Create(GetConnectionBuilder(), new Uri($"tcp://localhost:1113"));
            var projectionsManager = new ProjectionsManager(new TestLogger(), new IPEndPoint(IPAddress.Loopback, 2113),
                                                            TimeSpan.FromSeconds(5));
            var projectionName = "LockeshTest";
            var streamName     = "ciccio1";

            try
            {
                conn.Reconnecting  += Conn_Reconnecting;
                conn.ErrorOccurred += Conn_ErrorOccurred;
                conn.ConnectAsync();
                do
                {
                    Console.Clear();
                    Console.WriteLine($"Press P to create projection {projectionName}");
                    Console.WriteLine($"Press S to send 1000 events to stream {streamName}");
                    Console.WriteLine($"Press O to send 1 event to stream {streamName}");
                    Console.WriteLine($"Press D to  Disable projection '{projectionName}'");
                    Console.WriteLine($"Press E to  Enable projection '{projectionName}'");
                    Console.WriteLine($"Press A to set $maxAge=2 to stream {streamName}");
                    Console.WriteLine($"Press C to set $maxCount=2 to stream {streamName}");
                    var key = Console.ReadKey();
                    if (key.Key == ConsoleKey.O || key.Key == ConsoleKey.NumPad0)
                    {
                        AppendToStreamAsync(conn, streamName, 1);
                    }
                    if (key.Key == ConsoleKey.S)
                    {
                        AppendToStreamAsync(conn, streamName, 1000);
                    }
                    if (key.Key == ConsoleKey.P)
                    {
                        projectionsManager.CreateContinuousAsync(projectionName,
                                                                 $"fromStream('{streamName}').when({{'$any': function(state, evnt) {{linkTo('OurTargetStreamName', evnt);  }}}});",
                                                                 new UserCredentials("admin", "changeit"));
                    }
                    if (key.Key == ConsoleKey.D)
                    {
                        projectionsManager.DisableAsync(projectionName, new UserCredentials("admin", "changeit")).Wait();
                    }
                    if (key.Key == ConsoleKey.E)
                    {
                        projectionsManager.EnableAsync(projectionName, new UserCredentials("admin", "changeit")).Wait();
                    }
                    if (key.Key == ConsoleKey.A)
                    {
                        SetStreamMetadata(conn, streamName, new Dictionary <string, int> {
                            { "$maxAge", 2 }
                        });
                    }
                    if (key.Key == ConsoleKey.C)
                    {
                        SetStreamMetadata(conn, streamName, new Dictionary <string, int> {
                            { "$maxCount", 2 }
                        });
                    }
                } while (true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }
        public static void SetupEventStore(StartConflictOption opt = StartConflictOption.Connect)
        {
            //TODO: Convert to Embedded when I can figure out loading the miniWeb component
            var runningEventStores = Process.GetProcessesByName("EventStore.ClusterNode");

            if (runningEventStores.Length != 0)
            {
                switch (opt)
                {
                case StartConflictOption.Connect:
                    _process = runningEventStores[0];
                    break;

                case StartConflictOption.Kill:
                    foreach (var es in runningEventStores)
                    {
                        es.Kill();
                    }
                    break;

                case StartConflictOption.Error:
                    throw new Exception("Conflicting EventStore running.");

                default:
                    throw new ArgumentOutOfRangeException(nameof(opt), opt, null);
                }
            }
            if (_process == null)
            {
                _process = new Process
                {
                    StartInfo =
                    {
                        UseShellExecute = false, CreateNoWindow = true, FileName = Path, Arguments = Args, Verb = "runas"
                    }
                };
                _process.Start();
            }
            var tcp  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1113);
            var http = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2113);

            Connection = EventStoreConnection.Create(tcp);
            Connection.ConnectAsync().Wait();
            var  pManager = new ProjectionsManager(new NullLogger(), http, TimeSpan.FromSeconds(5));
            var  creds    = new UserCredentials("admin", "changeit");
            bool ready    = false;
            int  retry    = 0;

            while (!ready)
            {
                try
                {
                    pManager.EnableAsync("$streams", creds).Wait();
                    pManager.EnableAsync("$by_event_type", creds).Wait();
                    pManager.EnableAsync("$by_category", creds).Wait();
                    pManager.EnableAsync("$stream_by_category", creds).Wait();
                    ready = true;
                }
                catch
                {
                    retry++;
                    if (retry > 8)
                    {
                        throw new Exception("EventStore Projection Start Error.");
                    }
                    System.Threading.Thread.Sleep(250);
                }
            }
        }
Example #30
0
        public override async Task TestFixtureSetUp()
        {
            await base.TestFixtureSetUp();

#if (!DEBUG)
            Assert.Ignore("These tests require DEBUG conditional");
#else
            _nodeEndpoints[0] = new Endpoints(
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback));
            _nodeEndpoints[1] = new Endpoints(
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback));
            _nodeEndpoints[2] = new Endpoints(
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback),
                PortsHelper.GetAvailablePort(IPAddress.Loopback), PortsHelper.GetAvailablePort(IPAddress.Loopback));

            _nodes[0] = CreateNode(0,
                                   _nodeEndpoints[0], new[] { _nodeEndpoints[1].InternalHttp, _nodeEndpoints[2].InternalHttp });
            _nodes[1] = CreateNode(1,
                                   _nodeEndpoints[1], new[] { _nodeEndpoints[0].InternalHttp, _nodeEndpoints[2].InternalHttp });
            _nodes[2] = CreateNode(2,
                                   _nodeEndpoints[2], new[] { _nodeEndpoints[0].InternalHttp, _nodeEndpoints[1].InternalHttp });
            WaitIdle();

            var projectionsStarted = _projections.Select(p => SystemProjections.Created(p.LeaderMainBus)).ToArray();

            foreach (var node in _nodes)
            {
                node.Start();
                node.WaitIdle();
            }

            await Task.WhenAll(_nodes.Select(x => x.Started)).WithTimeout(TimeSpan.FromSeconds(30));

            _conn = EventStoreConnection.Create(_nodes[0].ExternalTcpEndPoint);
            await _conn.ConnectAsync().WithTimeout();

            _manager = new ProjectionsManager(
                new ConsoleLogger(),
                _nodes.Single(x => x.NodeState == VNodeState.Leader).ExternalHttpEndPoint,
                TimeSpan.FromMilliseconds(10000));

            if (GivenStandardProjectionsRunning())
            {
                await Task.WhenAny(projectionsStarted).WithTimeout(TimeSpan.FromSeconds(10));
                await EnableStandardProjections().WithTimeout(TimeSpan.FromMinutes(2));
            }

            WaitIdle();

            try {
                await Given().WithTimeout();
            } catch (Exception ex) {
                throw new Exception("Given Failed", ex);
            }

            try {
                await When().WithTimeout();
            } catch (Exception ex) {
                throw new Exception("When Failed", ex);
            }
#endif
        }
Example #31
0
 /// <summary>
 /// Initializes a new <see cref="ProjectionsInitializationTask"/>
 /// </summary>
 /// <param name="logger">The service used to perform logging</param>
 /// <param name="applicationOptions">The service used to access the current <see cref="Infrastructure.Configuration.ApplicationOptions"/></param>
 /// <param name="startupTaskManager">The service used to manage <see cref="IStartupTask"/>s</param>
 /// <param name="projectionsManager">The service used to manage EventStore projections</param>
 public ProjectionsInitializationTask(ILogger <ProjectionsInitializationTask> logger, IOptions <ApplicationOptions> applicationOptions, IStartupTaskManager startupTaskManager, ProjectionsManager projectionsManager)
     : base(startupTaskManager)
 {
     this.Logger             = logger;
     this.ApplicationOptions = applicationOptions.Value;
     this.ProjectionsManager = projectionsManager;
 }