when I create a new background task I want it to keep running every minute until a certain condition is met. In the method called by Hangfire I call out to a remote webservice asking for a status, and when it indicates the external system is complete doing it’s thing, then I want to stop running my background task as well.
I see that I can throw an exception in my method to force the task to rerun in a minute, but that feels messy.
What is the best practice in doing this? Can I indicate in a more friendly manner that the task needs to be run again?
You could just make one background job that checks for the condition in a loop, and when it isn’t met it creates another background job to do whatever minutely task you want it to do. In short, a background job can start other background jobs if you so desire.
I’m going to try to create a Recurring task that runs minutely to check, “is the document done yet?”. When the external service indicates that document is ready, then I’ll enqueue a new fire-and-forget task to “download the document”, and at the same time remove the first recurring job using RecurringJob.RemoveIfExists.