-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various improvements to hosting scheduled jobs
- Loading branch information
Showing
15 changed files
with
389 additions
and
184 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Foundatio.Jobs; | ||
using Foundatio.Utility; | ||
|
||
namespace Foundatio.Extensions.Hosting.Jobs; | ||
|
||
internal class DynamicJob : IJob | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly Func<IServiceProvider, CancellationToken, Task> _action; | ||
|
||
public DynamicJob(IServiceProvider serviceProvider, Func<IServiceProvider, CancellationToken, Task> action) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_action = action; | ||
} | ||
|
||
public async Task<JobResult> RunAsync(CancellationToken cancellationToken = default) | ||
{ | ||
await _action(_serviceProvider, cancellationToken).AnyContext(); | ||
|
||
return JobResult.Success; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Foundatio.Caching; | ||
using Foundatio.Jobs; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Foundatio.Extensions.Hosting.Jobs; | ||
|
||
public class ScheduledJobManager | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly ILoggerFactory _loggerFactory; | ||
private readonly ICacheClient _cacheClient; | ||
|
||
public ScheduledJobManager(IServiceProvider serviceProvider, ILoggerFactory loggerFactory) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_loggerFactory = loggerFactory; | ||
_cacheClient = serviceProvider.GetService<ICacheClient>() ?? new InMemoryCacheClient(o => o.LoggerFactory(loggerFactory)); | ||
Jobs.AddRange(serviceProvider.GetServices<ScheduledJobRegistration>().Select(j => new ScheduledJobRunner(j.Schedule, j.Name, j.JobFactory, serviceProvider, _cacheClient, loggerFactory))); | ||
} | ||
|
||
public void AddOrUpdate<TJob>(string cronSchedule) where TJob : class, IJob | ||
{ | ||
string jobName = typeof(TJob).Name; | ||
var job = Jobs.FirstOrDefault(j => j.JobName == jobName); | ||
if (job == null) | ||
{ | ||
Jobs.Add(new ScheduledJobRunner(cronSchedule, jobName, sp => sp.GetRequiredService<TJob>(), _serviceProvider, _cacheClient, _loggerFactory)); | ||
} | ||
else | ||
{ | ||
job.Schedule = cronSchedule; | ||
} | ||
} | ||
|
||
public void AddOrUpdate(string jobName, string cronSchedule, Func<IServiceProvider, CancellationToken, Task> action) | ||
{ | ||
var job = Jobs.FirstOrDefault(j => j.JobName == jobName); | ||
if (job == null) | ||
{ | ||
Jobs.Add(new ScheduledJobRunner(cronSchedule, jobName, sp => new DynamicJob(sp, action), _serviceProvider, _cacheClient, _loggerFactory)); | ||
} | ||
else | ||
{ | ||
job.Schedule = cronSchedule; | ||
} | ||
} | ||
|
||
public void AddOrUpdate(string jobName, string cronSchedule, Func<CancellationToken, Task> action) | ||
{ | ||
AddOrUpdate(jobName, cronSchedule, (_, ct) => action(ct)); | ||
} | ||
|
||
public void AddOrUpdate(string jobName, string cronSchedule, Func<Task> action) | ||
{ | ||
AddOrUpdate(jobName, cronSchedule, (_, _) => action()); | ||
} | ||
|
||
public void AddOrUpdate(string jobName, string cronSchedule, Action<IServiceProvider, CancellationToken> action) | ||
{ | ||
AddOrUpdate(jobName, cronSchedule, (sp, ct) => | ||
{ | ||
action(sp, ct); | ||
return Task.CompletedTask; | ||
}); | ||
} | ||
|
||
public void AddOrUpdate(string jobName, string cronSchedule, Action<CancellationToken> action) | ||
{ | ||
AddOrUpdate(jobName, cronSchedule, (_, ct) => | ||
{ | ||
action(ct); | ||
return Task.CompletedTask; | ||
}); | ||
} | ||
|
||
public void AddOrUpdate(string jobName, string cronSchedule, Action action) | ||
{ | ||
AddOrUpdate(jobName, cronSchedule, (_, _) => | ||
{ | ||
action(); | ||
return Task.CompletedTask; | ||
}); | ||
} | ||
|
||
public void Remove<TJob>() where TJob : class, IJob | ||
{ | ||
string jobName = typeof(TJob).Name; | ||
var job = Jobs.FirstOrDefault(j => j.JobName == jobName); | ||
if (job != null) | ||
{ | ||
Jobs.Remove(job); | ||
} | ||
} | ||
|
||
public void Remove(string jobName) | ||
{ | ||
var job = Jobs.FirstOrDefault(j => j.JobName == jobName); | ||
if (job != null) | ||
{ | ||
Jobs.Remove(job); | ||
} | ||
} | ||
|
||
internal List<ScheduledJobRunner> Jobs { get; } = new(); | ||
} |
Oops, something went wrong.