Reputation: 23078
I am trying to gracefully end a Quartz job which run in IIS. My code is as follows:
[DisallowConcurrentExecution]
public class TestJob : IJob
{
private ILoggingService Logger { get; }
private IApplicationLifetime ApplicationLifetime { get; }
private static object lockHandle = new object();
private static bool shouldExit = false;
public TestJob(ILoggingService loggingService, IApplicationLifetime applicationLifetime)
{
Logger = loggingService;
ApplicationLifetime = applicationLifetime;
}
public Task Execute(IJobExecutionContext context)
{
//TODO: does not seem work
//context.CancellationToken.Register(() =>
//{
// lock (lockHandle)
// {
// shouldExit = true;
// }
//});
return Task.Run(() =>
{
//TODO: check
ApplicationLifetime.ApplicationStopping.Register(() =>
{
lock (lockHandle)
{
shouldExit = true;
}
});
try
{
for (int i = 0; i < 10; i ++)
{
lock (lockHandle)
{
if (shouldExit)
{
Logger.LogDebug($"TestJob detected that application is shutting down - exiting");
break;
}
}
Logger.LogDebug($"TestJob ran step {i+1}");
Thread.Sleep(3000);
}
}
catch (Exception exc)
{
Logger.LogError(exc, "An error occurred during execution of scheduled job");
}
});
}
}
protected void StartJobs(IApplicationBuilder app, IApplicationLifetime lifetime)
{
var scheduler = app.ApplicationServices.GetService<IScheduler>();
//TODO: use some config
QuartzServicesUtilities.StartJob<TestJob>(scheduler, TimeSpan.FromSeconds(60));
lifetime.ApplicationStarted.Register(() => scheduler.Start());
lifetime.ApplicationStopping.Register(() => scheduler.Shutdown());
}
private static void ConfigureApplicationLifetime(ILoggingService logger, IApplicationLifetime lifetime)
{
lifetime.ApplicationStopping.Register(() =>
{
logger.LogInfo(null, "Application is stopping...");
});
lifetime.ApplicationStopped.Register(() =>
{
logger.LogInfo(null, "Application stopped");
});
}
So, I have hooked to ApplicationStopping
to be able to shutdown Quartz jobs in a graceful way. However, when IIS application pool is about to be ended the jobs are ended abruptly:
Application is stopping
and immediately Application stopped
, thus the job is finished abruptlyI remember that a similar implementation worked in ASP.NET with Quartz 2.x: Quartz jobs were allowed to finalise their work provided they managed to do so within application pool shutdown period.
Question: How to gracefully end a Quartz 3.0.x with ASP.NET Core 2.2 job in IIS?
Upvotes: 2
Views: 1690