I am currently implementing a monitoring job scheduling system. My application looks basically like following:
The user can create monitoring jobs with a start time (date & time) and an end time (also date & time). Those jobs being created are listed in a table. Each job has a status (Created, Running, Canceled and Finished). This looks like below.
In the backend I schedule these jobs every 5 seconds from start time till end time like following
// Build the job
IJobDetail job = JobBuilder.Create<MonitoringJob>()
.WithIdentity(request.Job.Id.ToString())
.UsingJobData(jobDataMap)
.Build();
// Build trigger to run every 5 seconds with start and end time
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(request.Job.Id.ToString())
.StartAt(request.Job.StartTime)
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
.EndAt(request.Job.EndTime)
.Build();
await scheduler.ScheduleJob(job, trigger);
In the frontend I want to display the current status of the job in the table (see image). The status are:
Created: Job is added but not yet executed by the scheduler (e.g. start time is in the future)
Running: Job is scheduled and currently running every 5 seconds
Canceled: Job has been canceled by the user scheduler.DeleteJob
is invoked on the backend side
Finished: Job has terminated successfully. The end time is reached.
I already tried to achieve this using the Listeners interfaces (ISchedulerListener, ITriggerListener & IJobListener
) but they are barely documentated about when each method is invoked. Can someone help me to figure out which listener and method I need to use in order to be able to change the status of each job in the frontend?
Thanks for helping!
In Quartz.NET, you can use the ITriggerListener
, IJobListener
and ISchedulerListener
interfaces to track the execution status of your jobs and update the frontend accordingly. Here's how you can use these listeners to achieve the desired functionality:
// Implement the ITriggerListener interface
public class CustomTriggerListener : ITriggerListener
{
public string Name => "CustomTriggerListener";
public async Task TriggerFired(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default)
{
// This method is called when a trigger is fired and a job is about to be executed.
// You can update the status of the job to "Running" here.
}
// Implement other methods of the ITriggerListener interface as needed
}
// Implement the IJobListener interface
public class CustomJobListener : IJobListener
{
public string Name => "CustomJobListener";
public async Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default)
{
// This method is called when a job is about to be executed.
// You can update the status of the job to "Running" here if necessary.
}
public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default)
{
// This method is called after a job has been executed.
// You can update the status of the job to "Finished" here if necessary.
}
// Implement other methods of the IJobListener interface as needed
}
// Implement the ISchedulerListener interface
public class CustomSchedulerListener : ISchedulerListener
{
public async Task JobScheduled(ITrigger trigger, CancellationToken cancellationToken = default)
{
// This method is called when a job is scheduled.
// You can update the status of the job to "Created" here if necessary.
}
public async Task JobUnscheduled(TriggerKey triggerKey, CancellationToken cancellationToken = default)
{
// This method is called when a job is unscheduled.
// You can update the status of the job to "Canceled" here if necessary.
}
// Implement other methods of the ISchedulerListener interface as needed
}
// Create an instance of the listeners
var triggerListener = new CustomTriggerListener();
var jobListener = new CustomJobListener();
var schedulerListener = new CustomSchedulerListener();
// Register the listeners with the scheduler
scheduler.ListenerManager.AddTriggerListener(triggerListener);
scheduler.ListenerManager.AddJobListener(jobListener);
scheduler.ListenerManager.AddSchedulerListener(schedulerListener);
Within the listener methods, you can access the job's information, such as its ID, using the IJobExecutionContext
object. You can then update the status of the job in your frontend based on its execution state.
Note: Make sure to handle exceptions appropriately within the listeners to prevent any issues with the scheduler.
By implementing the listeners as shown above, you should be able to track the execution status of your jobs and update the frontend accordingly.