diff --git a/samples/Foundatio.HostingSample/Jobs/EveryMinuteJob.cs b/samples/Foundatio.HostingSample/Jobs/EveryMinuteJob.cs index 3f697a93..86f130e8 100644 --- a/samples/Foundatio.HostingSample/Jobs/EveryMinuteJob.cs +++ b/samples/Foundatio.HostingSample/Jobs/EveryMinuteJob.cs @@ -17,8 +17,7 @@ public EveryMinuteJob(ILoggerFactory loggerFactory) public async Task RunAsync(CancellationToken cancellationToken = default) { - if (_logger.IsEnabled(LogLevel.Information)) - _logger.LogInformation("EveryMinuteJob Run Thread={ManagedThreadId}", Thread.CurrentThread.ManagedThreadId); + _logger.LogInformation("EveryMinuteJob Run Thread={ManagedThreadId}", Thread.CurrentThread.ManagedThreadId); await Task.Delay(TimeSpan.FromSeconds(4)); diff --git a/samples/Foundatio.HostingSample/Jobs/Sample1Job.cs b/samples/Foundatio.HostingSample/Jobs/Sample1Job.cs index f8f09af7..98742768 100644 --- a/samples/Foundatio.HostingSample/Jobs/Sample1Job.cs +++ b/samples/Foundatio.HostingSample/Jobs/Sample1Job.cs @@ -19,8 +19,7 @@ public Sample1Job(ILoggerFactory loggerFactory) public Task RunAsync(CancellationToken cancellationToken = default) { Interlocked.Increment(ref _iterationCount); - if (_logger.IsEnabled(LogLevel.Information)) - _logger.LogTrace("Sample1Job Run #{IterationCount} Thread={ManagedThreadId}", _iterationCount, Thread.CurrentThread.ManagedThreadId); + _logger.LogTrace("Sample1Job Run #{IterationCount} Thread={ManagedThreadId}", _iterationCount, Thread.CurrentThread.ManagedThreadId); return Task.FromResult(JobResult.Success); } diff --git a/samples/Foundatio.HostingSample/Jobs/Sample2Job.cs b/samples/Foundatio.HostingSample/Jobs/Sample2Job.cs index 7a996780..dc7fbdcc 100644 --- a/samples/Foundatio.HostingSample/Jobs/Sample2Job.cs +++ b/samples/Foundatio.HostingSample/Jobs/Sample2Job.cs @@ -23,8 +23,7 @@ public Task RunAsync(CancellationToken cancellationToken = default) { _lastRun = DateTime.UtcNow; Interlocked.Increment(ref _iterationCount); - if (_logger.IsEnabled(LogLevel.Information)) - _logger.LogTrace("Sample2Job Run #{IterationCount} Thread={ManagedThreadId}", _iterationCount, Thread.CurrentThread.ManagedThreadId); + _logger.LogTrace("Sample2Job Run #{IterationCount} Thread={ManagedThreadId}", _iterationCount, Thread.CurrentThread.ManagedThreadId); return Task.FromResult(JobResult.Success); } diff --git a/samples/Foundatio.HostingSample/Jobs/SampleLockJob.cs b/samples/Foundatio.HostingSample/Jobs/SampleLockJob.cs new file mode 100644 index 00000000..2fc0e46a --- /dev/null +++ b/samples/Foundatio.HostingSample/Jobs/SampleLockJob.cs @@ -0,0 +1,32 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Foundatio.Caching; +using Foundatio.Jobs; +using Foundatio.Lock; +using Microsoft.Extensions.Logging; + +namespace Foundatio.HostingSample; + +[Job(Description = "Sample lock job", Interval = "5s")] +public class SampleLockJob : JobWithLockBase +{ + private readonly ILockProvider _lockProvider; + + public SampleLockJob(ICacheClient cache) + { + _lockProvider = new ThrottlingLockProvider(cache, 1, TimeSpan.FromMinutes(1));; + } + + protected override Task GetLockAsync(CancellationToken cancellationToken = default) + { + return _lockProvider.AcquireAsync(nameof(SampleLockJob), TimeSpan.FromMinutes(15), new CancellationToken(true)); + } + + protected override Task RunInternalAsync(JobContext context) + { + _logger.LogTrace("SampleLockJob Run Thread={ManagedThreadId}", Thread.CurrentThread.ManagedThreadId); + + return Task.FromResult(JobResult.Success); + } +} diff --git a/samples/Foundatio.HostingSample/Program.cs b/samples/Foundatio.HostingSample/Program.cs index 966e413a..edbccf63 100644 --- a/samples/Foundatio.HostingSample/Program.cs +++ b/samples/Foundatio.HostingSample/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Diagnostics; using System.Linq; using System.Threading; @@ -67,10 +67,12 @@ if (sample1) builder.Services.AddJob("Sample1", sp => new Sample1Job(sp.GetRequiredService()), o => o.ApplyDefaults().WaitForStartupActions().InitialDelay(TimeSpan.FromSeconds(4))); + builder.Services.AddJob(o => o.WaitForStartupActions().Name(nameof(SampleLockJob))); + if (sample2) { builder.Services.AddHealthChecks().AddCheck("Sample2Job"); - builder.Services.AddJob(o => o.WaitForStartupActions()); + builder.Services.AddJob(o => o.WaitForStartupActions().Name(nameof(Sample2Job))); } // if you don't specify priority, actions will automatically be assigned an incrementing priority starting at 0