protected virtual void MessageReceived(object sender, BasicDeliverEventArgs ea) { using (var scope = ServiceScopeFactory.CreateScope()) { var context = new JobExecutionContext( scope.ServiceProvider, JobConfiguration.JobType, Serializer.Deserialize(ea.Body, typeof(TArgs)) ); try { JobExecuter.Execute(context); ChannelAccessor.Channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); } catch (BackgroundJobExecutionException) { //TODO: Reject like that? ChannelAccessor.Channel.BasicReject(deliveryTag: ea.DeliveryTag, requeue: true); } catch (Exception) { //TODO: Reject like that? ChannelAccessor.Channel.BasicReject(deliveryTag: ea.DeliveryTag, requeue: false); } } }
public void Execute_DuplicatedNamesTest() { var executer = new JobExecuter(Mock.Create <ILogger>()); var res = executer.Execute(new List <char>() { 'a', 'a', 'a', 'B', 'B', 'B' }, TimeSpan.FromMilliseconds(500)); Assert.AreEqual <int>(6, res.Count); }
public void Execute_UpperCaseNameTest() { var executer = new JobExecuter(Mock.Create <ILogger>()); var res = executer.Execute(new List <char>() { 'B' }, TimeSpan.FromMilliseconds(500)); Assert.AreEqual <int>(1, res.Count); var job = res.Find(x => x.JobName == 'B'); TestExecutionResult(job, 'B'); }
/// <summary> /// /// </summary> /// <param name="job"></param> private void JobExecute(Job job) { var logService = new TaskLogService(this); var jobExecuter = new JobExecuter(job); jobExecuter.Container.Register <ITaskLogService>(logService); jobExecuter.Container.Register <ITaskCustomizedLogService>(new TaskCustomizedLogService(this)); jobExecuter.Initialize(); jobExecuter.TaskExecuted += executer => logService.Information(Environment.NewLine); DisableComponents(); ThreadTask.Run(() => jobExecuter.Execute()).ContinueWith(OnExecuteFinished); }
protected override void DoWork() { using (var scope = ServiceScopeFactory.CreateScope()) { var store = scope.ServiceProvider.GetRequiredService <IBackgroundJobStore>(); var waitingJobs = AsyncHelper.RunSync( () => store.GetWaitingJobsAsync(WorkerOptions.MaxJobFetchCount) ); foreach (var jobInfo in waitingJobs) { jobInfo.TryCount++; jobInfo.LastTryTime = Clock.Now; try { var jobConfiguration = JobOptions.GetJob(jobInfo.JobName); var jobArgs = Serializer.Deserialize(jobInfo.JobArgs, jobConfiguration.ArgsType); var context = new JobExecutionContext(scope.ServiceProvider, jobConfiguration.JobType, jobArgs); try { JobExecuter.Execute(context); AsyncHelper.RunSync(() => store.DeleteAsync(jobInfo.Id)); } catch (BackgroundJobExecutionException) { var nextTryTime = CalculateNextTryTime(jobInfo); if (nextTryTime.HasValue) { jobInfo.NextTryTime = nextTryTime.Value; } else { jobInfo.IsAbandoned = true; } TryUpdate(store, jobInfo); } } catch (Exception ex) { Logger.LogException(ex); jobInfo.IsAbandoned = true; TryUpdate(store, jobInfo); } } } }
public void Execute_EstimatedTimeRangeTest() { var executer = new JobExecuter(Mock.Create <ILogger>()); var res = executer.Execute(new List <char>() { 'E' }, TimeSpan.FromMilliseconds(500)); Assert.AreEqual <int>(1, res.Count); var job = res.Find(x => x.JobName == 'E'); Assert.IsNotNull(job); Assert.IsTrue(job.EstimatedExecutionTime.TotalMilliseconds > 0); Assert.IsTrue(job.EstimatedExecutionTime.TotalMilliseconds < 500); }
public void Execute_MixedCaseNameTest() { var executer = new JobExecuter(Mock.Create <ILogger>()); var jobs = new List <char>() { 'B', 'c', 'e', 'Z' }; var res = executer.Execute(jobs, TimeSpan.FromMilliseconds(500)); Assert.AreEqual <int>(4, res.Count); jobs.ForEach(jobName => { var job = res.Find(x => x.JobName == jobName); TestExecutionResult(job, jobName); }); }
public void Execute_LoggerMustBeCalledTest() { var logger = Mock.Create <ILogger>(); Mock.Arrange(() => logger.Debug(Arg.IsAny <string>())).MustBeCalled(); Mock.Arrange(() => logger.Info(Arg.IsAny <string>())).MustBeCalled(); var executer = new JobExecuter(logger); var jobs = new List <char>() { 'B', 'c', 'e', 'Z' }; executer.Execute(jobs, TimeSpan.FromMilliseconds(500)); Mock.Assert(logger); }