Click here to Skip to main content
15,063,166 members
Articles / All Topics
Technical Blog
Posted 5 Mar 2015

Tagged as

Stats

8K views
4 bookmarked

Job Tracking using Quartz for .NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
5 Mar 2015CPOL2 min read
Job Tracking using Quartz for .NET

A project that I recently worked on has a worker role that runs about 40 jobs. With some of these jobs running for hours, it became imperative that we knew when these jobs were running, if they were currently running, how long they were running, if they were overlapping with other dependent jobs, etc.

Below, I will show you how to easily setup Job Tracking using quartz-scheduler for .NET.

Listeners

Quartz.net provides us with the concept of Listeners. Listeners are objects that you create to perform actions based on events occurring within the scheduler. Quartz provides the following three listener interfaces:

  • ITriggerListener
  • IJobListener
  • ISchedulerListener

Each of these provides you with methods that get called on various events. More information on these can be found here and here.

Setup

First, you need to create the implementation of whatever listener you want.

C#
public class MyJobListener : IJobListener

Then you need to register the listener with your scheduler.

C#
var scheduler = SchedulerFactory.GetScheduler();
scheduler.ListenerManager.AddJobListener(myInstanceOfJobListener);
scheduler.ListenerManager.AddSchedulerListener(myInstanceOfScheduleListener);

All that is left is to implement the methods for the events you want to hook into. You can register multiple listeners of the same type.

Implementation

For my solution, I hooked into the following methods.

On IScheduleListener:

C#
public void JobScheduled(ITrigger trigger)
{
    var name = trigger.JobKey.Name;
    DateTime? nextStartDate = null;
    if (trigger.GetNextFireTimeUtc().HasValue)
    {
        nextStartDate = trigger.GetNextFireTimeUtc().Value.DateTime;
    }
    //Do something with the information
}

This method gets called each time a job is scheduled. I use this to keep track of all the jobs that are scheduled and can easily extract which jobs have never ran. If you need to, you can also extract the trigger type. You can then use the trigger type to form some kind of calendar view of when jobs will run.

On IJobListener:

C#
public void JobToBeExecuted(IJobExecutionContext context)
{
    var name = context.JobDetail.JobType.Name;
    DateTime startTime = context.FireTimeUtc.HasValue ? context.FireTimeUtc.Value.DateTime : DateTime.UtcNow;
    //do something with info
}

public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
{
    var name = context.JobDetail.JobType.Name;
    var runtime = context.JobRunTime;
    if(jobException != null)
    {
        //do something with error info
    }

    DateTime? nextRunDate = null;
    if(context.NextFireTimeUtc.HasValue)
    {
        nextRunDate = context.NextFireTimeUtc.Value.DateTime;
    }
    //do something with info
}

It is apparent that these methods get called just before a job executes and just after it finished. JobWasExecuted will get called even if there was an exception and quartz is nice enough to provide you with the exception object.

As mentioned, we use the data gathered to make sure our worker role is in good health. There are various other methods on these interfaces, so have a look at the linked documentation.


This post was first published on blog.entelect.co.za

This article was originally posted at http://feeds.feedburner.com/sneakycode

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SneakyPeet
Software Developer (Senior)
Unknown
I write about Domain Driven Design, Clean Code and .net

Comments and Discussions

 
-- There are no messages in this forum --