我们有一个Azure Scale Out资源,问题是:对于每个实例,Hangfire都会启动一个新服务器,只能限制到一个服务器吗?
答案 0 :(得分:1)
假设您正在使用Sql Server存储,似乎就是这种情况,您可以这样做:
GlobalConfiguration.Configuration.UseSqlServerStorage("YourDB");
var servers = Hangfire.SqlServer.SqlServerStorage.Current
.GetMonitoringApi()
.Servers();
if (servers != null && servers.Count < 1)
//start new server here;
答案 1 :(得分:0)
此问题的解决方法是:检查SQLServer是否存在活动的Hangfire服务器:
using (var connection = new SqlConnection(myConnString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT MAX(LastHeartBeat) FROM HangFire.Server";
connection.Open();
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult))
{
if (reader.HasRows)
{
reader.Read();
DateTime lastBeat = reader.GetDateTime(0);
bool myResult = lastBeat >= DateTime.UtcNow.AddHours(-2);
}
}
}