Ejemplo n.º 1
0
        private void aspnetcore(JasperRegistry parent)
        {
            this.AddSingleton <ConnegRules>();

            this.AddScoped <IHttpContextAccessor>(x => new HttpContextAccessor());
            this.AddSingleton(parent.HttpRoutes.Routes.Router);
            this.AddSingleton(parent.HttpRoutes.Routes);
            ForSingletonOf <IUrlRegistry>().Use(parent.HttpRoutes.Routes.Router.Urls);

            this.AddSingleton <IServiceProviderFactory <IServiceCollection> >(new DefaultServiceProviderFactory());
        }
Ejemplo n.º 2
0
        private static async Task <JasperRuntime> bootstrap(JasperRegistry registry)
        {
            if (registry.Logging.UseConsoleLogging)
            {
                registry.Services.AddTransient <IMessageLogger, ConsoleMessageLogger>();
                registry.Services.AddTransient <ITransportLogger, ConsoleTransportLogger>();
            }


            var timer = new PerfTimer();

            timer.Start("Bootstrapping");

            timer.Record("Finding and Applying Extensions", () => { applyExtensions(registry); });

            timer.Record("Bootstrapping Settings", () => registry.Settings.Bootstrap());


            var handlerCompilation = registry.Bus.CompileHandlers(registry, timer);


            var services = registry.CombinedServices();

            var runtime         = new JasperRuntime(registry, services, timer);
            var featureBuilding = registry.BuildFeatures(runtime, timer);

            await handlerCompilation;
            await registry.Bus.Activate(runtime, registry.Generation, timer);

            await featureBuilding;

            await registry.Startup(runtime);

            // Run environment checks
            timer.Record("Environment Checks", () =>
            {
                var recorder = EnvironmentChecker.ExecuteAll(runtime);
                if (runtime.Get <BusSettings>().ThrowOnValidationErrors)
                {
                    recorder.AssertAllSuccessful();
                }
            });

            timer.MarkStart("Register Node");
            await registerRunningNode(runtime);

            timer.MarkFinished("Register Node");

            timer.Stop();

            return(runtime);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Add Jasper to an ASP.Net Core application with optional configuration to Jasper
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="overrides">Programmatically configure Jasper options</param>
        /// <param name="configure">Programmatically configure Jasper options using the application's IConfiguration and IHostingEnvironment</param>
        /// <returns></returns>
        public static IWebHostBuilder UseJasper(this IWebHostBuilder builder, Action <JasperRegistry> overrides = null, Action <WebHostBuilderContext, JasperOptions> configure = null)
        {
            var registry = new JasperRegistry(builder.GetSetting(WebHostDefaults.ApplicationKey));

            overrides?.Invoke(registry);

            if (configure != null)
            {
                registry.Settings.Messaging(configure);
            }

            return(builder.UseJasper(registry));
        }
Ejemplo n.º 4
0
        public static Assembly DetermineApplicationAssembly(JasperRegistry registry)
        {
            var assembly  = registry.GetType().GetAssembly();
            var isFeature = assembly.GetCustomAttribute <JasperFeatureAttribute>() != null;

            if (!Equals(assembly, typeof(JasperRegistry).GetAssembly()) && !isFeature)
            {
                return(assembly);
            }
            else
            {
                return(CallingAssembly.Find());
            }
        }
Ejemplo n.º 5
0
        private JasperRuntime(JasperRegistry registry, PerfTimer timer)
        {
            Bootstrapping = timer;


            registry.CodeGeneration.Sources.Add(new NowTimeVariableSource());

            registry.CodeGeneration.Assemblies.Add(GetType().GetTypeInfo().Assembly);
            registry.CodeGeneration.Assemblies.Add(registry.ApplicationAssembly);

            Registry = registry;

            _bus = new Lazy <IMessageContext>(Get <IMessageContext>);
        }
Ejemplo n.º 6
0
        private void conneg(JasperRegistry parent)
        {
            this.AddOptions();

            var forwarding = new Forwarders();

            For <Forwarders>().Use(forwarding);

            Scan(_ =>
            {
                _.Assembly(parent.ApplicationAssembly);
                _.AddAllTypesOf <IMessageSerializer>();
                _.AddAllTypesOf <IMessageDeserializer>();
                _.With(new ForwardingRegistration(forwarding));
            });
        }
Ejemplo n.º 7
0
        private static void applyExtensions(JasperRegistry registry)
        {
            var assemblies = FindExtensionAssemblies();

            if (!assemblies.Any())
            {
                return;
            }

            var extensions = assemblies
                             .Select(x => x.GetAttribute <JasperModuleAttribute>().ExtensionType)
                             .Where(x => x != null)
                             .Select(x => TypeExtensions.As <IJasperExtension>(Activator.CreateInstance(x)))
                             .ToArray();

            registry.ApplyExtensions(extensions);
        }
Ejemplo n.º 8
0
        private void messaging(JasperRegistry parent)
        {
            ForSingletonOf <MessagingSerializationGraph>().Use <MessagingSerializationGraph>();

            For <IEnvelopePersistor>().Use <NulloEnvelopePersistor>();
            this.AddSingleton <InMemorySagaPersistor>();

            this.AddSingleton(parent.Messaging.Graph);
            this.AddSingleton <IChannelGraph>(parent.Messaging.Channels);
            this.AddSingleton <ILocalWorkerSender>(parent.Messaging.LocalWorker);

            this.AddSingleton <IRetries, EnvelopeRetries>();

            For <ITransport>()
            .Use <LoopbackTransport>();

            For <ITransport>()
            .Use <TcpTransport>();


            ForSingletonOf <IMessagingRoot>().Use <MessagingRoot>();

            ForSingletonOf <ObjectPoolProvider>().Use(new DefaultObjectPoolProvider());



            MessagingRootService(x => x.Workers);
            MessagingRootService(x => x.Pipeline);

            MessagingRootService(x => x.Router);
            MessagingRootService(x => x.Lookup);
            MessagingRootService(x => x.ScheduledJobs);

            For <IMessageContext>().Use(new MessageContextInstance());

            ForSingletonOf <ITransportLogger>().Use <TransportLogger>();

            ForSingletonOf <INodeDiscovery>().UseIfNone(new InMemoryNodeDiscovery(parent.MessagingSettings));
            ForSingletonOf <ISubscriptionsRepository>().UseIfNone <DefaultSubscriptionsRepository>();


            For <IUriLookup>().Use <ConfigUriLookup>();

            For <IEnvironmentRecorder>().Use <EnvironmentRecorder>();
        }
Ejemplo n.º 9
0
        private static void applyExtensions(JasperRegistry registry)
        {
            var assemblies = AssemblyFinder
                             .FindAssemblies(a => a.HasAttribute <JasperModuleAttribute>())
                             .ToArray();

            if (!assemblies.Any())
            {
                return;
            }

            var extensions = assemblies
                             .Select(x => x.GetAttribute <JasperModuleAttribute>().ExtensionType)
                             .Select(x => Activator.CreateInstance(x).As <IJasperExtension>())
                             .ToArray();

            registry.ApplyExtensions(extensions);
        }
Ejemplo n.º 10
0
        private static async Task applyExtensions(JasperRegistry registry, IEnumerable <Assembly> assemblies)
        {
            if (!assemblies.Any())
            {
                return;
            }

            Func <Type, bool> filter = type => type.CanBeCastTo <IJasperExtension>() && type.IsConcreteWithDefaultCtor();

            var extensionTypes = await TypeRepository.FindTypes(assemblies,
                                                                TypeClassification.Concretes | TypeClassification.Closed, filter).ConfigureAwait(false);

            foreach (var extensionType in extensionTypes)
            {
                var extension = Activator.CreateInstance(extensionType).As <IJasperExtension>();
                extension.Configure(registry);
            }
        }
Ejemplo n.º 11
0
        private static async Task <JasperRuntime> bootstrap(JasperRegistry registry)
        {
            applyExtensions(registry);

            registry.Settings.Bootstrap();

            var features = registry.Features;

            var serviceRegistries = await Task.WhenAll(features.Select(x => x.Bootstrap(registry)))
                                    .ConfigureAwait(false);

            var collections = new List <IServiceCollection>();

            collections.AddRange(serviceRegistries);
            collections.Add(registry.ExtensionServices);
            collections.Add(registry.Services);


            var services = await ServiceCollectionExtensions.Combine(collections.ToArray());

            registry.Generation.ReadServices(services);


            var runtime = new JasperRuntime(registry, services);

            registry.Http.As <IWebHostBuilder>().UseSetting(WebHostDefaults.ApplicationKey, registry.ApplicationAssembly.FullName);

            runtime.HttpAddresses = registry.Http.As <IWebHostBuilder>().GetSetting(WebHostDefaults.ServerUrlsKey);

            await Task.WhenAll(features.Select(x => x.Activate(runtime, registry.Generation)))
            .ConfigureAwait(false);

            // Run environment checks
            var recorder = EnvironmentChecker.ExecuteAll(runtime);

            if (runtime.Get <BusSettings>().ThrowOnValidationErrors)
            {
                recorder.AssertAllSuccessful();
            }

            await registerRunningNode(runtime);

            return(runtime);
        }
Ejemplo n.º 12
0
        private void messaging(JasperRegistry parent)
        {
            this.AddSingleton(parent.Bus.Graph);
            this.AddSingleton <IChannelGraph>(parent.Bus.Channels);
            this.AddSingleton <ILocalWorkerSender>(parent.Bus.LocalWorker);


            For <ITransport>()
            .Use <LoopbackTransport>();

            For <ITransport>()
            .Use <TcpTransport>();



            ForSingletonOf <MessagingRoot>().Use <MessagingRoot>();

            ForSingletonOf <ObjectPoolProvider>().Use(new DefaultObjectPoolProvider());

            this.AddSingleton <IWorkerQueue>(s => s.GetService <MessagingRoot>().Workers);
            this.AddSingleton(s => s.GetService <MessagingRoot>().Pipeline);
            this.AddSingleton(s => s.GetService <MessagingRoot>().Serialization);
            this.AddTransient(s => s.GetService <MessagingRoot>().Build());
            this.AddSingleton(s => s.GetService <MessagingRoot>().Logger);
            this.AddSingleton(s => s.GetService <MessagingRoot>().Router);
            this.AddSingleton(s => s.GetService <MessagingRoot>().Lookup);
            this.AddSingleton(s => s.GetService <MessagingRoot>().ScheduledJobs);


            ForSingletonOf <CompositeTransportLogger>().Use <CompositeTransportLogger>();

            ForSingletonOf <INodeDiscovery>().UseIfNone(new InMemoryNodeDiscovery(parent.BusSettings));
            ForSingletonOf <ISubscriptionsRepository>().UseIfNone(new InMemorySubscriptionsRepository());


            For <IUriLookup>().Use <ConfigUriLookup>();


            For <IPersistence>().Use <NulloPersistence>();

            For <IEnvironmentRecorder>().Use <EnvironmentRecorder>();
        }
Ejemplo n.º 13
0
        private JasperRuntime(JasperRegistry registry, Registry[] serviceRegistries)
        {
            Container = new Container(_ =>
            {
                _.AddRegistry(registry.Services);
                foreach (var serviceRegistry in serviceRegistries)
                {
                    _.AddRegistry(serviceRegistry);
                }
            })
            {
                DisposalLock = DisposalLock.Ignore
            };

            registry.Generation.Sources.Add(new StructureMapServices(Container));
            registry.Generation.Assemblies.Add(GetType().GetTypeInfo().Assembly);
            registry.Generation.Assemblies.Add(registry.ApplicationAssembly);

            _registry = registry;
        }
Ejemplo n.º 14
0
        public JasperServiceRegistry(JasperRegistry parent)
        {
            For <IMetrics>().Use <NulloMetrics>();
            For <IHostedService>().Use <MetricsCollector>();

            this.AddLogging();

            For <IMessageLogger>().Use <MessageLogger>().Singleton();
            For <ITransportLogger>().Use <TransportLogger>().Singleton();


            this.AddSingleton(parent.CodeGeneration);

            For <IHostedService>().Use <NodeRegistration>();
            For <IHostedService>().Use <BackPressureAgent>();

            conneg(parent);
            messaging(parent);

            aspnetcore(parent);
        }
Ejemplo n.º 15
0
        private static async Task <JasperRuntime> bootstrap(JasperRegistry registry)
        {
            /*
             * Questions:
             * Pre-load settings that we know we're looking for?
             *
             *
             * 1. Look for extensions
             * 2. Apply all extensions
             * 3. Do all the necessary Settings stuff
             * 4. Activate each feature in parallel
             * 5. Apply services
             * 6. Activate each in parallel
             *
             *
             *
             */

            var assemblies = AssemblyFinder.FindAssemblies(a => a.HasAttribute <JasperModuleAttribute>());


            await applyExtensions(registry, assemblies).ConfigureAwait(false);

            registry.Settings.Bootstrap();

            var features = registry.Features;

            var serviceRegistries = await Task.WhenAll(features.Select(x => x.Bootstrap(registry)))
                                    .ConfigureAwait(false);

            var runtime = new JasperRuntime(registry, serviceRegistries);


            await Task.WhenAll(features.Select(x => x.Activate(runtime, registry.Generation)))
            .ConfigureAwait(false);

            return(runtime);
        }
Ejemplo n.º 16
0
        private JasperRuntime(JasperRegistry registry, IServiceCollection services)
        {
            services.AddSingleton(this);

            Services = services.ToImmutableArray();

            Container = new Container(_ =>
            {
                _.Populate(services);
            })
            {
                DisposalLock = DisposalLock.Ignore
            };

            registry.Generation.Sources.Add(new NowTimeVariableSource());

            registry.Generation.Assemblies.Add(GetType().GetTypeInfo().Assembly);
            registry.Generation.Assemblies.Add(registry.ApplicationAssembly);

            _registry = registry;

            _bus = new Lazy <IServiceBus>(Get <IServiceBus>);
        }
Ejemplo n.º 17
0
        private void messaging(JasperRegistry parent)
        {
            ForSingletonOf <MessagingSerializationGraph>().Use <MessagingSerializationGraph>();

            For <IEnvelopePersistor>().Use <NulloEnvelopePersistor>();
            this.AddSingleton <InMemorySagaPersistor>();

            this.AddSingleton(parent.Messaging.Graph);
            this.AddSingleton <ISubscriberGraph>(parent.Messaging.Subscribers);
            this.AddSingleton <ILocalWorkerSender>(parent.Messaging.LocalWorker);

            this.AddSingleton <IRetries, EnvelopeRetries>();

            For <ITransport>()
            .Use <LoopbackTransport>();

            For <ITransport>()
            .Use <TcpTransport>();


            ForSingletonOf <IMessagingRoot>().Use <MessagingRoot>();

            ForSingletonOf <ObjectPoolProvider>().Use(new DefaultObjectPoolProvider());


            MessagingRootService(x => x.Workers);
            MessagingRootService(x => x.Pipeline);

            MessagingRootService(x => x.Router);
            MessagingRootService(x => x.ScheduledJobs);

            For <IMessageContext>().Use(new MessageContextInstance());

            ForSingletonOf <ITransportLogger>().Use <TransportLogger>();

            For <IEnvironmentRecorder>().Use <EnvironmentRecorder>();
        }
Ejemplo n.º 18
0
        private static async Task <JasperRuntime> bootstrap(JasperRegistry registry)
        {
            applyExtensions(registry);

            registry.Settings.Bootstrap();

            var features = registry.Features;

            var serviceRegistries = await Task.WhenAll(features.Select(x => x.Bootstrap(registry)))
                                    .ConfigureAwait(false);

            var collections = new List <IServiceCollection>();

            collections.AddRange(serviceRegistries);
            collections.Add(registry.ExtensionServices);
            collections.Add(registry.Services);


            var services = await ServiceCollectionExtensions.Combine(collections.ToArray());

            registry.Generation.ReadServices(services);


            var runtime = new JasperRuntime(registry, services);


            await Task.WhenAll(features.Select(x => x.Activate(runtime, registry.Generation)))
            .ConfigureAwait(false);

            // TODO -- THIS IS TEMPORARY UNTIL WE DO GH-212
            var hostedServices = runtime.Container.GetAllInstances <IHostedService>()
                                 .Select(x => x.StartAsync(CancellationToken.None));

            await Task.WhenAll(hostedServices);

            return(runtime);
        }
Ejemplo n.º 19
0
        private JasperRuntime(JasperRegistry registry, IServiceCollection services, PerfTimer timer)
        {
            Bootstrapping = timer;

            services.AddSingleton(this);

            timer.Record("new Container()", () =>
            {
                Container = new Container(services, timer)
                {
                    DisposalLock = DisposalLock.Ignore
                };
            });


            registry.Generation.Sources.Add(new NowTimeVariableSource());

            registry.Generation.Assemblies.Add(GetType().GetTypeInfo().Assembly);
            registry.Generation.Assemblies.Add(registry.ApplicationAssembly);

            Registry = registry;

            _bus = new Lazy <IServiceBus>(Get <IServiceBus>);
        }
Ejemplo n.º 20
0
 /// <summary>
 ///     Builds and initializes a JasperRuntime for the registry
 /// </summary>
 /// <param name="registry"></param>
 /// <returns></returns>
 public static Task <JasperRuntime> ForAsync(JasperRegistry registry)
 {
     return(bootstrap(registry));
 }
Ejemplo n.º 21
0
 public static JasperRuntime For(JasperRegistry registry)
 {
     return(bootstrap(registry).GetAwaiter().GetResult());
 }
Ejemplo n.º 22
0
        private static async Task <JasperRuntime> bootstrap(JasperRegistry registry)
        {
            var timer = new PerfTimer();

            timer.Start("Bootstrapping");

            timer.Record("Finding and Applying Extensions", () =>
            {
                applyExtensions(registry);
            });

            var buildingServices = Task.Factory.StartNew(() =>
            {
                return(timer.Record("Combining Services and Building Settings", registry.CompileConfigurationAndServicesForIdiomaticBootstrapping));
            });



            var handlerCompilation = registry.Messaging.CompileHandlers(registry, timer);


            var runtime = new JasperRuntime(registry, timer);

            var services = await buildingServices;

            services.AddSingleton(runtime);


            var container = await Lamar.Container.BuildAsync(services, timer);

            container.DisposalLock = DisposalLock.Ignore;
            runtime.Container      = container;


            var routeDiscovery = registry.HttpRoutes.Enabled
                ? registry.HttpRoutes.FindRoutes(runtime, registry, timer)
                : Task.CompletedTask;

            runtime.buildAspNetCoreServer(services);

            await routeDiscovery;

            await Task.WhenAll(runtime.startAspNetCoreServer(), handlerCompilation, runtime.startHostedServices());


            // Run environment checks
            timer.Record("Environment Checks", () =>
            {
                var recorder = EnvironmentChecker.ExecuteAll(runtime);
                if (runtime.Settings.ThrowOnValidationErrors)
                {
                    recorder.AssertAllSuccessful();
                }
            });

            _lifetime = TypeExtensions.As <ApplicationLifetime>(container.GetInstance <IApplicationLifetime>());
            _lifetime.NotifyStarted();

            timer.Stop();

            return(runtime);
        }
Ejemplo n.º 23
0
 public Logging(JasperRegistry parent)
 {
     _parent = parent;
 }
Ejemplo n.º 24
0
 /// <summary>
 ///     Builds and initializes a JasperRuntime for the registry
 /// </summary>
 /// <param name="registry"></param>
 /// <returns></returns>
 public static JasperRuntime For(JasperRegistry registry)
 {
     return(ForAsync(registry).GetAwaiter().GetResult());
 }