Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add try-catch DistributedLockTimeoutException means another Hangfire … #2007

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/Hangfire.Core/Server/RecurringJobScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,29 +232,41 @@ private void TryEnqueueBackgroundJob(
string recurringJobId,
DateTime now)
{
using (connection.AcquireDistributedRecurringJobLock(recurringJobId, LockTimeout))
try
{
var recurringJob = connection.GetRecurringJob(recurringJobId, _timeZoneResolver, now);

if (recurringJob == null)
using (connection.AcquireDistributedRecurringJobLock(recurringJobId, LockTimeout))
{
RemoveRecurringJob(connection, recurringJobId);
return;
}
var recurringJob = connection.GetRecurringJob(recurringJobId, _timeZoneResolver, now);

Exception exception;
if (recurringJob == null)
{
RemoveRecurringJob(connection, recurringJobId);
return;
}

try
{
ScheduleRecurringJob(context, connection, recurringJobId, recurringJob, now);
return;
}
catch (BackgroundJobClientException ex)
{
exception = ex.InnerException;
}
Exception exception;

try
{
ScheduleRecurringJob(context, connection, recurringJobId, recurringJob, now);
return;
}
catch (BackgroundJobClientException ex)
{
exception = ex.InnerException;
}

RetryRecurringJob(connection, recurringJobId, recurringJob, exception);
RetryRecurringJob(connection, recurringJobId, recurringJob, exception);
}
}
catch (DistributedLockTimeoutException e) when (e.Resource.EndsWith($"lock:recurring-job:{recurringJobId}"))
{
// DistributedLockTimeoutException here doesn't mean that others weren't scheduled.
// It just means another Hangfire server did this work.
_logger.Log(
LogLevel.Debug,
() => $@"An exception was thrown during acquiring distributed lock the {recurringJobId} resource within {LockTimeout.TotalSeconds} seconds. The recurring job has not been handled this time.",
e);
}
}

Expand Down Expand Up @@ -419,4 +431,4 @@ private bool IsBatchingAvailable(IStorageConnection connection)
});
}
}
}
}