From 92b45fa94a8aee0e86b791481fba14ac533fd3e2 Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Wed, 4 Sep 2024 20:21:18 -0500 Subject: [PATCH 1/2] Couple job ergonomic improvements --- src/Foundatio.Extensions.Hosting/Jobs/JobOptionsBuilder.cs | 4 ++-- .../Jobs/ScheduledJobOptionsBuilder.cs | 4 ++-- src/Foundatio/Jobs/JobBase.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Foundatio.Extensions.Hosting/Jobs/JobOptionsBuilder.cs b/src/Foundatio.Extensions.Hosting/Jobs/JobOptionsBuilder.cs index 731b0584..708a5162 100644 --- a/src/Foundatio.Extensions.Hosting/Jobs/JobOptionsBuilder.cs +++ b/src/Foundatio.Extensions.Hosting/Jobs/JobOptionsBuilder.cs @@ -42,7 +42,7 @@ public HostedJobOptionsBuilder JobFactory(Func value) return this; } - public HostedJobOptionsBuilder RunContinuous(bool value) + public HostedJobOptionsBuilder RunContinuous(bool value = true) { Target.RunContinuous = value; return this; @@ -72,7 +72,7 @@ public HostedJobOptionsBuilder InstanceCount(int value) return this; } - public HostedJobOptionsBuilder WaitForStartupActions(bool value) + public HostedJobOptionsBuilder WaitForStartupActions(bool value = true) { Target.WaitForStartupActions = value; return this; diff --git a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobOptionsBuilder.cs b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobOptionsBuilder.cs index 37526e5a..bf2b6802 100644 --- a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobOptionsBuilder.cs +++ b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobOptionsBuilder.cs @@ -36,13 +36,13 @@ public ScheduledJobOptionsBuilder JobFactory(Func value) return this; } - public ScheduledJobOptionsBuilder WaitForStartupActions(bool value) + public ScheduledJobOptionsBuilder WaitForStartupActions(bool value = true) { Target.WaitForStartupActions = value; return this; } - public ScheduledJobOptionsBuilder Distributed(bool value) + public ScheduledJobOptionsBuilder Distributed(bool value = true) { Target.IsDistributed = value; return this; diff --git a/src/Foundatio/Jobs/JobBase.cs b/src/Foundatio/Jobs/JobBase.cs index a19a0b59..8c2aa3c6 100644 --- a/src/Foundatio/Jobs/JobBase.cs +++ b/src/Foundatio/Jobs/JobBase.cs @@ -9,7 +9,7 @@ namespace Foundatio.Jobs; public abstract class JobBase : IJob, IHaveLogger, IHaveTimeProvider { - private readonly TimeProvider _timeProvider; + protected readonly TimeProvider _timeProvider; protected readonly ILogger _logger; public JobBase(ILoggerFactory loggerFactory = null) : this(null, loggerFactory) From 2a7679b3f3a114e34845f30c7c91ee56f22e4858 Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Wed, 4 Sep 2024 20:25:59 -0500 Subject: [PATCH 2/2] Add timeprovider to JobWithLockBase --- src/Foundatio/Jobs/JobWithLockBase.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Foundatio/Jobs/JobWithLockBase.cs b/src/Foundatio/Jobs/JobWithLockBase.cs index d81317e9..8ca1e626 100644 --- a/src/Foundatio/Jobs/JobWithLockBase.cs +++ b/src/Foundatio/Jobs/JobWithLockBase.cs @@ -8,17 +8,25 @@ namespace Foundatio.Jobs; -public abstract class JobWithLockBase : IJob, IHaveLogger +public abstract class JobWithLockBase : IJob, IHaveLogger, IHaveTimeProvider { protected readonly ILogger _logger; + private readonly TimeProvider _timeProvider; public JobWithLockBase(ILoggerFactory loggerFactory = null) { _logger = loggerFactory?.CreateLogger(GetType()) ?? NullLogger.Instance; } + public JobWithLockBase(TimeProvider timeProvider, ILoggerFactory loggerFactory = null) + { + _timeProvider = timeProvider ?? TimeProvider.System; + _logger = loggerFactory?.CreateLogger(GetType()) ?? NullLogger.Instance; + } + public string JobId { get; } = Guid.NewGuid().ToString("N").Substring(0, 10); ILogger IHaveLogger.Logger => _logger; + TimeProvider IHaveTimeProvider.TimeProvider => _timeProvider; public virtual async Task RunAsync(CancellationToken cancellationToken = default) {