在我的 asp.net 5 应用程序中,我使用 Hangfire 仅根据服务器应照顾的租户来处理某些队列。因此,在应用程序启动时,我会查找哪些队列并相应地启动 Hangfire 服务器。
public void ConfigureServices(IServiceCollection services)
{
// ... do stuff
services.AddHangfire(config => config
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseSqlServerStorage(hangfireConnectionString, new SqlServerStorageOptions
{
QueuePollInterval = new TimeSpan(0, 0, 0, 0, 500)
})
);
string[] queueNames = GetQueueNamesForThisServerToProcess();
services.AddHangfireServer(options =>
{
options.Queues = queueNames;
});
// ... do more stuff
}
稍后我想更改此服务器正在处理的队列。我think我需要停止这个hangfire服务器并启动另一个......但是a)我如何获得对这个hangfire服务器的引用,以及b)我应该如何正确启动一个新的,鉴于建议的方法使用
services.AddHangfireServer()
? 在 aspnet core 中启动 Hangfire 服务器
BackgroundProcessingServer注入后可以在任意controller中访问
_server.SendStop();
await _server.WaitForShutdownAsync(new System.Threading.CancellationToken());
_server.Dispose();
AddHangfireServer 可以触发访问注入的 IApplicationBuilder
_applicationBuilder.UseHangfireServer(new BackgroundJobServerOptions
{
WorkerCount = 1,
Queues = new[] { "customqueuename" },
ServerName = "Custom server 2",
});
services.AddSingleton<IBackgroundProcessingServer, BackgroundProcessingServer>();
services.AddSingleton<IApplicationBuilder, ApplicationBuilder>();
使用
JobStorage.Current.GetConnection().RemoveTimedOutServers(new TimeSpan(0, 0, 15));
更新/hangfire/servers
这是否可以在没有代码的情况下完成,例如进行某种 API 调用?