HangfireIO / Hangfire

An easy way to perform background job processing in .NET and .NET Core applications. No Windows Service or separate process required
https://www.hangfire.io
Other
9.45k stars 1.71k forks source link

Questions about Hangfire with a console application #2255

Open SoftCircuits opened 1 year ago

SoftCircuits commented 1 year ago

I'm trying to determine if Hangfire will work for us. We have a .NET Core console application that performs various scheduled tasks.

Is there any way someone could address a couple of questions?

  1. It appears this requires that my console application is always running. Doesn't that mean that my application should really be a Windows service? So that it's always running?
  2. Can tasks be added, removed and modified while the application is running? And can they be modified from another application?
  3. When multiple tasks are scheduled in an overlapping manner, does Hangfire queue them up so that only one task is performed at a time?
cezar-pimentel commented 1 year ago

Hi there, how are you @SoftCircuits?

I do not consider myself an expert, but at this point I've used Hangfire in a lot of different ways, so let's get started!

1) It appears this requires that my console application is always running. Doesn't that mean that my application should really be a Windows service? So that it's always running?

R: Not necessarily. Yes, your application will need to stay running all the time, but it doesn't need to be a Windows Service. You can for an example create a ASP.Net Core 6 Application and set it to always run no matter what. Let me know if you need help explaining this better, but it's indeed possible, I have HF implemented using both ASP.Net and Windows Services as well, and they work the same way. I used both approaches just to have them on my "resume".

2) Can tasks be added, removed and modified while the application is running? And can they be modified from another application?

R: I'm not sure if I got what you meant, but if I did, the answer is yes. All the applications that I built using Hangfire have predefined Jobs implemented, and they are triggered by time or manually. In those applications if I needed to add a new job or remove, I would definitely need to change the code, recompile and redeploy. So, if you're planning on creating, changing or removing a schedule job during runtime, I'm not sure if that is possible, but I would say that nowadays, with all the dependency injection stuff, and how powerful C# is, that's probably possible. I will try to implement here a POC to check if that would work, when triggered for example by a Rest API, and I'll let you know.

3) When multiple tasks are scheduled in an overlapping manner, does Hangfire queue them up so that only one task is performed at a time?

R: You can create multiple "queues" to address your jobs, but if you keep them in the same queue, I believe they will be processed in order. I'm not 100% sure about this one, because the jobs that I've implemented don't concur between them. But I'm pretty sure there's a way to make them run concurrently.

SoftCircuits commented 1 year ago

@cezar-pimentel Thank you for the detailed reply.

Regarding a Window service, it is possible for a console application to crash and stop running. I'm thinking Windows services have options to restart if they ever shut down. Sorry, I don't know what you mean about setting my application to run no matter what. Would this recover it if it crashed?

cezar-pimentel commented 1 year ago

@cezar-pimentel Thank you for the detailed reply.

Regarding a Window service, it is possible for a console application to crash and stop running. I'm thinking Windows services have options to restart if they ever shut down. Sorry, I don't know what you mean about setting my application to run no matter what. Would this recover it if it crashed?

Yeah, you can configured a Windows Service to auto restart upon failure, or, if you're hosting your Hangfire app under IIS then you can also configured the IIS to auto start, and to auto restart upon failure as well. That's what I meant by always running no matter what.

If you need advice on how to do that, just let me know.

Cheers!