Anders G. Nordby

Lead Consultant at Itera

Episerver 7: ScheduledJob: Last Successful Run

In an Episerver 7 project I’m working on, I need a scheduled job to continue its work with all that has happened since its last run. So I created the following code to make the job figure out when it last had a successful run:

[ScheduledPlugIn(
	DisplayName = "Some Job",
	Description = "Some Job Reporting its Last Successful Run"
	)]
public class SomeJob : JobBase
{
	internal static Injected<ServiceAccessor<SchedulerDB>> DataAccess;

	private DateTime GetLastSuccessfulRun()
	{
		var maxDate = DateTime.MinValue;
		foreach (DataRow row in DataAccess.Service().ListLog(ScheduledJobId).Tables[0].Rows)
		{
			if (Converter.ToIntZero(row["Status"]) != 0)
				continue;

			var execution = ((DateTime)row["Exec"]).ToLocalTime();
			if (execution > maxDate)
				maxDate = execution;
		}

		return maxDate;
	}

	public override string Execute()
	{
		var lastSuccessfulRun = GetLastSuccessfulRun();
		return $"The job was last successfully run on {lastSuccessfulRun:s}";

		// TODO: Make the job do more than just report its last successful run...
	}
}

…of course the job (when it’s fully implemented) will do more than just report its last successful run…

Advertisements

One response to “Episerver 7: ScheduledJob: Last Successful Run

  1. Scott Reed 2017-10-05 at 22:50

    You can inject the IScheduledJobLogRepository and use that to get the logs returning back all the information including dates and completed status of a job. This is a new service as part of episerver 10 replacing the same functionality via the IScheduledJobStatusService pre 10.

    https://world.episerver.com/documentation/Class-library/?documentId=cms/10/89E55D99

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: