I have a task connected with jobs running in my ASP.NET Core project (.net 6.0 based). I need to run concrete job type with an unique identifier (for example, GUID as an unique marker) once delayed at 15 minutes, for example. But also I strongly need to have an ability to "rearm" this job. For example, a "Guided unique" job after 10 minutes (from 15 minutes delay) should be shifted in execution time on 15 minutes again. I'm trying to realize this in Quartz.NET, but have no Idea how to realize "unique" job running and rearming. IS it possible to do in Quartz.NET? I can use any of schedulers, but I strongly need jobs being able to store in database.
Can anyone helps me?
Thank you.
P.S. Sorry, if this question is too stupid. Earlier I user Coravel and it allows me to do anything I want except jobs storing.
CodePudding user response:
While it is generally not advisable to manipulate jobs during runtime, it is possible to reschedule and replace it with new jobs. Not sure if this is the right way to go about solving this, but it would solve your problem as I understand it.
CodePudding user response:
Consider this example, the idea is to use the method RescheduleJob to rearm the Job
class Program
{
static async Task Main(string[] args)
{
IScheduler scheduler = null;
try
{
var tcs = new CancellationTokenSource();
var endTask = new TaskCompletionSource<object>();
tcs.Token.Register(() => endTask.SetResult(null));
Console.CancelKeyPress = (sender, e) => tcs.Cancel();
var factory = new StdSchedulerFactory();
scheduler = await factory.GetScheduler();
await scheduler.Start(tcs.Token);
var job = JobBuilder.Create<MyJob>()
.Build();
var trigger = TriggerBuilder.Create()
.StartAt(DateTimeOffset.Now.AddSeconds(15))
.Build();
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} - Scheduled job in 15 seconds");
await scheduler.ScheduleJob(job, trigger, tcs.Token);
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} - Waiting 10 seconds...");
await Task.Delay(10000, tcs.Token);
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} - Rescheduling job in 20 seconds");
var delayedTrigger = TriggerBuilder.Create()
.StartAt(DateTimeOffset.Now.AddSeconds(20))
.Build();
await scheduler.RescheduleJob(trigger.Key, delayedTrigger, tcs.Token);
await endTask.Task;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (scheduler != null)
await scheduler.Shutdown();
}
}
}
class MyJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} - I'm the job!");
return Task.CompletedTask;
}
}