同时调用相同的静态方法时Hangfire DistributedLockTimeoutException

Jim*_*mbo 6 c# static-methods hangfire

我有一个Web服务,当发布到,会在Hangfire中排队下载图像,这样如果图像下载失败,Hangfire将自动重试:

[AutomaticRetry(Attempts = 5, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public static void DownloadImage(string url)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

每次发布Web服务时,它都会添加一些调用此方法的后台作业,如下所示:

Hangfire.BackgroundJob.Enqueue(() => Downloader.DownloadImage(o.SourceURL));
Run Code Online (Sandbox Code Playgroud)

我所看到的是,第一个后台作业成功,其余部分在RAPID继承中失败,即在不到一秒钟内,比如100个排队的作业将失败分配5次(根据AutomaticRetry属性)

Hangfire任务中的错误是:

Hangfire.Storage.DistributedLockTimeoutException

Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'HangFire:Downloader.DownloadImage' resource.

Hangfire.Storage.DistributedLockTimeoutException: Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'HangFire:Downloader.DownloadImage' resource.
   at Hangfire.SqlServer.SqlServerDistributedLock.Acquire(IDbConnection connection, String resource, TimeSpan timeout)
   at Hangfire.SqlServer.SqlServerDistributedLock..ctor(SqlServerStorage storage, String resource, TimeSpan timeout)
   at Hangfire.SqlServer.SqlServerConnection.AcquireDistributedLock(String resource, TimeSpan timeout)
   at Hangfire.DisableConcurrentExecutionAttribute.OnPerforming(PerformingContext filterContext)
   at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)
Run Code Online (Sandbox Code Playgroud)