Configure Hangfire to call WebAPI via separate instance without writing code in API

61 Views Asked by At

We have lot of background jobs that used to run in console applications, which we have now moved to API calls. Everything is working fine and does the job.

We created a separate website with JSON configuration using Quartz.Net, and all API calls and schedules are defined in this website, without writing Quartz related code into every API.

However, with number of APIs increasing and complex CRON expressions (for example, a job should run every 2seconds from 9AM to 9:10AM, and then coming down to 10 seconds). This has created a very complex cron expressions and managing them is becoming difficult and load balancing is also issue here, as when we load balance this website, both servers fire together.

We thought of using Hangfire; however, I am absolutely new to it and don't know if, we can create a separate website just for HangFire and use configurations to call API at regular intervals?

Like, a completely separate URL, and not part of API code.

Or

There is something else that can do this for us. Cloud is not an option.

1

There are 1 best solutions below

0
J.Memisevic On

Well hangfire comes with built in UI (Hangfire dashboard) which can be easily hosted in your web api - with just few lines of code.

For hangfire server i would suggest to have windows service of some kind of worker that would host Hangfire server (Hangfire server is the one who is responsible for executing the job). U can always add hangfire server to web api also - but in my opinion that should be separate application.

Why am I saying this ?

Well hangfire has 2 phases.

  1. Phase Job initialization - enqueue of the job (which means saving job definition to database and setting the state for the job)

  2. Phase Job execution , that is where Hangfire server comes in play. It uses JobActivator to create an instance of your job and executes it.

So as you can see these could be two completely separated processes - but they don't have to be.

Hangfire has full support for Microsoft DI with extension methods. Everything is pretty straight forward.

Hangfire also has rich API so you can modify job schedule , schedule new one at specific times etc.