Beispiel #1
0
        public void Start(IAppBuilder app)
        {
            WarmupPlugins();


            _log.Debug("Starting ...");
            try
            {
                _compositionRoot.Build(registrar =>
                {
                    LetPluginsRegisterServices(registrar);

                    registrar.RegisterService(x => _cqsBuilder.CreateCommandBus(Container), Lifetime.Singleton);
                    registrar.RegisterService(x => _cqsBuilder.CreateQueryBus(Container), Lifetime.Singleton);
                    registrar.RegisterService(x => _cqsBuilder.CreateEventBus(Container), Lifetime.Singleton);

                    // let us guard it since it runs events in the background.
                    var service = registrar.Registrations.First(x => x.Implements(typeof(IEventBus)));
                    service.AddService(typeof(IApplicationService));

                    registrar.RegisterService(x => _cqsBuilder.CreateRequestReplyBus(Container), Lifetime.Singleton);
                }, Startup.ConfigurationStore);

                BuildServices();
                _appManager.Start();
                _backgroundJobManager.Start();
                _log.Debug("...started");
            }
            catch (Exception exception)
            {
                _log.Error("Failed to start.", exception);
                throw;
            }
        }
Beispiel #2
0
        public void BackgroundJobManagerTest()
        {
            IIocManager iocResolver = new IocManager();

            // 注册服务
            iocResolver.Register <SimpleSendEmailJob>(DependencyLifeStyle.Transient);
            IBackgroundJobStore   jobStore = new InMemoryBackgroundJobStore();
            IBackgroundJobManager _backgroundJobManager = new BackgroundJobManager(iocResolver, jobStore, new Enterprises.Framework.Threading.AbpTimer());

            _backgroundJobManager.Enqueue <SimpleSendEmailJob, SimpleSendEmailJobArgs>(
                new SimpleSendEmailJobArgs
            {
                Subject      = "邮件主题",
                Body         = "测试邮件",
                SenderUserId = 1000,
                TargetUserId = 2000
            });


            _backgroundJobManager.Enqueue <SimpleSendEmailJob, SimpleSendEmailJobArgs>(
                new SimpleSendEmailJobArgs
            {
                Subject      = "邮件主题2",
                Body         = "测试邮件2",
                SenderUserId = 1000,
                TargetUserId = 2000
            }, BackgroundJobPriority.Normal, TimeSpan.FromSeconds(10));

            _backgroundJobManager.Start();

            Console.WriteLine($"datetime={DateTime.Now} end send Email ");
        }
        public void Start()
        {
            _log.Debug("Starting ...");
            try
            {
                _compositionRoot.Build(registrar =>
                {
                    registrar.RegisterService(x => _cqsBuilder.CreateCommandBus(Container), Lifetime.Singleton);
                    registrar.RegisterService(x => _cqsBuilder.CreateQueryBus(Container), Lifetime.Singleton);
                    registrar.RegisterService(x => _cqsBuilder.CreateEventBus(Container), Lifetime.Singleton);
                    registrar.RegisterService(x => _cqsBuilder.CreateRequestReplyBus(Container), Lifetime.Singleton);
                });
                BuildServices();
                ConfigureDataMapper();

                _appManager.Start();
                _backgroundJobManager.Start();
                _log.Debug("...started");
            }
            catch (Exception exception)
            {
                _log.Error("Failed to start.", exception);
                throw;
            }
        }
        public void Start(StartContext context)
        {
            var adapter = new DependencyInjectionAdapter(context.ServiceProvider);

            _backgroundJobManager = new BackgroundJobManager(adapter);
            _backgroundJobManager.ExecuteSequentially = true;
            _backgroundJobManager.JobFailed          += OnBackgroundJobFailed;
            _backgroundJobManager.StartInterval       = TimeSpan.FromSeconds(Debugger.IsAttached ? 0 : 10);
            _backgroundJobManager.ExecuteInterval     = TimeSpan.FromSeconds(Debugger.IsAttached ? 0 : 30);
            _backgroundJobManager.ScopeClosing       += OnBackgroundJobScopeClosing;
            _backgroundJobManager.Start();
        }
        public void Start(IAppBuilder app)
        {
            WarmupPlugins();


            _log.Debug("Starting ...");
            try
            {
                _compositionRoot.Build(registrar =>
                {
                    LetPluginsRegisterServices(registrar);

                    registrar.RegisterService <IMessageBus>(x =>
                    {
                        var queue = x.Resolve <IMessageQueueProvider>().Open("Messaging");
                        var bus   = new SingleInstanceMessageBus(queue);
                        return(bus);
                    }, Lifetime.Singleton);
                    registrar.RegisterConcrete <ScopedQueryBus>();
                    registrar.RegisterService(CreateMessageInvoker, Lifetime.Scoped);

                    registrar.RegisterService(x => CreateQueueListener(x, "Messaging", "Messaging"), Lifetime.Singleton);
                    registrar.RegisterService(x => CreateQueueListener(x, "Reports", "Messaging"), Lifetime.Singleton);
                    registrar.RegisterService(x => CreateQueueListener(x, "Feedback", "Messaging"), Lifetime.Singleton);
                    registrar.RegisterInstance <IMessageQueueProvider>(_queueProvider);

                    // let us guard it since it runs events in the background.
                    //var service = registrar.Registrations.First(x => x.Implements(typeof(IMessageBus)));
                    //service.AddService(typeof(IApplicationService));
                }, Startup.ConfigurationStore);

                var listeners = Container.ResolveAll <QueueListener>().ToArray();
                listeners
                .ForEach(x => x
                         .RunAsync(_shutdownToken.Token)
                         .ContinueWith(OnStopped)
                         );

                BuildServices();
                _appManager.Start();
                _backgroundJobManager.Start();
                _log.Debug("...started");
            }
            catch (Exception exception)
            {
                _log.Error("Failed to start.", exception);
                throw;
            }
        }
        public void execute_found_job()
        {
            var sl    = Substitute.For <IContainer>();
            var scope = Substitute.For <IContainerScope>();
            var job   = Substitute.For <IBackgroundJob>();

            sl.CreateScope().Returns(scope);
            scope.Resolve(job.GetType()).Returns(job);
            scope.ResolveAll <IBackgroundJob>().Returns(new[] { job });

            var sut = new BackgroundJobManager(sl);

            sut.StartInterval = TimeSpan.FromSeconds(0);
            sut.Start();
            Thread.Sleep(100);

            job.Received().Execute();
        }
        public void trigger_ScopeClosed_before_closing_it_even_if_job_execution_fails()
        {
            var sl    = Substitute.For <IContainer>();
            var scope = Substitute.For <IContainerScope>();
            var job   = Substitute.For <IBackgroundJob>();

            sl.CreateScope().Returns(scope);
            scope.Resolve(job.GetType()).Returns(job);
            scope.ResolveAll <IBackgroundJob>().Returns(new[] { job });

            var sut = new BackgroundJobManager(sl);

            sut.StartInterval = TimeSpan.FromSeconds(0);
            sut.Start();
            Thread.Sleep(100);

            job.Received().Execute();
        }
        public void trigger_ScopeClosed_before_closing_it_after_job_Execution()
        {
            var sl    = Substitute.For <IContainer>();
            var job   = Substitute.For <IBackgroundJob>();
            var scope = Substitute.For <IContainerScope>();

            sl.CreateScope().Returns(scope);
            scope.Resolve(job.GetType()).Returns(job);
            scope.ResolveAll <IBackgroundJob>().Returns(new[] { job });
            ScopeClosingEventArgs actual = null;

            var sut = new BackgroundJobManager(sl);

            sut.StartInterval = TimeSpan.FromSeconds(0);
            sut.ScopeClosing += (o, e) => actual = e;
            sut.Start();
            Thread.Sleep(100);

            actual.Should().NotBeNull();
        }
        public void run_next_async_job_even_if_first_fails()
        {
            var sl    = Substitute.For <IContainer>();
            var scope = Substitute.For <IContainerScope>();
            var job   = Substitute.For <IBackgroundJobAsync>();
            var job2  = Substitute.For <IBackgroundJobAsync>();

            job.When(x => x.ExecuteAsync()).Do(x => { throw new SqlNullValueException(); });
            sl.CreateScope().Returns(scope);
            scope.Resolve(job.GetType()).Returns(job);
            scope.Resolve(job2.GetType()).Returns(job2);
            scope.ResolveAll <IBackgroundJobAsync>().Returns(new[] { job, job2 });

            var sut = new BackgroundJobManager(sl);

            sut.StartInterval = TimeSpan.FromSeconds(0);
            sut.Start();
            Thread.Sleep(10000);

            job2.Received().ExecuteAsync();
        }
        public void report_job_failure_using_the_event_and_include_NoJob_if_type_cant_be_resolved()
        {
            var sl    = Substitute.For <IContainer>();
            var scope = Substitute.For <IContainerScope>();
            var job   = Substitute.For <IBackgroundJob>();
            BackgroundJobFailedEventArgs actual = null;

            job.When(x => x.Execute()).Do(x => { throw new InvalidDataException(); });
            sl.CreateScope().Returns(scope);
            scope.ResolveAll <IBackgroundJob>().Returns(new[] { job });

            var sut = new BackgroundJobManager(sl);

            sut.StartInterval = TimeSpan.FromSeconds(0);
            sut.Start();
            sut.JobFailed += (sender, args) => actual = args;
            Thread.Sleep(100);

            actual.Exception.Should().BeOfType <InvalidOperationException>();
            actual.Job.Should().BeOfType <BackgroundJobManager.NoJob>();
        }
        public void consume_exceptions_thrown_by_event_subscribers()
        {
            var sl    = Substitute.For <IContainer>();
            var scope = Substitute.For <IContainerScope>();
            var job   = Substitute.For <IBackgroundJob>();
            var job2  = Substitute.For <IBackgroundJob>();

            job.When(x => x.Execute()).Do(x => { throw new SqlNullValueException(); });
            sl.CreateScope().Returns(scope);
            scope.ResolveAll <IBackgroundJob>().Returns(new[] { job, job2 });
            scope.Resolve(job.GetType()).Returns(job);
            scope.Resolve(job2.GetType()).Returns(job2);

            var sut = new BackgroundJobManager(sl);

            sut.StartInterval = TimeSpan.FromSeconds(0);
            sut.JobFailed    += (o, e) => { throw new Exception(); };
            sut.Start();
            Thread.Sleep(100);

            job2.Received().Execute();
        }
Beispiel #12
0
        private void RunDemo()
        {
            var adapter = CreateContainer();

            _serviceManager = new ApplicationServiceManager(adapter);
            _serviceManager.ServiceFailed += OnApplicationFailure;
            _serviceManager.Start();

            _jobManager            = new BackgroundJobManager(adapter);
            _jobManager.JobFailed += OnJobFailed;
            _jobManager.Start();


            //Press enter to shut down
            Console.ReadLine();

            _jobManager.Stop();
            _serviceManager.Stop();

            Console.WriteLine("Done, press enter to exit");
            Console.ReadLine();
        }
Beispiel #13
0
 public async Task Start()
 {
     _backgroundJobManager.Register <TestJob>(1000);
     await _backgroundJobManager.Start();
 }
Beispiel #14
0
 public async Task Start()
 {
     _backgroundJobManager.Register <TestJob>("TestJobIntervalInMs");
     await _backgroundJobManager.Start();
 }
Beispiel #15
0
 public override void Start()
 {
     backgroundJobManager.Register <TestJob>(1000);
     backgroundJobManager.UseAutofacActivator();
     backgroundJobManager.Start();
 }
 public override void Start()
 {
     BackgroundJobManager.Register <TestJob>(5000);
     BackgroundJobManager.Start();
 }