Running a never ending singleton job

What’s the recommended setup for these requirements.

  • The job is never ending. It’s polling a server for updates and then updates internal storage
  • Only one job running at the same time. It will run in Azure env.
  • HF storage is SQL Server

I plan to use DisableConcurrentExecution to restict it to only one job instance.
DisableConcurrentExecution takes a mandatory parameter, timeoutInSeconds. When the timeout expires, will that cause another instance of the same job to be started? What happens with current job? Will there be several jobs running at the same time then?

Any other suggestions how to handle these requirements?

I have a similar requirement and am stumped. I am trying to fire and forget a background worker task when my web site starts, which constantly polls an amazon service for new work to process. I am not sure how to use Hangfire to do this.

Why would you want to use Hangfire to handle this? I’m not saying it’s impossible, it just doesn’t make a lot of sense to me.

Perhaps Hangfire is not the answer. What would you do?

I’d create a static bootstrapper class that encapsulates the polling logic along with error handling and so on and start it in Application_Start in global.asax and stop it in Application_End. Without more details, I can’t suggest a more specific solution, but that’s my general line of thinking.

Thanks for the feedback.
Would you just run it in the background as a TPL Task? Do I need to be concerned with AppDomain shutdowns?

It depends on the complexity and amount of work you need to do. If you’re only polling the AWS and not doing any processing, I wouldn’t be too concerned about AppDomain shutdowns.

What I’d do is fire a Timer to poll AWS every X minutes. Then if new data is available, enqueue Hangfire tasks to do the actual processing. And finally, mark the last fetched work item id, so that I don’t enqueue multiple jobs for the same work item. If you want to make it more resilient, in your Application_End method, you should ensure that all tasks have been flushed to Hangfire before returning.

1 Like