Exemple #1
0
        public async Task <int> QueueDispatchesInternalEvents()
        {
            var services = new ServiceCollection();

            services.AddEvents();
            services.AddTransient <QueueConsumationStartedListener>();
            var provider = services.BuildServiceProvider();

            IEventRegistration registration = provider.ConfigureEvents();

            registration
            .Register <QueueConsumationStarted>()
            .Subscribe <QueueConsumationStartedListener>();


            int successfulTasks = 0;

            Queue queue = new Queue(provider.GetService <IServiceScopeFactory>(), provider.GetService <IDispatcher>());

            queue.QueueTask(() => successfulTasks++);
            queue.QueueTask(() => successfulTasks++);
            queue.QueueTask(() => successfulTasks++);

            await queue.ConsumeQueueAsync();

            // Should not throw due to null Dispatcher


            Assert.True(QueueConsumationStartedListener.Ran);

            return(successfulTasks); // Avoids "unused variable" warning ;)
        }
Exemple #2
0
        public async Task <bool> SchedulerDispatchesEvents()
        {
            var services = new ServiceCollection();

            services.AddEvents();
            services.AddTransient <ScheduledEventStartedListener>();
            var provider = services.BuildServiceProvider();

            IEventRegistration registration = provider.ConfigureEvents();

            registration
            .Register <ScheduledEventStarted>()
            .Subscribe <ScheduledEventStartedListener>();

            var  scheduler = new Scheduler(new InMemoryMutex(), provider.GetRequiredService <IServiceScopeFactory>(), provider.GetRequiredService <IDispatcher>());
            bool dummy     = true;

            scheduler.Schedule(() => dummy = true)
            .EveryMinute();

            await scheduler.RunAtAsync(DateTime.Parse("2018/06/07"));

            Assert.True(ScheduledEventStartedListener.Ran);

            return(dummy); // Avoids "unused variable" warning ;)
        }
Exemple #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            IEventRegistration registration = app.ApplicationServices.ConfigureEvents();

            registration.Register <DemoEvent>()
            .Subscribe <WriteMessageToConsoleListener>()
            .Subscribe <WriteStaticMessageToConsoleListener>();

            //添加定时任务1:每秒打印
            app.ApplicationServices.UseScheduler(scheduler =>
            {
                scheduler.Schedule(() => Console.WriteLine($"Runs every minute Ran at: {DateTime.UtcNow}")).Cron("* * * * *");//.EverySecond();
            });

            //添加队列

            app.ApplicationServices.ConfigureQueue().LogQueuedTaskProgress(app.ApplicationServices.GetService <ILogger <IQueue> >());
        }
Exemple #4
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            IEventRegistration registration = app.ApplicationServices.ConfigureEvents();

            registration.Register <DemoEvent>()
            .Subscribe <Listener1>()
            .Subscribe <Listener2>();
        }
Exemple #5
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            IEventRegistration registration = app.ApplicationServices.ConfigureEvents();

            registration.Register <DemoEvent>()
            .Subscribe <WriteMessageToConsoleListener>()
            .Subscribe <WriteStaticMessageToConsoleListener>();

            app.ApplicationServices.UseScheduler(scheduler =>
            {
                // scheduler.Schedule(() => Console.WriteLine($"Every minute (ran at ${DateTime.UtcNow}) on thread {Thread.CurrentThread.ManagedThreadId}"))
                //     .EveryMinute();

                // scheduler.Schedule(() => Console.WriteLine($"Every minute#2 (ran at ${DateTime.UtcNow}) on thread {Thread.CurrentThread.ManagedThreadId}"))
                //     .EveryMinute();

                // scheduler.ScheduleAsync(async () =>
                // {
                //     await Task.Delay(10000);
                //     Console.WriteLine($"Every minute#3 (ran at ${DateTime.UtcNow}) on thread {Thread.CurrentThread.ManagedThreadId}");
                // })
                // .EveryMinute();

                // scheduler.Schedule(() => Console.WriteLine($"Every five minutes (ran at ${DateTime.UtcNow}) on thread {Thread.CurrentThread.ManagedThreadId}"))
                //     .EveryFiveMinutes();

                // scheduler.Schedule<SendNightlyReportsEmailJob>()
                //     .Cron("* * * * *")
                //     .PreventOverlapping("SendNightlyReportsEmailJob");

                //     scheduler.ScheduleAsync(async () => {
                //         var dispatcher = app.ApplicationServices.GetRequiredService<IDispatcher>();
                //         await dispatcher.Broadcast(new DemoEvent("This event happens every minute!"));
                //     }).EveryMinute();

                scheduler.OnWorker("EmailTasks");
                scheduler
                .Schedule <SendNightlyReportsEmailJob>().Daily();
                scheduler
                .Schedule <SendPendingNotifications>().EveryMinute();

                scheduler.OnWorker("CPUIntensiveTasks");
                scheduler
                .Schedule <RebuildStaticCachedData>().Hourly();
            });

            app.ApplicationServices
            .ConfigureQueue()
            .LogQueuedTaskProgress(app.ApplicationServices.GetService <ILogger <IQueue> >());

            app.ApplicationServices.ConfigureQueue()
            .LogQueuedTaskProgress(app.ApplicationServices.GetService <ILogger <IQueue> >());
        }