Open Closed

Configuring Hangfire with abp 4.2.1 #4463


User avatar
0
bqabani created
  • ABP Framework version: v4.2.1
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

I need to configure Hangfire with abp 4.2.1 I need to do the following: 1- Control the number of instances of each server 2- Divide jobs into queues, I want to add some queues dynamically 3- I need to add more servers (I can create a replica of the Host project), But I need to configure with job work on which server 4- I want to control the time span between jobs in the same queue 5- I want to be able to configure AddHangfireServer not only AddHangfire

Thank you


5 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Control the number of instances of each server

    Typically there is only one hangfire server per application instance, you can use AbpHangfireOptions to create hangfire server instance if you want.

    Divide jobs into queues, I want to add some queues dynamically

    ABP supports this in the 7.0 https://github.com/abpframework/abp/pull/13921. If you do not upgrade to the latest version, you need to use the Hangfire native API instead of Background job&worker.

    I need to add more servers (I can create a replica of the Host project), But I need to configure with job work on which server

    You don't care about it, Hangfire will handle it

    I want to control the time span between jobs in the same queue

    I didn't get it, you can check if hangfire supports this feature

    I want to be able to configure AddHangfireServer not only AddHangfire

    AddHangfireServer is not used inside ABP. but you still can configure HangfireServer via BackgroundJobServerOptions

    Configure<BackgroundJobServerOptions>(options=>
    {
        //.....
    });
    
  • User Avatar
    0
    bqabani created

    Thank you for your response, Can you provide me more details about the first point:

    Typically there is only one hangfire server per application instance, you can use AbpHangfireOptions to create hangfire server instance if you want.

    Currently I add hangfire using the following code:

    GlobalConfiguration.Configuration.UseSqlServerStorage(configuration.GetConnectionString("Default-hangfire"));
    context.Services.AddHangfire(config =>
    {
        config.UseSqlServerStorage(configuration.GetConnectionString("Default-hangfire"),  options: ops);
    });
    

    Thank you

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    For example:

    var options = serviceProvider.GetRequiredService<IOptions<AbpHangfireOptions>>().Value;
    var hangfireServer = options.BackgroundJobServerFactory.Invoke(serviceProvider);
    
    Configure<AbpHangfireOptions>(options =>
    {
        options.ServerOptions = new BackgroundJobServerOptions
        {
            WorkerCount = 10,
            ServerName = "myname",
            //.....
        };
    });
    
  • User Avatar
    0
    bqabani created

    I could make this work by using the following code:

    public override void PostConfigureServices(ServiceConfigurationContext context) { var configuration = context.Services.GetConfiguration(); var serverName = configuration.GetValue<string>("Schedule:ServerName"); var workerCount = configuration.GetValue<int>("Schedule:WorkerCount"); if (!string.IsNullOrEmpty(serverName) && workerCount > 0) { Configure<AbpHangfireOptions>(options => { options.ServerOptions = new BackgroundJobServerOptions { WorkerCount = workerCount, ServerName = serverName, Queues = new string[] { "default" , "logs"}, }; }); } base.PostConfigureServices(context); }

    Still cannot limit the execution time for job instance, so any job does not work more than 5 minutes for example

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Still cannot limit the execution time for job instance, so any job does not work more than 5 minutes for example

    It's Hangfire support this? can you explant it in detail thanks?

Made with ❤️ on ABP v9.1.0-preview. Updated on December 10, 2024, 06:38