如何重启 Hangfire 服务器,或停止它并启动一个新服务器,以更改它正在处理的队列?

问题描述 投票:0回答:2

在我的 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 服务器
asp.net-core hangfire asp.net-core-5.0
2个回答
6
投票

停止正在运行的服务器

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",
  });

启动.cs

  services.AddSingleton<IBackgroundProcessingServer, BackgroundProcessingServer>();
  services.AddSingleton<IApplicationBuilder, ApplicationBuilder>();

这里查看完整的 POC

使用

JobStorage.Current.GetConnection().RemoveTimedOutServers(new TimeSpan(0, 0, 15));
更新/hangfire/servers


-1
投票

这是否可以在没有代码的情况下完成,例如进行某种 API 调用?

© www.soinside.com 2019 - 2024. All rights reserved.