Getting the date of last successful execution of a scheduled job

by: Joel Abrahamsson

When building a scheduled job (a class with the ScheduledPlugIn attribute) it can sometimes be of interest to know when the job was last successfully run.

One such example is a job that fetches data from an external source and only needs to get data that has been updated since the last fetch. This might be accomplished by sending a parameter to the external source telling it the maximum age of items to respond with. As the external source can be offline or there might be communication problems we need to make sure that that parameter is not the last time the job was run but the last time it was successfully run.

The GetLastSuccessfulExecutionDate method

My solution to this is a method named GetLastSuccessfulExectionDate. it can either be a part of the scheduled jobs own class or implemented in an external class to make it reusable.

//Alternative one, implemented in the same class as the job
[ScheduledPlugIn]
public class MyScheduledJob
{
    private static DateTime? GetLastSuccessfulExecutionDate()
    {
        //Implementation
    }
}
 
//Alternative two, implemented in an external class
public static class ScheduledJobsUtility
{
    private static DateTime? GetLastSuccessfulExecutionDate(
        string methodName, Type type, Assembly assembly)
    {
        //Implementation
    }
}
 

Here I’ll show the first alternative.

Implementation

The first thing that we need to do is get a hold of the ScheduledJob object that corresponds to our job class. This is done with the static Load method in the ScheduledJob class. It has two overloads, one that expects the ID of the job, which we don’t know, and one that expects the method name, the type name and the assembly name, which we know.

Type thisType = typeof (MyScheduledJob);
string typeName = thisType.FullName;
string assemblyName =
    Assembly.GetAssembly(thisType).GetName().Name;
 
ScheduledJob thisJob =
    ScheduledJob.Load("Execute", typeName, assemblyName);

Once we have our ScheduledJob object we are able to see when it was last executed, and whether the last execution was successful with by querying it’s “LastExecution” and “HasLastExecutionFailed” properties. That’s not enough to figure out the date of last successful execution however. To do that we’ll have to retrieve the jobs log which we can do using the LoadLog method.

DataTable log = thisJob.LoadLog();

The LoadLog method returns a DataTable object containing four columns: Exec, Status, Text and StatusMessage. The Exec column contains the date when the job was run and the Status column contains an integer, which if it’s zero means that it was successful.

To get the date of last successful execution we filter the DataTable to only include rows where Status is 0, and sort it by the Exec column in a descending order. If the resulting DataRow array contains at least one row the first row will be contain information about the last successful execution.

DataRow[] successFullExecutions = log.Select(
    "Status = 0", "Exec DESC");
 
DateTime? lastSuccessfullExecution = new DateTime?();
if (successFullExecutions.Length > 0)
    lastSuccessfullExecution = 
        (DateTime)successFullExecutions[0]["Exec"];

The complete method

The complete method looks like this:

private static DateTime? GetLastSuccessfulExecutionDate()
{
    Type thisType = typeof(MyScheduledJob);
    string typeName = thisType.FullName;
    string assemblyName =
        Assembly.GetAssembly(thisType).GetName().Name;
 
    ScheduledJob thisJob =
        ScheduledJob.Load("Execute", typeName, assemblyName);
 
    DataTable log = thisJob.LoadLog();
    DataRow[] successFullExecutions = log.Select(
        "Status = 0", "Exec DESC");
 
    DateTime? lastSuccessfullExecution = new DateTime?();
    if (successFullExecutions.Length > 0)
        lastSuccessfullExecution = 
            (DateTime)successFullExecutions[0]["Exec"];
 
    return lastSuccessfullExecution;
}

Conclusion

The above code can be downloaded from here.

If anyone knows an easier way to do this, please let me know :)

15 March 2009


Comments

  1. Great post! Saved me some time
  2. What would be the status for success or failure?
Post a comment    
User verification Image for user verification  
EPiTrace logger