Skip to content

Commit

Permalink
Rename ScheduledJobManager to JobManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Aug 30, 2024
1 parent 6168e28 commit 31300b1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
4 changes: 2 additions & 2 deletions samples/Foundatio.HostingSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public static IHostBuilder CreateHostBuilder(string[] args)
{
e.MapGet("/jobstatus", httpContext =>
{
var jobManager = httpContext.RequestServices.GetRequiredService<ScheduledJobManager>();
var jobManager = httpContext.RequestServices.GetRequiredService<JobManager>();
var status = jobManager.GetJobStatus();
return httpContext.Response.WriteAsJsonAsync(status);
});
e.MapGet("/runjob", async httpContext =>
{
var jobManager = httpContext.RequestServices.GetRequiredService<ScheduledJobManager>();
var jobManager = httpContext.RequestServices.GetRequiredService<JobManager>();
await jobManager.RunJobAsync("EvenMinutes");
await jobManager.RunJobAsync<EveryMinuteJob>();
});
Expand Down
8 changes: 4 additions & 4 deletions samples/Foundatio.HostingSample/Startup/MyStartupAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace Foundatio.HostingSample;

public class MyStartupAction : IStartupAction
{
private readonly IScheduledJobManager _scheduledJobManager;
private readonly IJobManager _jobManager;
private readonly ILogger _logger;

public MyStartupAction(IScheduledJobManager scheduledJobManager, ILogger<MyStartupAction> logger)
public MyStartupAction(IJobManager jobManager, ILogger<MyStartupAction> logger)
{
_scheduledJobManager = scheduledJobManager;
_jobManager = jobManager;
_logger = logger;
}

Expand All @@ -25,7 +25,7 @@ public async Task RunAsync(CancellationToken cancellationToken = default)
await Task.Delay(500);
}

_scheduledJobManager.AddOrUpdate("MyJob", "* * * * *", async () =>
_jobManager.AddOrUpdate("MyJob", "* * * * *", async () =>
{
_logger.LogInformation("Running MyJob");
await Task.Delay(1000);
Expand Down
8 changes: 4 additions & 4 deletions src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ public static IServiceCollection AddJobScheduler(this IServiceCollection service
if (!services.Any(s => s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService)))
services.AddTransient<IHostedService, ScheduledJobService>();

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

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

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Foundatio.Extensions.Hosting.Jobs;

public interface IScheduledJobManager
public interface IJobManager
{
void AddOrUpdate<TJob>(string cronSchedule, Action<ScheduledJobOptionsBuilder> configure = null) where TJob : class, IJob;
void AddOrUpdate(string jobName, string cronSchedule, Action<ScheduledJobOptionsBuilder> configure = null);
Expand All @@ -23,9 +23,12 @@ public interface IScheduledJobManager
void AddOrUpdate(string jobName, string cronSchedule, Action action, Action<ScheduledJobOptionsBuilder> configure = null);
void Remove<TJob>() where TJob : class, IJob;
void Remove(string jobName);
JobStatus[] GetJobStatus();
Task RunJobAsync<TJob>(CancellationToken cancellationToken = default) where TJob : class, IJob;
Task RunJobAsync(string jobName, CancellationToken cancellationToken = default);
}

public class ScheduledJobManager : IScheduledJobManager
public class JobManager : IJobManager
{
private readonly IServiceProvider _serviceProvider;
private readonly ILoggerFactory _loggerFactory;
Expand All @@ -34,7 +37,7 @@ public class ScheduledJobManager : IScheduledJobManager
private ScheduledJobRunner[] _jobsArray;
private readonly object _lock = new();

public ScheduledJobManager(IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
public JobManager(IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
{
_serviceProvider = serviceProvider;
_loggerFactory = loggerFactory;
Expand Down
12 changes: 3 additions & 9 deletions src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@

namespace Foundatio.Extensions.Hosting.Jobs;

public class ScheduledJobService : BackgroundService, IJobStatus
public class ScheduledJobService : BackgroundService
{
private readonly IServiceProvider _serviceProvider;
private readonly ScheduledJobManager _jobManager;
private readonly JobManager _jobManager;

public ScheduledJobService(IServiceProvider serviceProvider, ScheduledJobManager jobManager)
public ScheduledJobService(IServiceProvider serviceProvider, JobManager jobManager)
{
_serviceProvider = serviceProvider;
_jobManager = jobManager;

var lifetime = serviceProvider.GetService<ShutdownHostIfNoJobsRunningService>();
lifetime?.RegisterHostedJobInstance(this);
}

public bool IsRunning { get; private set; } = true;

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var startupContext = _serviceProvider.GetService<StartupActionsContext>();
Expand All @@ -32,7 +27,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
var result = await startupContext.WaitForStartupAsync(stoppingToken).AnyContext();
if (!result.Success)
{
IsRunning = false;
throw new ApplicationException("Failed to wait for startup actions to complete");
}
}
Expand Down

0 comments on commit 31300b1

Please sign in to comment.