执行“服务器心跳” - 负载均衡

时间:2016-09-06 10:37:46

标签: sql-server asp.net-mvc scheduled-tasks recurring hangfire

我有以下内容: - 在负载平衡设置中运行的2个Web服务器。 - 其中每个都可以使用OwinStartup.cs中的以下代码将重复作业排入队列:

GlobalConfiguration.Configuration.UseSqlServerStorage("ConnectionString")
                .UseFilter(new LogFailureAttribute());
            app.UseHangfireServer();

 RecurringJob.AddOrUpdate("Job", () =>FunctionTrigger(), Cron.Daily(5, 0));

FunctionTrigger是一个使用无状态事务会话将记录插入数据库的简单函数。

因此,当我使用一台服务器成功运行作业时,但在负载均衡时会发生错误并且没有记录插入数据库。

这是日志错误:

Hangfire.Server.ServerHeartbeat - Error occurred during execution of 'Server Heartbeat' component. Execution will be retried (attempt 2 of 2147483647) in 00:00:03 seconds.
System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Hangfire.SqlServer.SqlServerStorage.GetConnection()
at Hangfire.Server.ServerHeartbeat.Execute(CancellationToken cancellationToken)
at Hangfire.Server.AutomaticRetryServerComponentWrapper.ExecuteWithAutomaticRetry(CancellationToken cancellationToken)

我正在使用Hangfire 1.4.5

**这是因为作业在每台服务器上运行两次?我怎么能避免这种情况。 ?! ** 感谢。

1 个答案:

答案 0 :(得分:1)

我一直面临同样的问题,经过一些搜索后,我在hangfire discussion上找到了这个讨论,因为Hangfire版本的错误发生了。 1.6.4。 该问题已在1.6.4 release上修复。 希望如果你还有问题可以帮助你。