-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix RecurringJob schedule inconsistencies (#91)
* added more jobs to the workerService example * formatting * BackgroundJobRegistrations now state whether they're RecurringJobs or not * added method for retrieving recurring jobs from the scheduler * added logic for scheduling and running recurring jobs by using a timer * removed unnecessary namespace * added tests to cover new functionality * Added two new recurring job types: IRecurringJobWithInitialDelay and IRecurringJobWithNoInitialDelay * added test to verify amount of cronjob occurrences * update approved public api doc * added registration methods for IRecurringJobWithInitialDelay * Added DelegateRecurringJobWithInitialDelay * updated test to work with new scheduler * removed IRecurringJobWithNoInitialDelay * Public signature update * added more to backgroundservice test * remove unneeded line --------- Co-authored-by: Niels Pilgaard Grøndahl <[email protected]>
- Loading branch information
1 parent
2c01fe9
commit b42155d
Showing
25 changed files
with
494 additions
and
65 deletions.
There are no files selected for viewing
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,22 @@ | ||
using Cronos; | ||
using Pilgaard.BackgroundJobs; | ||
|
||
namespace BackgroundJobs.WorkerService; | ||
|
||
public class CronJob : ICronJob | ||
{ | ||
private readonly ILogger<CronJob> _logger; | ||
public CronJob(ILogger<CronJob> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task RunJobAsync(CancellationToken cancellationToken = default) | ||
{ | ||
_logger.LogInformation("{jobName} executed at {now:G}", nameof(CronJob), DateTime.Now); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public CronExpression CronExpression => CronExpression.Parse("* * * * *"); | ||
} |
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,22 @@ | ||
using Pilgaard.BackgroundJobs; | ||
|
||
namespace BackgroundJobs.WorkerService; | ||
|
||
public class OneTimeJob : IOneTimeJob | ||
{ | ||
private readonly ILogger<OneTimeJob> _logger; | ||
private static readonly DateTime _utcNowAtStartup = DateTime.UtcNow; | ||
public OneTimeJob(ILogger<OneTimeJob> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task RunJobAsync(CancellationToken cancellationToken = default) | ||
{ | ||
_logger.LogInformation("{jobName} executed at {now:G}", nameof(OneTimeJob), DateTime.Now); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public DateTime ScheduledTimeUtc => _utcNowAtStartup.AddMinutes(1); | ||
} |
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
using BackgroundJobs.WorkerService; | ||
using Pilgaard.BackgroundJobs; | ||
|
||
IHost host = Host.CreateDefaultBuilder(args) | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureServices(services => | ||
{ | ||
services.AddBackgroundJobs() | ||
.AddJob<RecurringJob>(nameof(RecurringJob)); | ||
.AddJob<RecurringJobEvery1Minute>() | ||
.AddJob<RecurringJobEvery5Minutes>() | ||
.AddJob<RecurringJobEvery10Minutes>() | ||
.AddJob<RecurringJobEvery30Minutes>() | ||
.AddJob<CronJob>() | ||
.AddJob<OneTimeJob>(); | ||
}) | ||
.Build(); | ||
|
||
host.Run(); | ||
.Build().Run(); |
42 changes: 21 additions & 21 deletions
42
...kgroundJobs.WorkerService/RecurringJob.cs → ...rkerService/RecurringJobEvery10Minutes.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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
using Pilgaard.BackgroundJobs; | ||
|
||
namespace BackgroundJobs.WorkerService; | ||
|
||
public class RecurringJob : IRecurringJob | ||
{ | ||
private readonly ILogger<RecurringJob> _logger; | ||
public RecurringJob(ILogger<RecurringJob> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task RunJobAsync(CancellationToken cancellationToken = default) | ||
{ | ||
_logger.LogInformation("{jobName} executed at {now:G}", nameof(RecurringJob), DateTime.Now); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public TimeSpan Interval => TimeSpan.FromMinutes(10); | ||
} | ||
using Pilgaard.BackgroundJobs; | ||
|
||
namespace BackgroundJobs.WorkerService; | ||
|
||
public class RecurringJobEvery10Minutes : IRecurringJob | ||
{ | ||
private readonly ILogger<RecurringJobEvery10Minutes> _logger; | ||
public RecurringJobEvery10Minutes(ILogger<RecurringJobEvery10Minutes> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task RunJobAsync(CancellationToken cancellationToken = default) | ||
{ | ||
_logger.LogInformation("{jobName} executed at {now:G}", nameof(RecurringJobEvery10Minutes), DateTime.Now); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public TimeSpan Interval => TimeSpan.FromMinutes(10); | ||
} |
21 changes: 21 additions & 0 deletions
21
samples/BackgroundJobs.WorkerService/RecurringJobEvery1Minute.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,21 @@ | ||
using Pilgaard.BackgroundJobs; | ||
|
||
namespace BackgroundJobs.WorkerService; | ||
|
||
public class RecurringJobEvery1Minute : IRecurringJob | ||
{ | ||
private readonly ILogger<RecurringJobEvery1Minute> _logger; | ||
public RecurringJobEvery1Minute(ILogger<RecurringJobEvery1Minute> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task RunJobAsync(CancellationToken cancellationToken = default) | ||
{ | ||
_logger.LogInformation("{jobName} executed at {now:G}", nameof(RecurringJobEvery1Minute), DateTime.Now); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public TimeSpan Interval => TimeSpan.FromMinutes(1); | ||
} |
21 changes: 21 additions & 0 deletions
21
samples/BackgroundJobs.WorkerService/RecurringJobEvery30Minutes.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,21 @@ | ||
using Pilgaard.BackgroundJobs; | ||
|
||
namespace BackgroundJobs.WorkerService; | ||
|
||
public class RecurringJobEvery30Minutes : IRecurringJob | ||
{ | ||
private readonly ILogger<RecurringJobEvery30Minutes> _logger; | ||
public RecurringJobEvery30Minutes(ILogger<RecurringJobEvery30Minutes> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task RunJobAsync(CancellationToken cancellationToken = default) | ||
{ | ||
_logger.LogInformation("{jobName} executed at {now:G}", nameof(RecurringJobEvery30Minutes), DateTime.Now); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public TimeSpan Interval => TimeSpan.FromMinutes(30); | ||
} |
21 changes: 21 additions & 0 deletions
21
samples/BackgroundJobs.WorkerService/RecurringJobEvery5Minutes.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,21 @@ | ||
using Pilgaard.BackgroundJobs; | ||
|
||
namespace BackgroundJobs.WorkerService; | ||
|
||
public class RecurringJobEvery5Minutes : IRecurringJob | ||
{ | ||
private readonly ILogger<RecurringJobEvery5Minutes> _logger; | ||
public RecurringJobEvery5Minutes(ILogger<RecurringJobEvery5Minutes> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task RunJobAsync(CancellationToken cancellationToken = default) | ||
{ | ||
_logger.LogInformation("{jobName} executed at {now:G}", nameof(RecurringJobEvery5Minutes), DateTime.Now); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public TimeSpan Interval => TimeSpan.FromMinutes(5); | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/Pilgaard.BackgroundJobs/Jobs/DelegateRecurringJobWithInitialDelay.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,25 @@ | ||
namespace Pilgaard.BackgroundJobs; | ||
|
||
/// <summary> | ||
/// A simple implementation of <see cref="IRecurringJobWithInitialDelay"/> which uses a provided delegate to | ||
/// implement the job. | ||
/// </summary> | ||
internal sealed class DelegateRecurringJobWithInitialDelay : IRecurringJobWithInitialDelay | ||
{ | ||
private readonly Func<CancellationToken, Task> _job; | ||
|
||
public TimeSpan Interval { get; } | ||
|
||
public TimeSpan InitialDelay { get; } | ||
|
||
public DelegateRecurringJobWithInitialDelay(Func<CancellationToken, Task> job, TimeSpan interval, TimeSpan initialDelay) | ||
{ | ||
_job = job ?? throw new ArgumentNullException(nameof(job)); | ||
Interval = interval; | ||
InitialDelay = initialDelay; | ||
} | ||
|
||
public Task RunJobAsync(CancellationToken cancellationToken = default) | ||
=> _job(cancellationToken); | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/Pilgaard.BackgroundJobs/Jobs/IRecurringJobWithInitialDelay.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,15 @@ | ||
namespace Pilgaard.BackgroundJobs; | ||
|
||
/// <summary> | ||
/// This interface represents a background job that runs at a specified interval, after an initial delay. | ||
/// </summary> | ||
public interface IRecurringJobWithInitialDelay : IRecurringJob | ||
{ | ||
/// <summary> | ||
/// The initial delay before triggering <see cref="IBackgroundJob.RunJobAsync"/> the first time. | ||
/// <para> | ||
/// Setting this to <see cref="TimeSpan.Zero"/> triggers it immediately on startup. | ||
/// </para> | ||
/// </summary> | ||
TimeSpan InitialDelay { get; } | ||
} |
Oops, something went wrong.