Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all the active jobs from Quartz.NET scheduler

Tags:

quartz.net

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

like image 756
VJAI Avatar asked Jul 11 '11 10:07

VJAI


1 Answers

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.

like image 107
LeftyX Avatar answered Oct 26 '22 18:10

LeftyX