public HostedJobService(IServiceProvider serviceProvider, HostedJobOptions jobOptions, ILoggerFactory loggerFactory) { _serviceProvider = serviceProvider; _loggerFactory = loggerFactory; _logger = loggerFactory.CreateLogger <HostedJobService>(); _jobOptions = jobOptions; var lifetime = serviceProvider.GetService <ShutdownHostIfNoJobsRunningService>(); lifetime?.RegisterHostedJobInstance(this); }
public static IServiceCollection AddJob(this IServiceCollection services, HostedJobOptions jobOptions) { if (jobOptions.JobFactory == null) { throw new ArgumentNullException(nameof(jobOptions), "jobOptions.JobFactory is required"); } if (String.IsNullOrEmpty(jobOptions.CronSchedule)) { return(services.AddTransient <IHostedService>(s => new HostedJobService(s, jobOptions, s.GetService <ILoggerFactory>()))); } else { if (!services.Any(s => s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService))) { services.AddTransient <IHostedService, ScheduledJobService>(); } return(services.AddTransient(s => new ScheduledJobRegistration(jobOptions.JobFactory, jobOptions.CronSchedule))); } }
public HostedJobOptionsBuilder(HostedJobOptions target = null) { Target = target ?? new HostedJobOptions(); }
public static IServiceCollection AddJob <T>(this IServiceCollection services, HostedJobOptions jobOptions) where T : class, IJob { services.AddTransient <T>(); if (String.IsNullOrEmpty(jobOptions.CronSchedule)) { return(services.AddTransient <IHostedService>(s => { if (jobOptions.JobFactory == null) { jobOptions.JobFactory = s.GetRequiredService <T>; } return new HostedJobService(s, jobOptions, s.GetService <ILoggerFactory>()); })); } else { if (!services.Any(s => s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService))) { services.AddTransient <IHostedService, ScheduledJobService>(); } return(services.AddTransient(s => new ScheduledJobRegistration(jobOptions.JobFactory ?? (s.GetRequiredService <T>), jobOptions.CronSchedule))); } }
public static IServiceCollection AddJob(this IServiceCollection services, Func <IServiceProvider, IJob> jobFactory, HostedJobOptions jobOptions) { if (String.IsNullOrEmpty(jobOptions.CronSchedule)) { return(services.AddTransient <IHostedService>(s => { jobOptions.JobFactory = () => jobFactory(s); return new HostedJobService(s, jobOptions, s.GetService <ILoggerFactory>()); })); } else { if (!services.Any(s => s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService))) { services.AddTransient <IHostedService, ScheduledJobService>(); } return(services.AddTransient(s => new ScheduledJobRegistration(() => jobFactory(s), jobOptions.CronSchedule))); } }