Terminate all running Jobs at a set daily time.

Tyler (2 posts)
December 3, 2020 07:36 AM
Accepted Answer

I am trying to figure out a way to terminate any active job's running at a daily given time, and was wondering the best method to achieve this option.

Scenario:

Currently jobs are broken down to morning, afternoon, and night time processing's.

Once or twice a month we run into an issue where the afternoon processes need to be terminated at 7:10pm as at 7:15pm night time processing jobs are scheduled to run.

Were currently having to have someone manually login at home at night at 7pm to check job statuses, and if a job has not completed to manually terminate "multiple jobs" prior to 7:15pm 

 

Any suggestions would be great, and thanks for looking.

Tyler

  
Bill Staff (599 posts)
December 3, 2020 10:23 AM
Accepted Answer

You can do this with a job that runs a script that uses the adTempus API to find and kill all the other jobs.

To do this, create a new job and schedule it for 7:10 PM. Important: this job has to run under a user account that has an adTempus login with permission to execute/terminate all jobs. Otherwise it won't be able to terminate the jobs, because the API authenticates all actions.

Add a step to this job to run a new script. In the script editor, change the language to C# and replace the script code with this code:

using System;
using System.Text;
using System.Collections.Generic;
using ArcanaDevelopment.adTempus.ApplicationIntegration;
using ArcanaDevelopment.adTempus.Shared;
using ArcanaDevelopment.adTempus.Client;

namespace UserScript
{
    public class UserScript : ArcanaDevelopment.adTempus.ApplicationIntegration.UserScriptBase
    {
        public override Object Run()
        {
            var sb = new StringBuilder();
            
            
            //connect to the local server
            using (var session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", ""))
            {
                using (var context = session.NewDataContext())
                {
                    var parms=new InstanceQueryParameters();
                    //Add WellKnownOIDs.RootGroup to look at all jobs.
                    parms.TargetObjects.Add(WellKnownOIDs.RootGroup);
                    
                    //look for all active instances
                    parms.Statuses.AddRange(JobStatusHelpers.ActiveStatuses);

                    //fetch all active job instances on the server
                    var instances=context.GetJobHistory(parms);
                    
                    foreach(ExecutionHistoryItem instance in instances)
                    {
                        if(!instance.OID.ObjectID.Equals(Parameters.ExecutionHistoryOID.Value))    //make sure we don't kill the job running the script
                        {
                            sb.AppendFormat("Job {0} instance {1}\r\n", instance.JobName, instance.InstanceID);
                            instance.Terminate(TerminationOptions.None);                
                        }
                    }
                }

                if(sb.Length==0)
                {
                    adTempus.LogMessage(MessageTypeEnum.Informational,0,"No running jobs found");    
                }
                else
                {
                    adTempus.LogMessage(MessageTypeEnum.Informational, 0, "Terminated the following jobs:\r\n" + sb.ToString());

                }
            }
            return 0;
         }
    
    }
}

This script uses the adTempus API to connect to the the server and query for all the active job instances. It then sends a terminate command to each of them. Finally it logs a message indicating which jobs/instances it terminated.

It's possible to refine the script to, for example, look only at jobs in a certain group.

 

 

Replies are disabled for this topic.