Skip to content

Commit

Permalink
Add activity around job lock
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Oct 31, 2024
1 parent 7348c07 commit dd08d28
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Foundatio/Jobs/JobWithLockBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ public abstract class JobWithLockBase : IJob, IHaveLogger, IHaveTimeProvider
{
protected readonly ILogger _logger;
private readonly TimeProvider _timeProvider;
private readonly string _jobName;

public JobWithLockBase(ILoggerFactory loggerFactory = null)
{
_jobName = GetType().Name;
_logger = loggerFactory?.CreateLogger(GetType()) ?? NullLogger.Instance;
}

public JobWithLockBase(TimeProvider timeProvider, ILoggerFactory loggerFactory = null)
{
_jobName = GetType().Name;
_timeProvider = timeProvider ?? TimeProvider.System;
_logger = loggerFactory?.CreateLogger(GetType()) ?? NullLogger.Instance;
}
Expand All @@ -30,10 +33,16 @@ public JobWithLockBase(TimeProvider timeProvider, ILoggerFactory loggerFactory =

public virtual async Task<JobResult> RunAsync(CancellationToken cancellationToken = default)
{
var lockValue = await GetLockAsync(cancellationToken).AnyContext();
if (lockValue is null)
ILock lockValue;
using (var lockActivity = FoundatioDiagnostics.ActivitySource.StartActivity("Job Lock: " + _jobName))
{
return JobResult.CancelledWithMessage("Unable to acquire job lock");
lockActivity?.AddTag("job.id", JobId);

lockValue = await GetLockAsync(cancellationToken).AnyContext();
if (lockValue is null)
{
return JobResult.CancelledWithMessage("Unable to acquire job lock");
}
}

try
Expand Down

0 comments on commit dd08d28

Please sign in to comment.