3
我們有一個Azure Scale Out資源,問題是:對於每個實例,Hangfire啓動一個新的服務器,任何方式只限於一臺服務器?Hangfire只允許一個服務器
我們有一個Azure Scale Out資源,問題是:對於每個實例,Hangfire啓動一個新的服務器,任何方式只限於一臺服務器?Hangfire只允許一個服務器
此問題的解決方法是:檢查SQLServer的,如果有一個活躍的遲髮型服務器:
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);
}
}
}
假設你正在使用SQL Server的存儲,這似乎是的話,你可以這樣做:
GlobalConfiguration.Configuration.UseSqlServerStorage("YourDB");
var servers = Hangfire.SqlServer.SqlServerStorage.Current
.GetMonitoringApi()
.Servers();
if (servers != null && servers.Count < 1)
//start new server here;
只是不啓動服務器呢?或者將服務器部署在單獨的應用程序中,而您只部署其中一個應用程序? – mason
如何確定是否有Hangfire正在運行?任何API方法? SQL Server選擇? TKS – Alexandre