Hi,
is it possible to have a job to be recurring exactly after it has finished executing? That is, I dont want it to be timed. I would like to have it start again after it has finished a run (without the use ofcourse of a while(true); statement.
regards
Seems like you want to start your long-running background job when your application start and don’t finish it while your application alive? Am I correct?
Not exactly. I have a cleaning process which needs to be executed. When it finishes, I need it to wait for 5 seconds, and then it executes again ad infinitum.
Hangfire 1.5.0-beta1 provides support for custom background processes. They aren’t new to this version, Hangfire used them almost from the beginning, but they were internal. You can see examples – SchedulePoller, RecurringJobScheduler, Worker and so on. You can implement the IBackgroundProcess
interface and pass an instance to the BackgroundJobServer
ctor:
public class TempCleanupProcess : IBackgroundProcess
{
public void Execute(BackgroundProcessContext context)
{
// Clean temp folder
context.CancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(5));
}
}
using (new BackgroundJobServer(
new BackgroundJobServerOptions(),
JobStorage.Current,
new [] { new TempCleanupProcess() }))
{
}
Your process will be started on BackgroundJobServer
instantiation. The Execute
method will be called infinitely, while cancellation token is not signaled (it is signaled on server disposal). In case of an exception, all your logic will be retried.
Will it suit your needs?
I think so. Will try it out, thanks.