Table of Contents

Fetch recent job instances

These samples demonstrate how to retrieve recent job instances

The samples below show:

  1. Fetch all instances for the last 30 days for a job
  2. Fetch the most recent 5 instances for a job
  3. Fetch all instances for the last 30 days for all jobs. This sample also demonstrates how to use paging when fetching large amounts of data.

Sample 1

//fetch all instances for a particular job that finished in the last 30 days
void Main()
{
    using (var session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", ""))
    {
        using (var context = session.NewDataContext())
        {
            //fetch the job we want to report on
            var job=context.GetJob("My Job");
            
            InstanceQueryParameters queryParameters = new InstanceQueryParameters()
            {
                FetchOptions = ObjectFetchOptions.FullFetch // Use FullFetch because we are also getting LogMessages. Using StubsOnly is much slower.
            };

            //query the history for this job only
            queryParameters.TargetObjects.Add(job.OID);
            
            
            //look for instances within the last 30 days
            queryParameters.EndTimestamp=DateTime.Now;
            queryParameters.StartTimestamp=queryParameters.StartTimestamp.Value.Subtract(TimeSpan.FromDays(30));
            
            //only include instances that have finished execution
            queryParameters.Statuses.AddRange(JobStatusHelpers.FinalStatuses);
            
            //sort by Instance ID
            queryParameters.SortOrder.Add(InstanceQueryParameters.HistorySortOrder.InstanceIDDescending);
            
            //get the history
            var history=context.GetJobHistory(queryParameters);

            if (history != null)
            {
                foreach (var h in history)
                {
                    Console.WriteLine($"{h.JobName} instance {h.InstanceID}");
                    
                    foreach(var message in h.LogMessages)
                    {
                        //do something with log messages if you want.	
                    }
                }

            }
        }
    }
}

Sample 2

//fetch the most recent 5 completed instances for a particular job
void Main()
{
    using (var session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", ""))
    {
        using (var context = session.NewDataContext())
        {
            //fetch the job we want to report on
            var job=context.GetJob("My Job");
            
            InstanceQueryParameters queryParameters = new InstanceQueryParameters()
            {
                FetchOptions = ObjectFetchOptions.FullFetch // Use FullFetch because we are also getting LogMessages. Using StubsOnly is much slower.
            };

            //query the history for this job only
            queryParameters.TargetObjects.Add(job.OID);
            
            //only include instances that have finished execution
            queryParameters.Statuses.AddRange(JobStatusHelpers.FinalStatuses);
            
            //sort by Instance ID
            queryParameters.SortOrder.Add(InstanceQueryParameters.HistorySortOrder.InstanceIDDescending);
            
            //only fetch 5 instances. Since we are sorting by instance ID (descending), this will be the most recent 5 that
            //meet the other criteria
            queryParameters.PageSize=5;
            queryParameters.PageNumber=1;
            
            //get the history
            var history=context.GetJobHistory(queryParameters);

            if (history != null)
            {
                foreach (var h in history)
                {
                    Console.WriteLine($"{h.JobName} instance {h.InstanceID}");
                    
                    foreach(var message in h.LogMessages)
                    {
                        //do something with log messages if you want.	
                    }
                }

            }
        }

    }
}

Sample 3

//fetch all instances in the last 30 days for all jobs
void Main()
{
    using (var session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", ""))
    {
        using (var context = session.NewDataContext())
        {
            InstanceQueryParameters queryParameters = new InstanceQueryParameters()
            {
                FetchOptions = ObjectFetchOptions.FullFetch // Use FullFetch because we are also getting LogMessages. Using StubsOnly is much slower.
            };

            //Specify the RootGroup as the target to look at all jobs (this tells it to look at the root group, all child groups and contained jobs)
            queryParameters.TargetObjects.Add(WellKnownOIDs.RootGroup);
            
            
            //look for instances within the last 30 days
            queryParameters.EndTimestamp=DateTime.Now;
            queryParameters.StartTimestamp=queryParameters.StartTimestamp.Value.Subtract(TimeSpan.FromDays(30));
            
            //only include instances that have finished execution
            queryParameters.Statuses.AddRange(JobStatusHelpers.FinalStatuses);

            //sort by Instance ID
            queryParameters.SortOrder.Add(InstanceQueryParameters.HistorySortOrder.InstanceIDDescending);
            queryParameters.PageSize = 100;

            for(int i=1;;i++)
            {
                //if there are a lot of jobs running, 30 days of history may be too much for one request.
                //request the instances in sets of 100
                queryParameters.PageNumber=i;   //increment page number each call

                //get the history
                var history = context.GetJobHistory(queryParameters);

                if (history != null)
                {
                    foreach (var h in history)
                    {
                        Console.WriteLine($"{h.JobName} instance {h.InstanceID}");

                        foreach (var message in h.LogMessages)
                        {
                            //do something with log messages if you want.	
                        }
                    }
                }

                if(history.Count < queryParameters.PageSize)
                {
                    //if the method returned fewer items than requested, there are no more items
                    break;
                }
                else
                {
                    //there are more items to return. Continue the loop.	
                }

            }
        }
    }
}