From 739771080c3179a58551104bdd6b0a717a28fe00 Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Tue, 23 Jul 2024 18:32:31 -0500 Subject: [PATCH] Extract ScheduledJobManager interface --- .../Startup/MyStartupAction.cs | 4 ++-- .../Jobs/JobHostExtensions.cs | 6 +++--- .../Jobs/ScheduledJobManager.cs | 15 ++++++++++++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/samples/Foundatio.HostingSample/Startup/MyStartupAction.cs b/samples/Foundatio.HostingSample/Startup/MyStartupAction.cs index 88d39589..f0f55439 100644 --- a/samples/Foundatio.HostingSample/Startup/MyStartupAction.cs +++ b/samples/Foundatio.HostingSample/Startup/MyStartupAction.cs @@ -8,10 +8,10 @@ namespace Foundatio.HostingSample; public class MyStartupAction : IStartupAction { - private readonly ScheduledJobManager _scheduledJobManager; + private readonly IScheduledJobManager _scheduledJobManager; private readonly ILogger _logger; - public MyStartupAction(ScheduledJobManager scheduledJobManager, ILogger logger) + public MyStartupAction(IScheduledJobManager scheduledJobManager, ILogger logger) { _scheduledJobManager = scheduledJobManager; _logger = logger; diff --git a/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs b/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs index a3c5a215..edb1b973 100644 --- a/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs +++ b/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs @@ -24,7 +24,7 @@ public static IServiceCollection AddJob(this IServiceCollection services, Hosted if (!services.Any(s => s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService))) services.AddTransient(); - if (!services.Any(s => s.ServiceType == typeof(ScheduledJobManager))) + if (!services.Any(s => s.ServiceType == typeof(IScheduledJobManager) && s.ImplementationType == typeof(ScheduledJobManager))) services.AddSingleton(); return services.AddTransient(s => new ScheduledJobRegistration(jobOptions.CronSchedule, jobOptions.Name ?? Guid.NewGuid().ToString(), jobOptions.JobFactory)); @@ -45,7 +45,7 @@ public static IServiceCollection AddJob(this IServiceCollection services, Func s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService))) services.AddTransient(); - if (!services.Any(s => s.ServiceType == typeof(ScheduledJobManager))) + if (!services.Any(s => s.ServiceType == typeof(IScheduledJobManager) && s.ImplementationType == typeof(ScheduledJobManager))) services.AddSingleton(); return services.AddTransient(s => new ScheduledJobRegistration(jobOptions.CronSchedule, jobOptions.Name ?? Guid.NewGuid().ToString(), _ => jobFactory(s))); @@ -68,7 +68,7 @@ public static IServiceCollection AddJob(this IServiceCollection services, Hos if (!services.Any(s => s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService))) services.AddTransient(); - if (!services.Any(s => s.ServiceType == typeof(ScheduledJobManager))) + if (!services.Any(s => s.ServiceType == typeof(IScheduledJobManager) && s.ImplementationType == typeof(ScheduledJobManager))) services.AddSingleton(); return services.AddTransient(s => new ScheduledJobRegistration(jobOptions.CronSchedule, typeof(T).FullName, jobOptions.JobFactory ?? (_ => s.GetRequiredService()))); diff --git a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs index 18799c7d..18f0a0a4 100644 --- a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs +++ b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs @@ -10,7 +10,20 @@ namespace Foundatio.Extensions.Hosting.Jobs; -public class ScheduledJobManager +public interface IScheduledJobManager +{ + void AddOrUpdate(string cronSchedule) where TJob : class, IJob; + void AddOrUpdate(string jobName, string cronSchedule, Func action); + void AddOrUpdate(string jobName, string cronSchedule, Func action); + void AddOrUpdate(string jobName, string cronSchedule, Func action); + void AddOrUpdate(string jobName, string cronSchedule, Action action); + void AddOrUpdate(string jobName, string cronSchedule, Action action); + void AddOrUpdate(string jobName, string cronSchedule, Action action); + void Remove() where TJob : class, IJob; + void Remove(string jobName); +} + +public class ScheduledJobManager : IScheduledJobManager { private readonly IServiceProvider _serviceProvider; private readonly ILoggerFactory _loggerFactory;