public static int RunInConsole(JobRunOptions options, Action <IServiceProvider> afterBootstrap = null) { int result; string jobName = "N/A"; try { ResolveTypes(options); if (options.JobType == null) { Console.Error.WriteLine($"Unable to resolve job type {options.JobTypeName}."); return(-1); } jobName = options.JobType.Name; Logger.GlobalProperties.Set("job", jobName); if (!(options.NoServiceProvider.HasValue && options.NoServiceProvider.Value == false)) { ServiceProvider.SetServiceProvider(options.ServiceProviderType ?? options.JobType); } // force bootstrap now so logging will be configured if (ServiceProvider.Current is IBootstrappedServiceProvider) { ((IBootstrappedServiceProvider)ServiceProvider.Current).Bootstrap(); } Logger.Info().Message("Starting job...").Write(); afterBootstrap?.Invoke(ServiceProvider.Current); result = JobRunner.RunAsync(options).Result; if (Debugger.IsAttached) { Console.ReadKey(); } } catch (FileNotFoundException e) { Console.Error.WriteLine("{0} ({1})", e.GetMessage(), e.FileName); Logger.Error().Message($"{e.GetMessage()} ({e.FileName})").Write(); if (Debugger.IsAttached) { Console.ReadKey(); } return(1); } catch (Exception e) { Console.Error.WriteLine(e.ToString()); Logger.Error().Exception(e).Message($"Job \"{jobName}\" error: {e.GetMessage()}").Write(); if (Debugger.IsAttached) { Console.ReadKey(); } return(1); } return(result); }
public static async Task <int> RunAsync(JobRunOptions options, CancellationToken cancellationToken = default(CancellationToken)) { ResolveJobTypes(options); if (options.JobType == null) { return(-1); } Log.Info().Message("Starting {0}job type \"{1}\" on machine \"{2}\"...", options.RunContinuous ? "continuous " : String.Empty, options.JobType.Name, Environment.MachineName).Write(); WatchForShutdown(); var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(_cancellationTokenSource.Token, cancellationToken).Token; if (options.RunContinuous) { await RunContinuousAsync(options.JobType, options.ServiceProviderType, options.Interval, options.IterationLimit, options.InstanceCount, linkedToken); } else { return((await RunAsync(options.JobType, options.ServiceProviderType, linkedToken)).IsSuccess ? 0 : -1); } return(0); }
public void ResolveTypes(JobRunOptions options) { if (options.JobType == null && !String.IsNullOrEmpty(options.JobTypeName)) { options.JobType = TypeHelper.ResolveType(options.JobTypeName, typeof(JobBase), _logger); } if (options.ServiceProviderType == null && !String.IsNullOrEmpty(options.ServiceProviderTypeName)) { options.ServiceProviderType = TypeHelper.ResolveType(options.ServiceProviderTypeName, typeof(IServiceProvider), _logger); } }
public static async Task <int> RunAsync(JobRunOptions options, CancellationToken cancellationToken = default(CancellationToken)) { ResolveTypes(options); if (options.JobType == null) { return(-1); } WatchForShutdown(); var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(_cancellationTokenSource.Token, cancellationToken).Token; if (options.RunContinuous) { await RunContinuousAsync(options.JobType, options.Interval, options.InitialDelay, options.IterationLimit, options.InstanceCount, linkedCancellationToken).AnyContext(); } else { return((await RunAsync(options.JobType, options.InitialDelay, linkedCancellationToken).AnyContext()).IsSuccess ? 0 : -1); } return(0); }
public int RunInConsole(JobRunOptions options, Action <IServiceProvider> afterBootstrap = null) { int result; string jobName = "N/A"; try { ResolveTypes(options); if (options.JobType == null) { Console.Error.WriteLine($"Unable to resolve job type {options.JobTypeName}."); return(-1); } jobName = TypeHelper.GetTypeDisplayName(options.JobType); using (_logger.BeginScope(s => s.Property("job", jobName))) { if (!(options.NoServiceProvider.HasValue && options.NoServiceProvider.Value == false)) { ServiceProvider.SetServiceProvider(options.ServiceProviderType ?? options.JobType); } IServiceProvider serviceProvider = ServiceProvider.Current; // force bootstrap now so logging will be configured var bootstrappedServiceProvider = ServiceProvider.Current as IBootstrappedServiceProvider; if (bootstrappedServiceProvider != null) { bootstrappedServiceProvider.LoggerFactory = _loggerFactory; bootstrappedServiceProvider.Bootstrap(); serviceProvider = bootstrappedServiceProvider.ServiceProvider; } _logger.Info("Starting job..."); afterBootstrap?.Invoke(serviceProvider); result = RunAsync(options).Result; if (Debugger.IsAttached) { Console.ReadKey(); } } } catch (FileNotFoundException e) { Console.Error.WriteLine("{0} ({1})", e.GetMessage(), e.FileName); _logger.Error(() => $"{e.GetMessage()} ({ e.FileName})"); if (Debugger.IsAttached) { Console.ReadKey(); } return(1); } catch (Exception e) { Console.Error.WriteLine(e.ToString()); _logger.Error(e, "Job \"{jobName}\" error: {Message}", jobName, e.GetMessage()); if (Debugger.IsAttached) { Console.ReadKey(); } return(1); } return(result); }