Make Hangfire as Multi-Tenant

792 Views Asked by At

I am trying to create a scheduler using the library Hangfire. Created an ASP.NET Core API project added NuGet packages. (Using SQLServer for storage)

enter image description here

Added this code to set the server and storage

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    services.AddHangfire(config => config.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
    .UseSimpleAssemblyNameTypeSerializer()
    .UseDefaultTypeSerializer()

    .UseSqlServerStorage(Configuration["ConnectionStrings:HangfireDB"], new SqlServerStorageOptions
    {
        CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
        SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
        QueuePollInterval = TimeSpan.Zero,
        UseRecommendedIsolationLevel = true,
        DisableGlobalLocks = true,
        SchemaName = "Hangfire",
    }));

    services.AddHangfireServer();
    
    services.AddSingleton<IExecuteSimpleJob, ExecuteSimpleJob>();
}

And

public void Configure(
    IApplicationBuilder app,
    IWebHostEnvironment env,
    IBackgroundJobClient backgroundJobClient,
    IServiceProvider serviceProvider)
{
    //.... Removed some lines here to reduce no of lines
    // 

    app.UseHangfireDashboard();

    recurringJobManager.AddOrUpdate(
        "Execute Simple Job",
        () => serviceProvider.GetService<IExecuteSimpleJob>().Process(),
        "* * * * * ");
}

Now, I am trying to write an endpoint (controller) something like AppIp/ScheduleSampleJob and the request body looks something like

{
    "TenantID" : "T1",
    "Cron Expression" : "* * * * *"
}

I want to run the same job with different schedules for each tenant.

AND Importantly, If I want to see the dashboard with the jobs related to tenant T1 only.

0

There are 0 best solutions below