private async Task ExecuteAsyncJob(Type jobType)
        {
            IBackgroundJobAsync job = null;

            try
            {
                using (var scope = _container.CreateScope())
                {
                    try
                    {
                        job = (IBackgroundJobAsync)scope.Resolve(jobType);
                        ScopeCreated(this, new ScopeCreatedEventArgs(scope));
                        await job.ExecuteAsync();

                        ScopeClosing(this, new ScopeClosingEventArgs(scope, true));
                    }
                    catch (Exception exception)
                    {
                        var args = new BackgroundJobFailedEventArgs((object)job ?? new NoJob(jobType, exception),
                                                                    exception);
                        JobFailed(this, args);
                        ScopeClosing(this, new ScopeClosingEventArgs(scope, false)
                        {
                            Exception = exception
                        });
                    }
                }
            }
            catch (Exception exception)
            {
                JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(jobType, exception), exception));
                _logger.Error("Failed to execute job: " + job, exception);
            }
        }
        private void OnExecuteJob(object state)
        {
            Task allTask = null;

            try
            {
                Parallel.ForEach(_syncJobTypes, jobType =>
                {
                    IBackgroundJob job = null;
                    try
                    {
                        using (var scope = _container.CreateScope())
                        {
                            try
                            {
                                job = (IBackgroundJob)scope.Resolve(jobType);
                                ScopeCreated(this, new ScopeCreatedEventArgs(scope));
                                job.Execute();
                                Signal.Reset("ApplicationServices[" + job.GetType() + "].Failed");
                                ScopeClosing(this, new ScopeClosingEventArgs(scope, true));
                            }
                            catch (Exception exception)
                            {
                                Signal.Raise("ApplicationServices[" + jobType.FullName + "].Failed", "Failed to execute job.", exception);
                                var args = new BackgroundJobFailedEventArgs(job ?? new NoJob(jobType, exception), exception);
                                JobFailed(this, args);
                                ScopeClosing(this, new ScopeClosingEventArgs(scope, false)
                                {
                                    Exception = exception
                                });
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(jobType, exception), exception));
                        _logger.Error("Failed to execute job: " + job, exception);
                    }
                });

                var tasks = _asyncJobTypes.Select(ExecuteAsyncJob);
                allTask = Task.WhenAll(tasks);
                allTask.Wait();
            }
            catch (Exception exception)
            {
                _logger.Error("failed to execute jobs", exception);
                if (allTask != null)
                {
                    JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(GetType(), allTask.Exception), exception));
                }
                else
                {
                    JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(GetType(), exception), exception));
                }
            }
        }
        public void konstruktorn_ska_initiera_våra_properties()
        {
            var job = Substitute.For<IBackgroundJob>();
            var exception = new Exception();

            var args = new BackgroundJobFailedEventArgs(job, exception);

            args.Job.Should().Be(job);
            args.Exception.Should().Be(exception);
        }
        private void ExecuteSyncJob(Type jobType)
        {
            IBackgroundJob job = null;

            try
            {
                using (var scope = _container.CreateScope())
                {
                    try
                    {
                        job = (IBackgroundJob)scope.Resolve(jobType);
                        if (job == null)
                        {
                            throw new InvalidOperationException(string.Format("Failed to resolve job type '{0}'.", jobType.FullName));
                        }

                        ScopeCreated(this, new ScopeCreatedEventArgs(scope));
                        job.Execute();
                        ScopeClosing(this, new ScopeClosingEventArgs(scope, true));
                    }
                    catch (Exception exception)
                    {
                        var args = new BackgroundJobFailedEventArgs(job ?? new NoJob(jobType, exception), exception);
                        JobFailed(this, args);
                        ScopeClosing(this, new ScopeClosingEventArgs(scope, false)
                        {
                            Exception = exception
                        });
                    }
                }
            }
            catch (Exception exception)
            {
                JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(jobType, exception), exception));
                _logger.Error("Failed to execute job: " + job, exception);
            }
        }
 private async Task ExecuteAsyncJob(Type jobType)
 {
     IBackgroundJobAsync job = null;
     try
     {
         using (var scope = _container.CreateScope())
         {
             try
             {
                 job = (IBackgroundJobAsync) scope.Resolve(jobType);
                 ScopeCreated(this, new ScopeCreatedEventArgs(scope));
                 await job.ExecuteAsync();
                 Signal.Reset("ApplicationServices[" + job.GetType() + "].Failed");
                 ScopeClosing(this, new ScopeClosingEventArgs(scope, true));
             }
             catch (Exception exception)
             {
                 Signal.Raise("ApplicationServices[" + jobType.FullName + "].Failed", "Failed to execute async job.", exception);
                 var args = new BackgroundJobFailedEventArgs((object)job ?? new NoJob(jobType, exception),
                     exception);
                 JobFailed(this, args);
                 ScopeClosing(this, new ScopeClosingEventArgs(scope, false) {Exception = exception});
             }
         }
     }
     catch (Exception exception)
     {
         JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(jobType, exception), exception));
         _logger.Error("Failed to execute job: " + job, exception);
     }
 }
        private void OnExecuteJob(object state)
        {
            Task allTask = null;
            try
            {
                Parallel.ForEach(_syncJobTypes, jobType =>
                {
                    IBackgroundJob job = null;
                    try
                    {
                        using (var scope = _container.CreateScope())
                        {
                            try
                            {
                                job = (IBackgroundJob)scope.Resolve(jobType);
                                if (job == null)
                                    throw new InvalidOperationException(string.Format("Failed to resolve job type '{0}'.", jobType.FullName));

                                ScopeCreated(this, new ScopeCreatedEventArgs(scope));
                                job.Execute();
                                Signal.Reset("ApplicationServices[" + job.GetType() + "].Failed");
                                ScopeClosing(this, new ScopeClosingEventArgs(scope, true));
                            }
                            catch (Exception exception)
                            {
                                Signal.Raise("ApplicationServices[" + jobType.FullName + "].Failed", "Failed to execute job.", exception);
                                var args = new BackgroundJobFailedEventArgs(job ?? new NoJob(jobType, exception), exception);
                                JobFailed(this, args);
                                ScopeClosing(this, new ScopeClosingEventArgs(scope, false) { Exception = exception });
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(jobType, exception), exception));
                        _logger.Error("Failed to execute job: " + job, exception);
                    }
                });

                var tasks = _asyncJobTypes.Select(ExecuteAsyncJob);
                allTask = Task.WhenAll(tasks);
                allTask.Wait();
            }
            catch (Exception exception)
            {
                _logger.Error("failed to execute jobs", exception);
                if (allTask != null)
                    JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(GetType(), allTask.Exception), exception));
                else
                    JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(GetType(), exception), exception));
            }
        }
Пример #7
0
 private void OnJobFailed(object sender, BackgroundJobFailedEventArgs e)
 {
     Console.WriteLine(e.Job + " failed: " + e.Exception);
 }
        private void OnExecuteJob(object state)
        {
            Task allTask = null;
            try
            {
                Parallel.ForEach(_syncJobTypes, jobType =>
                {
                    IBackgroundJob job = null;
                    try
                    {
                        using (var scope = _container.CreateScope())
                        {
                            try
                            {
                                job = (IBackgroundJob)scope.Resolve(jobType);
                                ScopeCreated(this, new ScopeCreatedEventArgs(scope));
                                job.Execute();
                                ScopeClosing(this, new ScopeClosingEventArgs(scope, true));
                            }
                            catch (Exception exception)
                            {
                                var args = new BackgroundJobFailedEventArgs(job ?? new NoJob(jobType, exception), exception);
                                JobFailed(this, args);
                                ScopeClosing(this, new ScopeClosingEventArgs(scope, false) { Exception = exception });
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(jobType, exception), exception));
                        _logger.Error("Failed to execute job: " + job, exception);
                    }
                });

                var tasks = _asyncJobTypes.Select(ExecuteAsyncJob);
                allTask = Task.WhenAll(tasks);
                allTask.Wait();
            }
            catch (Exception exception)
            {
                _logger.Error("failed to execute jobs", exception);
                if (allTask != null)
                    JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(GetType(), allTask.Exception), exception));
                else
                    JobFailed(this, new BackgroundJobFailedEventArgs(new NoJob(GetType(), exception), exception));
            }
        }