Getting all the active jobs from Quartz.NET scheduler
Asked Answered
A

2

12

How I can get all the active jobs scheduled in the Quartz.NET scheduler? I tried the GetCurrentlyExecutingJobs() but it is returning always 0.

Arber answered 11/7, 2011 at 10:31 Comment(2)
The method is not cluster aware, therefore, setting quartz.jobStore.clusteredto false will return the correct numberPurlin
Did you find a solution for this problem, I want the current running job in a clustered environment.Sue
S
8

That method doesn't seem to work.
The only solution I had found was to loop through all the jobs:

var groups = sched.JobGroupNames;
for (int i = 0; i < groups.Length; i++)
    {
        string[] names = sched.GetJobNames(groups[i]);
        for (int j = 0; j < names.Length; j++)
        {
             var currentJob = sched.GetJobDetail(names[j], groups[i]);
        }
    }

When a job is found it means that it is still active. If you set your job as durable, though, it will never be deleted if there are no associated trigger.
In that situation this code works better:

var groups = sched.JobGroupNames;
for (int i = 0; i < groups.Length; i++)
    {
        string[] names = sched.GetJobNames(groups[i]);
        for (int j = 0; j < names.Length; j++)
        {
            var currentJob = sched.GetJobDetail(names[j], groups[i]);
            if (sched.GetTriggersOfJob(names[j], groups[i]).Count() > 0)
            {
                // still scheduled.
            }
        }
    }

UPDATE:

I did some debugging to see what happens with GetCurrentlyExecutingJobs().
As a matter of fact it returns the job being executed but the elements are remove from the collection as soon as the job is executed.
You can check the 2 functions JobToBeExecuted and JobWasExecuted in the QuartzScheduler class.

Silurian answered 11/7, 2011 at 14:45 Comment(3)
I want to get the running jobs? The above method will return all the jobs that are paused and running.Arber
Did you find a solution for this problem, I want the current running job in a clustered environment.Sue
not exactly but this did help. thanksAerology
N
0

A simpler loop option would be to get all of the job keys and iterate over them. This implementation is for a minimal API example. It gets all JobKeys from the scheduler and then iterates over each on to get the details and execution schedule. More details available in this sample repo: QuartzScheduler. If a job doesn't have a schedule, or it's scheduled execution has completed and there are no future executions planned then the job will not be included in the list of returned jobs.

            app.MapGet("/schedules", async (ISchedulerFactory sf) =>
            {
                var scheduler = await sf.GetScheduler();
                var definedJobDetails = new List<JobDetailsDto>();

                var jobKeys = await scheduler.GetJobKeys(GroupMatcher<JobKey>.AnyGroup());
                foreach (var jobKey in jobKeys)
                {
                    var jobDetail = await scheduler.GetJobDetail(jobKey);
                    var jobSchedule = await scheduler.GetTriggersOfJob(jobKey);
                    if (jobDetail != null && jobSchedule != null)
                    {
                        definedJobDetails.Add(new JobDetailsDto(
                            jobDetail.Key.Name,
                            jobDetail.Key.Group,
                            jobDetail.Description,
                            jobSchedule.First().GetPreviousFireTimeUtc(),
                            jobSchedule.First().GetNextFireTimeUtc())
                        );
                    }
                }

                return definedJobDetails;
            })
Nadene answered 26/11, 2022 at 17:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.