【发布时间】:2020-11-11 18:33:21
【问题描述】:
我有一个 ASP.NET 核心应用程序,并希望将 Hangfire 用于一些后台任务。 我按照官方文档中的描述设置了 Hangfire,我可以调用通常的后台任务。 但是,我在延迟调用这些方法时遇到了问题。
public void Configure(/*other params*/ IBackgroundJobClient backgroundJobsClient)
{
//Other code
backgroundJobsClient.Enqueue(() => Console.WriteLine("Method"));
backgroundJobsClient.Schedule(() => Console.WriteLine("Delayed Method"), TimeSpan.FromSeconds(2));
}
我在控制台中只有“方法”输出。
等了一分钟后,我的控制台出现异常:
Hangfire.Processing.BackgroundExecution[0] 由于异常,Execution DelayedJobScheduler 现在处于 Failed 状态,将在 00:00:04 内重试执行 Hangfire.PostgreSql.PostgreSqlDistributedLockException:无法在资源“HangFire:locks:schedulepoller”上加锁:锁定超时。 在 Hangfire.PostgreSql.PostgreSqlDistributedLock.PostgreSqlDistributedLock_Init_Transaction(字符串资源,TimeSpan 超时,IDbConnection 连接,PostgreSqlStorageOptions 选项) 在 Hangfire.PostgreSql.PostgreSqlDistributedLock..ctor(字符串资源,TimeSpan 超时,IDbConnection 连接,PostgreSqlStorageOptions 选项) 在 Hangfire.PostgreSql.PostgreSqlConnection.AcquireDistributedLock(字符串资源,TimeSpan 超时) 在 Hangfire.Server.DelayedJobScheduler.UseConnectionDistributedLock[T](JobStorage 存储,Func
2 action) at Hangfire.Server.DelayedJobScheduler.EnqueueNextScheduledJobs(BackgroundProcessContext context) at Hangfire.Server.DelayedJobScheduler.Execute(BackgroundProcessContext context) at Hangfire.Server.BackgroundProcessDispatcherBuilder.ExecuteProcess(Guid executionId, Object state) at Hangfire.Processing.BackgroundExecution.Run(Action2 回调,对象状态) !!!无法锁定资源“HangFire:locks:schedulepoller”:锁定超时。
所以,我认为延迟方法初始化存在一些问题。 有人可以帮我吗?可能我没有为 ASP.NET 核心正确设置 Hangfire。
【问题讨论】:
-
似乎您的 postgress 数据库上出现了锁定超时:` 无法在资源 'HangFire:locks:schedulepoller' 上加锁:锁定超时`。你在使用
DisableConcurrentExecutionAttribute吗? -
感谢您的帮助。这个属性帮助我摆脱了异常(我用自己的方法包装了 Console.WriteLine 并将属性应用到它)。但是,我仍然看不到延迟任务的输出。
-
请参考this tutorial在Asp.net核心应用程序中使用Hangfire,我已经按照步骤配置设置和注册服务,那么延迟方法对我来说效果很好。最后,如果还是不行,最好把相关代码贴在ConfigureServices方法里,这样我们就知道你是怎么注册Hangfire服务的了。
-
感谢您的评论。当我开始使用 Hangfire 时,我阅读了本教程。我能够找到问题的原因。请看我的回答。
标签: c# .net postgresql asp.net-core hangfire