0

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.

techrhl
  • 434
  • 8
  • 19
  • That should be quite easy. Hangfire doesn't care about tenancy. Just generate a job key unique to that tenant and everything should be good. Example: `{TenantId}_SampleJob` – Camilo Terevinto Aug 27 '21 at 08:37
  • @CamiloTerevinto: Thanks for quick reply. I edited my question. What if I want to see only Tenant specific jobs on Dashboard. – techrhl Aug 27 '21 at 08:41
  • 1
    Should also be simple, get all jobs that start with `{TenantId}_`. Of course, that assumes you know the TenantId via authentication or some other mechanism – Camilo Terevinto Aug 27 '21 at 08:43
  • 1
    One thing to be aware of is that IIS (assuming this site is hosted that way) will suspend the process hosting your website after a certain amount of inactivity. That would include your Hangfire server if this site is its host. If the recurring job absolutely needs to run, you'll want something like a Windows service to host the Hangfire server; the website can still host the dashboard and enqueue jobs – Tieson T. Aug 27 '21 at 08:47
  • @CamiloTerevinto **Should also be simple, get all jobs that start with {TenantId}_**. Can you help me with HOW to do it? – techrhl Aug 27 '21 at 08:53
  • Sure, some searches: https://stackoverflow.com/questions/54141581/how-to-get-list-of-all-hangfire-jobs-using-jobstorage-in-c | https://stackoverflow.com/questions/51779394/hangfire-net-core-get-enqueued-jobs-list | https://duckduckgo.com/?q=c%23+hangfire+get+jobs+by+key+site%3Astackoverflow.com&ia=web -> you should be able to get an idea from there – Camilo Terevinto Aug 27 '21 at 08:56
  • @CamiloTerevinto: Yes. I should be having Tenant's name through auth info. Unfortunately, I am not able to make out anything through your links. Looks like I need to read more. Can you confirm again, we are talking about applying filters on Dashboard (view level) ? – techrhl Aug 27 '21 at 09:59
  • Ah, you're talking about the dashboard, sorry, I didn't see that... I haven't done that, so can't confirm whether it's possible or not. Might be worth looking at the Hangfire docs or its API – Camilo Terevinto Aug 27 '21 at 10:02

0 Answers0