Find jobs that missed their scheduled executions because they were held

Language: C#

View on GitHub to download or comment.

See the Client API Examples Introduction for additional information and prerequisites.

Samples index

This script finds jobs that missed executions while they were held.

The script retrieves all jobs for the server and then examines those that are currently held. For each held job it retrieves the most recent execution (status.ExecutionFinish), then asks the server to calculate all expected executions beween the last execution and the current time. If any scheduled executions are found in that interval, we know that they were missed because the job was held.

sample.cs
void FindMissedJobs()
{
	var jobList = new StringBuilder();

	//if the adTempus server is on a different computer, replace "." with the name
	using (var session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", ""))
	{
		using (var context = session.NewDataContext())
		{
			//get all jobs on the server
			var jobs = context.GetJobs("*");
			foreach (var job in jobs)
			{
				if (HasMissedRuns(job))
				{
					jobList.AppendLine(job.FullyQualifiedName);
				}
			}
		}
	}

	Console.Write("Missed jobs: " + jobList.ToString());
}

//Determines whether the job should have run between its most recent execution and the current time.
bool HasMissedRuns(Job job)
{
	if (job.GetEffectiveHoldType() == HoldType.NotHeld)
	{
		//job is not currently held
		return false;
	}
	
	//job is held. See if any scheduled executions would have happened between the last execution and now if it weren't held
	var status = job.GetStatus();
	if (!status.ExecutionFinish.HasValue)
	{
		//job has never run before; ignore it
		return false;
	}

	//get a list of all calculated run times between the last execution and now.
	var parms = new JobQueryParameters();
	parms.TargetObjects.Add(job.OID);
	var runTimes = job.DataContext.GetJobExecutionTimes(parms, status.ExecutionFinish.Value.DateTime, job.DataContext.Scheduler.ServerTime.DateTime);
	if (runTimes.Count > 0)
	{
		//job should have run at least once
		return true;
	}
	else
	{
		return false;
	}
}

Comments

View on GitHub to comment.