private void Restart(JobRunnerOptions options) { _options = options; Stop(); Start(); }
public JobRunner(IOptionsMonitor <JobRunnerOptions> options, IServiceProvider serviceProvider, ILoggerFactory loggerFactory) { _options = options?.CurrentValue ?? throw new ArgumentNullException(nameof(options)); _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); _timers = new List <IDisposable>(); _jobs = new List <Type>(); options.OnChange(Restart); }
public static IHostBuilder CreateHostBuilder(string[] args) { var jobOptions = new JobRunnerOptions(args); Console.Title = jobOptions.JobName != null ? $"Exceptionless {jobOptions.JobName} Job" : "Exceptionless Jobs"; string environment = Environment.GetEnvironmentVariable("EX_AppMode"); if (String.IsNullOrWhiteSpace(environment)) { environment = "Production"; } var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddYamlFile("appsettings.yml", optional: true, reloadOnChange: true) .AddYamlFile($"appsettings.{environment}.yml", optional: true, reloadOnChange: true) .AddEnvironmentVariables("EX_") .AddEnvironmentVariables("ASPNETCORE_") .AddCommandLine(args) .Build(); var options = AppOptions.ReadFromConfiguration(config); var loggerConfig = new LoggerConfiguration().ReadFrom.Configuration(config) .Enrich.FromLogContext() .Enrich.WithMachineName() .Enrich.WithSpan(); if (!String.IsNullOrEmpty(options.ExceptionlessApiKey)) { loggerConfig.WriteTo.Sink(new ExceptionlessSink(), LogEventLevel.Information); } Log.Logger = loggerConfig.CreateLogger(); var configDictionary = config.ToDictionary("Serilog"); Log.Information("Bootstrapping Exceptionless {JobName} job(s) in {AppMode} mode ({InformationalVersion}) on {MachineName} with settings {@Settings}", jobOptions.JobName ?? "All", environment, options.InformationalVersion, Environment.MachineName, configDictionary); var builder = Host.CreateDefaultBuilder() .UseEnvironment(environment) .UseSerilog() .ConfigureWebHostDefaults(webBuilder => { webBuilder .UseConfiguration(config) .Configure(app => { app.UseSerilogRequestLogging(o => { o.MessageTemplate = "traceID={TraceId} HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms"; o.GetLevel = (context, duration, ex) => { if (ex != null || context.Response.StatusCode > 499) { return(LogEventLevel.Error); } return(duration < 1000 && context.Response.StatusCode < 400 ? LogEventLevel.Debug : LogEventLevel.Information); }; }); }) .Configure(app => { Bootstrapper.LogConfiguration(app.ApplicationServices, options, app.ApplicationServices.GetService <ILogger <Program> >()); if (!String.IsNullOrEmpty(options.ExceptionlessApiKey) && !String.IsNullOrEmpty(options.ExceptionlessServerUrl)) { app.UseExceptionless(ExceptionlessClient.Default); } app.UseHealthChecks("/health", new HealthCheckOptions { Predicate = hcr => !String.IsNullOrEmpty(jobOptions.JobName) ? hcr.Tags.Contains(jobOptions.JobName) : hcr.Tags.Contains("AllJobs") }); app.UseHealthChecks("/ready", new HealthCheckOptions { Predicate = hcr => hcr.Tags.Contains("Critical") }); app.UseWaitForStartupActionsBeforeServingRequests(); app.Run(async context => { await context.Response.WriteAsync($"Running Job: {jobOptions.JobName}"); }); }); }) .ConfigureServices((ctx, services) => { AddJobs(services, jobOptions); services.AddAppOptions(options); Bootstrapper.RegisterServices(services); Insulation.Bootstrapper.RegisterServices(services, options, true); services.AddApm(new ApmConfig(config, "Exceptionless.Job", "Exceptionless", options.InformationalVersion, options.CacheOptions.Provider == "redis")); }); if (!String.IsNullOrEmpty(options.MetricOptions.Provider)) { ConfigureMetricsReporting(builder, options.MetricOptions); } return(builder); }
private static void AddJobs(IServiceCollection services, JobRunnerOptions options) { services.AddJobLifetimeService(); if (options.CleanupData) { services.AddJob <CleanupDataJob>(); } if (options.CleanupOrphanedData) { services.AddJob <CleanupOrphanedDataJob>(); } if (options.CloseInactiveSessions) { services.AddJob <CloseInactiveSessionsJob>(true); } if (options.DailySummary) { services.AddJob <DailySummaryJob>(true); } if (options.DataMigration) { services.AddJob <DataMigrationJob>(true); } if (options.DownloadGeoIPDatabase) { services.AddJob <DownloadGeoIPDatabaseJob>(true); } if (options.EventNotifications) { services.AddJob <EventNotificationsJob>(true); } if (options.EventPosts) { services.AddJob <EventPostsJob>(true); } if (options.EventUserDescriptions) { services.AddJob <EventUserDescriptionsJob>(true); } if (options.MailMessage) { services.AddJob <MailMessageJob>(true); } if (options.MaintainIndexes) { services.AddJob <MaintainIndexesJob>(); } if (options.Migration) { services.AddJob <MigrationJob>(true); } if (options.StackStatus) { services.AddJob <StackStatusJob>(true); } if (options.StackEventCount) { services.AddJob <StackEventCountJob>(true); } if (options.WebHooks) { services.AddJob <WebHooksJob>(true); } if (options.WorkItem) { services.AddJob <WorkItemJob>(true); } }