Language: C#
View on GitHub to download or comment.
See the User script help topic for additional information.
This script can be used to send a notification message when jobs are found that have been put on hold.
Each time the script runs it creates a list of all held jobs, and stores this list in a file. It compares the list to the list from the previous run of the script to detect jobs that have been held since the last script run.
The script then places a list of the newly-held jobs in a Job Variable named "NewlyHeldJobs" and tells adTempus to execute the on-demand Response with the tag "NotifyForHeldJobs".
Create a new job. The user account used for the job must be an account that has permission to connect to adTempus and read all jobs.
Schedule the job to run at an appropriate interval.
Add a Script Execution step to the job. Set the script language to C# and paste in the sample code as the script body. Click OK to save the script.
In the step settings, go to the Responses tab and add a new Response. Add an Event for "Invoked by a script" and set the tag to NotifyForHeldJobs.
Add a Notification Task to the Response to send a message to the appropriate people. In the message body, insert the token %NewlyHeldJobs% where you want the list of jobs to appear.
The first time you run the job, the notification message will list all held jobs in the adTempus instance. Each subsequent run will only list jobs that have been put on hold since the previous run.
Find jobs that missed a scheduled execution because they were held
/*
This script finds all jobs that are held (only if held directly; it does not include jobs held because their group or queue is held).
It compares this list to the list of held jobs it found on the previous run. If any
newly-held jobs are found, it creates a list of them that it stores in the "NewlyHeldJobs" Job Variable. It then invokes the custom
Response "NotifyForHeldJobs", which your job can respond to to send a notification message.
*/
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using ArcanaDevelopment.adTempus.ApplicationIntegration;
using ArcanaDevelopment.adTempus.Client;
using ArcanaDevelopment.adTempus.Shared;
namespace UserScript
{
public class UserScript : ArcanaDevelopment.adTempus.ApplicationIntegration.UserScriptBase
{
public override Object Run()
{
//list of jobs that were held the last time the script ran
var jobsLastRun= new HashSet();
var instanceName=adTempus.JobVariables["InstanceName"];
//load the file containing the list of jobs we found last time, if it exists
var configPath = Environment.ExpandEnvironmentVariables(@"%LocalAppData%\Arcana Development\adTempus");
var configFileName=Path.Combine(configPath,$"job hold alert data - {instanceName}.dat");
if(!Directory.Exists(configPath))
{
Directory.CreateDirectory(configPath);
}
else if(File.Exists(configFileName))
{
//read the job IDs we saved last time
using(var file=File.OpenText(configFileName))
{
while(!file.EndOfStream)
{
var line=file.ReadLine();
if(Guid.TryParse(line, out var jobOID))
{
jobsLastRun.Add(jobOID);
}
}
}
}
//list of jobs that are held this time.
var jobsThisRun = new HashSet();
//jobs that are held this time and weren't held last time
var newlyHeldJobs=new List();
//connect to adTempus using the API. Note that the use account used for the job must have permission to log in to adTempus and view the job
using(var scheduler=Scheduler.Connect(adTempus.JobVariables["ServerAndInstanceName"],LoginAuthenticationType.Windows, "", ""))
{
using(var context=scheduler.NewDataContext())
{
bool restartPaging=true;
var filter=new JobFetchFilter();
//get all jobs that have any value set for HoldType other than NotHeld
filter.ExcludeHoldType = HoldType.NotHeld;
var jobs = context.GetJobs(filter,ObjectFetchOptions.FullFetch,0,ref restartPaging);
foreach(var job in jobs)
{
jobsThisRun.Add(job.OID.ObjectID);
if(!jobsLastRun.Contains(job.OID.ObjectID))
{
//job was not held last time the script ran
newlyHeldJobs.Add(job);
adTempus.LogMessage(MessageTypeEnum.Debug, 0, $"Found new held job: {job.FullyQualifiedName}");
}
else
{
adTempus.LogMessage(MessageTypeEnum.Debug, 0, $"Found previous held job: {job.FullyQualifiedName}");
}
}
}
}
//save the list of held jobs found this time
using (var file = File.CreateText(configFileName))
{
foreach(var jobOID in jobsThisRun)
{
file.WriteLine(jobOID);
}
}
if(newlyHeldJobs.Count()>0)
{
//make a list of the job names
var jobList=new StringBuilder();
foreach(var job in newlyHeldJobs)
{
jobList.AppendLine(job.FullyQualifiedName);
}
//store list of jobs in a Job Variable
adTempus.JobVariables.Add("NewlyHeldJobs", jobList.ToString());
adTempus.ExecuteOnDemandResponses("NotifyForHeldJobs");
}
return 0;
}
}
}
View on GitHub to comment.