From 31300b1a613597b845096978bac3307b0bd24589 Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Fri, 30 Aug 2024 15:58:56 -0500 Subject: [PATCH] Rename ScheduledJobManager to JobManager --- samples/Foundatio.HostingSample/Program.cs | 4 ++-- .../Startup/MyStartupAction.cs | 8 ++++---- .../Jobs/JobHostExtensions.cs | 8 ++++---- .../Jobs/{ScheduledJobManager.cs => JobManager.cs} | 9 ++++++--- .../Jobs/ScheduledJobService.cs | 12 +++--------- 5 files changed, 19 insertions(+), 22 deletions(-) rename src/Foundatio.Extensions.Hosting/Jobs/{ScheduledJobManager.cs => JobManager.cs} (95%) diff --git a/samples/Foundatio.HostingSample/Program.cs b/samples/Foundatio.HostingSample/Program.cs index bdd173cc..6ac2adaa 100644 --- a/samples/Foundatio.HostingSample/Program.cs +++ b/samples/Foundatio.HostingSample/Program.cs @@ -73,14 +73,14 @@ public static IHostBuilder CreateHostBuilder(string[] args) { e.MapGet("/jobstatus", httpContext => { - var jobManager = httpContext.RequestServices.GetRequiredService(); + var jobManager = httpContext.RequestServices.GetRequiredService(); var status = jobManager.GetJobStatus(); return httpContext.Response.WriteAsJsonAsync(status); }); e.MapGet("/runjob", async httpContext => { - var jobManager = httpContext.RequestServices.GetRequiredService(); + var jobManager = httpContext.RequestServices.GetRequiredService(); await jobManager.RunJobAsync("EvenMinutes"); await jobManager.RunJobAsync(); }); diff --git a/samples/Foundatio.HostingSample/Startup/MyStartupAction.cs b/samples/Foundatio.HostingSample/Startup/MyStartupAction.cs index f0f55439..e6837118 100644 --- a/samples/Foundatio.HostingSample/Startup/MyStartupAction.cs +++ b/samples/Foundatio.HostingSample/Startup/MyStartupAction.cs @@ -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 logger) + public MyStartupAction(IJobManager jobManager, ILogger logger) { - _scheduledJobManager = scheduledJobManager; + _jobManager = jobManager; _logger = logger; } @@ -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); diff --git a/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs b/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs index 1beff32c..14d986b1 100644 --- a/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs +++ b/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs @@ -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(); - if (!services.Any(s => s.ServiceType == typeof(ScheduledJobManager) && s.ImplementationType == typeof(ScheduledJobManager))) - services.AddSingleton(); + if (!services.Any(s => s.ServiceType == typeof(JobManager) && s.ImplementationType == typeof(JobManager))) + services.AddSingleton(); - if (!services.Any(s => s.ServiceType == typeof(IScheduledJobManager) && s.ImplementationType == typeof(ScheduledJobManager))) - services.AddSingleton(sp => sp.GetRequiredService()); + if (!services.Any(s => s.ServiceType == typeof(IJobManager) && s.ImplementationType == typeof(JobManager))) + services.AddSingleton(sp => sp.GetRequiredService()); return services; } diff --git a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs b/src/Foundatio.Extensions.Hosting/Jobs/JobManager.cs similarity index 95% rename from src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs rename to src/Foundatio.Extensions.Hosting/Jobs/JobManager.cs index fbad4849..beee03ed 100644 --- a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs +++ b/src/Foundatio.Extensions.Hosting/Jobs/JobManager.cs @@ -11,7 +11,7 @@ namespace Foundatio.Extensions.Hosting.Jobs; -public interface IScheduledJobManager +public interface IJobManager { void AddOrUpdate(string cronSchedule, Action configure = null) where TJob : class, IJob; void AddOrUpdate(string jobName, string cronSchedule, Action configure = null); @@ -23,9 +23,12 @@ public interface IScheduledJobManager void AddOrUpdate(string jobName, string cronSchedule, Action action, Action configure = null); void Remove() where TJob : class, IJob; void Remove(string jobName); + JobStatus[] GetJobStatus(); + Task RunJobAsync(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; @@ -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; diff --git a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobService.cs b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobService.cs index db521a86..2738ae84 100644 --- a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobService.cs +++ b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobService.cs @@ -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(); - lifetime?.RegisterHostedJobInstance(this); } - public bool IsRunning { get; private set; } = true; - protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var startupContext = _serviceProvider.GetService(); @@ -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"); } }