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 { AsyncHelper.RunSync(() => JobExecuter.ExecuteAsync(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); } } }
static void Main(string[] args) { _cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = _cancellationTokenSource.Token; Task.Run(() => { Send(); }, cancellationToken); /* RUN AS A DEDICATED THREAD */ //Task.Factory.StartNew(() => //{ // var executor = new JobExecuter(); // executor.DoWork(cancellationToken); //}, TaskCreationOptions.LongRunning); /* RUN ON A THREAD POOL THREAD */ Task.Run(() => { var executor = new JobExecuter(); executor.DoWork(cancellationToken); }, cancellationToken); Console.WriteLine("In Main, Done, Press enter to stop sending messages"); Console.ReadLine(); _cancellationTokenSource.Cancel(); Console.WriteLine("After cancellation, press any key to terminate"); Console.ReadLine(); }
public async Task Execute(IJobExecutionContext context) { using (var scope = ServiceScopeFactory.CreateScope()) { var args = JsonSerializer.Deserialize <TArgs>(context.JobDetail.JobDataMap.GetString(nameof(TArgs))); var jobType = Options.GetJob(typeof(TArgs)).JobType; var jobContext = new JobExecutionContext(scope.ServiceProvider, jobType, args); try { await JobExecuter.ExecuteAsync(jobContext); } catch (Exception exception) { var jobExecutionException = new JobExecutionException(exception); var retryIndex = context.JobDetail.JobDataMap.GetString(QuartzBackgroundJobManager.JobDataPrefix + QuartzBackgroundJobManager.RetryIndex).To <int>(); retryIndex++; context.JobDetail.JobDataMap.Put(QuartzBackgroundJobManager.JobDataPrefix + QuartzBackgroundJobManager.RetryIndex, retryIndex.ToString()); await BackgroundJobQuartzOptions.RetryStrategy.Invoke(retryIndex, context, jobExecutionException); throw jobExecutionException; } } }
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); }
protected virtual void MessageReceived(object sender, BasicDeliverEventArgs ea) { var context = new JobExecutionContext( 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 async Task <JsonEntityBase> Handle(SaveExecuterCmd request, CancellationToken cancellationToken) { if (_validationNotificationContext.HasErrorNotifications) { return(_validationNotificationContext.GetErrorNotifications().Error()); } var model = request.VM; JobExecuter executer = null; if (model.IsNew) { // add executer = new JobExecuter() { RegistryKey = model.RegistryKey, InTime = DateTime.Now, Name = model.ExecuterName, UpdateTime = DateTime.Now, Auto = model.Auto }; await _dbContext.JobExecuter.AddAsync(executer); if (!model.Auto) { var registryHosts = model.RegistryHosts?.Where(x => x.Url.NotNullOrEmpty()).Select(x => new RegistryHost() { ExecuterId = executer.Id, Host = x.Url, Order = 0 }); if (registryHosts != null && registryHosts.Any()) { // Z.EntityFramework.Extensions.EFCore.dll is commercial and required payment //await _dbContext.RegistryHost.BulkInsertAsync(registryHosts, options => //{ // options.InsertIfNotExists = true; // options.ColumnPrimaryKeyExpression = x => x.Host; //}); _dbContext.ChangeTracker.AutoDetectChangesEnabled = false; _dbContext.AddRange(registryHosts); } } else { var regList = await _dbContext.RegistryInfo.OrderBy(x => x.InTime) .Where(x => x.Name == model.RegistryKey).ToListAsync(); if (regList != null && executer != null) { var registryHosts = regList.Select(x => new RegistryHost() { ExecuterId = executer.Id, Host = x.Host, Order = 0 }); // Z.EntityFramework.Extensions.EFCore.dll is commercial and required payment //await _dbContext.RegistryHost.BulkInsertAsync(registryHosts, options => //{ // options.InsertIfNotExists = true; // options.ColumnPrimaryKeyExpression = x => x.Host; //}); _dbContext.ChangeTracker.AutoDetectChangesEnabled = false; _dbContext.AddRange(registryHosts); } } } else { executer = await _dbContext.JobExecuter.Include(x => x.RegistryHosts).FirstOrDefaultAsync(x => x.Id == model.ExecuterId); executer.RegistryKey = model.RegistryKey; executer.Name = model.ExecuterName; executer.UpdateTime = DateTime.Now; executer.Auto = model.Auto; if (!model.Auto && model.RegistryHosts != null) { var registryHosts = model.RegistryHosts.Where(x => x.Url.NotNullOrEmpty()) .Select(x => new RegistryHost() { ExecuterId = executer.Id, Host = x.Url, Order = 0, Id = x.Id }); if (registryHosts != null && registryHosts.Any()) { // Z.EntityFramework.Extensions.EFCore.dll is commercial and required payment // BLT.EntityFramework.Extensions.EFCore.dll was hack this code: // if (LicenseManager.\uE000.Count == 0) // { // if (DateTime.Now < new DateTime(2199, 2, 1)) //await _dbContext.RegistryHost.BulkMergeAsync(registryHosts, options => //{ // options.InsertIfNotExists = true; // options.ColumnPrimaryKeyExpression = x => x.Host; //}); _dbContext.ChangeTracker.AutoDetectChangesEnabled = false; var groups = registryHosts.GroupBy(x => x.Id == 0).ToDictionary(x => x.Key, y => y.ToList()); if (groups.ContainsKey(true) && groups[true].Any()) { _dbContext.AddRange(groups[true]); } if (groups.ContainsKey(false) && groups[false].Any()) { _dbContext.UpdateRange(groups[false]); } } } } await _dbContext.SaveChangesAsync(); return(new { }.Success()); }
public static void Main(string[] args) { JobExecuter.Main(args); }
/// <summary> /// Main method, run with --help for details /// </summary> /// <param name="args">program arguments</param> public static void Main(string[] args) { var commandLineApp = new CommandLineApplication(false) { Name = "defcore" }; // Set up the command for running the server commandLineApp.Command("server", c => { c.HelpOption("-?|-h|--help"); c.Description = "Runs the NodeManager server on a local IP address"; // specifiy and option var directoryOption = c.Option("-d|--directory <PATH>", "Path to the working directory of the server, input will be relative to current directory", CommandOptionType.SingleValue); c.OnExecute(() => { if (directoryOption.HasValue()) { SetOrCreateWorkingDirectory(directoryOption.Value()); } NodeManager.Main(); return(0); } ); }); // set up a command for running the node commandLineApp.Command("node", c => { c.HelpOption("-?|-h|--help"); c.Description = "Runs the Node on a local IP address"; // specifiy and option var directoryOption = c.Option("-d|--directory <PATH>", "Path to the working directory of the node, input will be relative to current directory", CommandOptionType.SingleValue); var ipArgument = c.Argument("[IP]", "The IP address for the server"); c.OnExecute(() => { if (directoryOption.HasValue()) { SetOrCreateWorkingDirectory(directoryOption.Value()); } if (ipArgument.Values.Count == 0) { Console.WriteLine("Must provide an ip address, use --help for more details"); return(1); } Node.Main(new [] { ipArgument.Value }); return(0); }); }); // set up a command for submitting a job commandLineApp.Command("submit", c => { c.HelpOption("-?|-h|--help"); c.Description = "submits a job to the system"; // specifiy and option var directoryOption = c.Option("-d|--directory <PATH>", "Path to the working directory, input will be relative to current directory", CommandOptionType.SingleValue); //var ipArgument = c.Argument("[IP]", "The IP address for the server"); var program = c.Argument("program", "User program and arguments", true); c.OnExecute(() => { if (directoryOption.HasValue()) { SetOrCreateWorkingDirectory(directoryOption.Value()); } SubmitJob.Main(program.Values.ToArray()); return(0); }); }); // setup a command for loading an executable commandLineApp.Command("load", c => { c.HelpOption("-?|-h|--help"); c.Description = "loads a job to and executes it"; var program = c.Argument("arguments", "program arguments", true); c.OnExecute(() => { JobExecuter.Main(program.Values.ToArray()); return(0); }); }); commandLineApp.HelpOption("-? | -h | --help"); commandLineApp.OnExecute(() => { if (args.Length == 0) { Console.WriteLine(commandLineApp.GetHelpText()); } return(0); }); try { commandLineApp.Execute(args); } catch (Exception e) { Console.WriteLine(e.Message); } }