Skip to content

Commit

Permalink
Extract ScheduledJobManager interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Jul 23, 2024
1 parent 3465aea commit 7397710
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions samples/Foundatio.HostingSample/Startup/MyStartupAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyStartupAction> logger)
public MyStartupAction(IScheduledJobManager scheduledJobManager, ILogger<MyStartupAction> logger)
{
_scheduledJobManager = scheduledJobManager;
_logger = logger;
Expand Down
6 changes: 3 additions & 3 deletions src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IHostedService, ScheduledJobService>();

if (!services.Any(s => s.ServiceType == typeof(ScheduledJobManager)))
if (!services.Any(s => s.ServiceType == typeof(IScheduledJobManager) && s.ImplementationType == typeof(ScheduledJobManager)))
services.AddSingleton<ScheduledJobManager>();

return services.AddTransient(s => new ScheduledJobRegistration(jobOptions.CronSchedule, jobOptions.Name ?? Guid.NewGuid().ToString(), jobOptions.JobFactory));
Expand All @@ -45,7 +45,7 @@ public static IServiceCollection AddJob(this IServiceCollection services, Func<I
if (!services.Any(s => s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService)))
services.AddTransient<IHostedService, ScheduledJobService>();

if (!services.Any(s => s.ServiceType == typeof(ScheduledJobManager)))
if (!services.Any(s => s.ServiceType == typeof(IScheduledJobManager) && s.ImplementationType == typeof(ScheduledJobManager)))
services.AddSingleton<ScheduledJobManager>();

return services.AddTransient(s => new ScheduledJobRegistration(jobOptions.CronSchedule, jobOptions.Name ?? Guid.NewGuid().ToString(), _ => jobFactory(s)));
Expand All @@ -68,7 +68,7 @@ public static IServiceCollection AddJob<T>(this IServiceCollection services, Hos
if (!services.Any(s => s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService)))
services.AddTransient<IHostedService, ScheduledJobService>();

if (!services.Any(s => s.ServiceType == typeof(ScheduledJobManager)))
if (!services.Any(s => s.ServiceType == typeof(IScheduledJobManager) && s.ImplementationType == typeof(ScheduledJobManager)))
services.AddSingleton<ScheduledJobManager>();

return services.AddTransient(s => new ScheduledJobRegistration(jobOptions.CronSchedule, typeof(T).FullName, jobOptions.JobFactory ?? (_ => s.GetRequiredService<T>())));
Expand Down
15 changes: 14 additions & 1 deletion src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@

namespace Foundatio.Extensions.Hosting.Jobs;

public class ScheduledJobManager
public interface IScheduledJobManager
{
void AddOrUpdate<TJob>(string cronSchedule) where TJob : class, IJob;
void AddOrUpdate(string jobName, string cronSchedule, Func<IServiceProvider, CancellationToken, Task> action);
void AddOrUpdate(string jobName, string cronSchedule, Func<CancellationToken, Task> action);
void AddOrUpdate(string jobName, string cronSchedule, Func<Task> action);
void AddOrUpdate(string jobName, string cronSchedule, Action<IServiceProvider, CancellationToken> action);
void AddOrUpdate(string jobName, string cronSchedule, Action<CancellationToken> action);
void AddOrUpdate(string jobName, string cronSchedule, Action action);
void Remove<TJob>() where TJob : class, IJob;
void Remove(string jobName);
}

public class ScheduledJobManager : IScheduledJobManager
{
private readonly IServiceProvider _serviceProvider;
private readonly ILoggerFactory _loggerFactory;
Expand Down

0 comments on commit 7397710

Please sign in to comment.