Skip to content

Commit

Permalink
Add job with lock to sample
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Oct 31, 2024
1 parent a5c7758 commit 7348c07
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
3 changes: 1 addition & 2 deletions samples/Foundatio.HostingSample/Jobs/EveryMinuteJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public EveryMinuteJob(ILoggerFactory loggerFactory)

public async Task<JobResult> 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));

Expand Down
3 changes: 1 addition & 2 deletions samples/Foundatio.HostingSample/Jobs/Sample1Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public Sample1Job(ILoggerFactory loggerFactory)
public Task<JobResult> 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);
}
Expand Down
3 changes: 1 addition & 2 deletions samples/Foundatio.HostingSample/Jobs/Sample2Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public Task<JobResult> 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);
}
Expand Down
32 changes: 32 additions & 0 deletions samples/Foundatio.HostingSample/Jobs/SampleLockJob.cs
Original file line number Diff line number Diff line change
@@ -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<ILock> GetLockAsync(CancellationToken cancellationToken = default)
{
return _lockProvider.AcquireAsync(nameof(SampleLockJob), TimeSpan.FromMinutes(15), new CancellationToken(true));
}

protected override Task<JobResult> RunInternalAsync(JobContext context)
{
_logger.LogTrace("SampleLockJob Run Thread={ManagedThreadId}", Thread.CurrentThread.ManagedThreadId);

return Task.FromResult(JobResult.Success);
}
}
6 changes: 4 additions & 2 deletions samples/Foundatio.HostingSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -67,10 +67,12 @@
if (sample1)
builder.Services.AddJob("Sample1", sp => new Sample1Job(sp.GetRequiredService<ILoggerFactory>()), o => o.ApplyDefaults<Sample1Job>().WaitForStartupActions().InitialDelay(TimeSpan.FromSeconds(4)));

builder.Services.AddJob<SampleLockJob>(o => o.WaitForStartupActions().Name(nameof(SampleLockJob)));

if (sample2)
{
builder.Services.AddHealthChecks().AddCheck<Sample2Job>("Sample2Job");
builder.Services.AddJob<Sample2Job>(o => o.WaitForStartupActions());
builder.Services.AddJob<Sample2Job>(o => o.WaitForStartupActions().Name(nameof(Sample2Job)));
}

// if you don't specify priority, actions will automatically be assigned an incrementing priority starting at 0
Expand Down

0 comments on commit 7348c07

Please sign in to comment.