示例#1
0
        public async Task Setup(string endpoint, int readsize, bool extraStats)
        {
            _endpoint   = endpoint;
            _readsize   = readsize;
            _extraStats = extraStats;

            if (_connection.Settings.GossipSeeds == null || !_connection.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(_connection.Settings.Log,
                                                 new IPEndPoint(_connection.Settings.GossipSeeds[0].EndPoint.Address, _connection.Settings.ExternalGossipPort), TimeSpan.FromSeconds(5));

            // We use this system projection - so enable it
            await manager.EnableAsync("$by_event_type", _connection.Settings.DefaultUserCredentials).ConfigureAwait(false);

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

            var stream = $"{_endpoint}.{Assembly.GetEntryAssembly().GetName().Version}";

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

            var definition = $"fromStreams([{eventTypes}]).when(\n{{{functions}}}\n);";

            try
            {
                var existing = await manager.GetQueryAsync(stream).ConfigureAwait(false);

                if (existing != definition)
                {
                    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, definition, _connection.Settings.DefaultUserCredentials)
                    .ConfigureAwait(false);
                }
                catch (ProjectionCommandConflictException)
                {
                }
            }
        }
        static List <Type> GetMessageTypesHandledByThisEndpoint(MessageHandlerRegistry handlerRegistry, Conventions conventions)
        {
            var messageTypesHandled = handlerRegistry.GetMessageTypes()                     //get all potential messages
                                      .Where(t => !conventions.IsInSystemConventionList(t)) //never auto-route system messages
                                      .ToList();

            return(messageTypesHandled);
        }
示例#3
0
 static List <Type> GetHandledEvents(MessageHandlerRegistry handlerRegistry, Conventions conventions)
 {
     // get all potential messages
     return(handlerRegistry.GetMessageTypes()
            // never auto-route system messages and events
            .Where(t => !conventions.IsInSystemConventionList(t) && conventions.IsEventType(t))
            .ToList());
 }
        static List <Type> GetMessageTypesHandledByThisEndpoint(MessageHandlerRegistry handlerRegistry, Conventions conventions, SubscribeSettings settings)
        {
            var messageTypesHandled = handlerRegistry.GetMessageTypes()                                                                                                                 //get all potential messages
                                      .Where(t => !conventions.IsInSystemConventionList(t))                                                                                             //never auto-subscribe system messages
                                      .Where(t => !conventions.IsCommandType(t))                                                                                                        //commands should never be subscribed to
                                      .Where(t => conventions.IsEventType(t))                                                                                                           //only events unless the user asked for all messages
                                      .Where(t => settings.AutoSubscribeSagas || handlerRegistry.GetHandlersFor(t).Any(handler => !typeof(Saga).IsAssignableFrom(handler.HandlerType))) //get messages with other handlers than sagas if needed
                                      .ToList();

            return(messageTypesHandled);
        }
示例#5
0
        static List<Type> GetMessageTypesHandledByThisEndpoint(MessageHandlerRegistry handlerRegistry, Conventions conventions, SubscribeSettings settings)
        {
            var messageTypesHandled = handlerRegistry.GetMessageTypes() //get all potential messages
                .Where(t => !conventions.IsInSystemConventionList(t)) //never auto-subscribe system messages
                .Where(t => !conventions.IsCommandType(t)) //commands should never be subscribed to
                .Where(conventions.IsEventType) //only events unless the user asked for all messages
                .Where(t => settings.AutoSubscribeSagas || handlerRegistry.GetHandlersFor(t).Any(handler => !typeof(Saga).IsAssignableFrom(handler.HandlerType))) //get messages with other handlers than sagas if needed
                .ToList();

            return messageTypesHandled;
        }
示例#6
0
    static List <Type> GetMessageTypesHandled(MessageHandlerRegistry handlerRegistry, Conventions conventions)
    {
        //get all potential messages
        return(handlerRegistry.GetMessageTypes()

               //never auto-subscribe system messages
               .Where(t => !conventions.IsInSystemConventionList(t))

               //commands should never be subscribed to
               .Where(t => !conventions.IsCommandType(t))

               //only events unless the user asked for all messages
               .Where(t => conventions.IsEventType(t))

               .ToList());
    }
 public IEnumerable <Type> GetMessageTypes()
 {
     return(_handlers.GetMessageTypes());
 }
 public Type[] GetHandledTypes()
 {
     return(_handlers.GetMessageTypes().ToArray());
 }
示例#9
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)
                    {
                    }
                }
            }
        }