Try our conversational search powered by Generative AI!

Last run of a scheduled job

Vote:
 

Hello,

I'm trying to get the last run of a scheduled job.  I figured the IScheduledJobLogRepository would do the trick, but I'm having difficulty getting results back.  I've DI hands me an instance of IScheduledJobLogRepository and I use i tlike this:

`

            PagedScheduledJobLogResult lastRuns = _jobLog.GetAsync(this.ScheduledJobId, DateTime.Now.AddDays(-7).ToFileTimeUtc(), 30).Result;
            if(lastRuns != null)
            {
                var latestResult = lastRuns.PagedResult.OrderBy(lr => lr.CompletedUtc).FirstOrDefault();
                if(latestResult != null)
                {
                    _lastRun = latestResult.CompletedUtc.ToLocalTime();
                }                
            }    

`

The GetAsync method takes a long for the second parameter.  The documentation doesn't say much as to what that long is for other than "Index for where to start getting log items for.".  I figured it's a long date representation.  Even if I pass in 0 I get no results.  Does anyone have some advice or a better solution to get the last run time of a job?  Help would be greatly appreciated.

(I realize upon posting that I should also filter for successful jobs too)

#248402
Feb 09, 2021 16:17
Vote:
 

No specific advice, but maybe check out the code for the EPiServer Scheduled job overview plugin to see if it points you in the right direction? 

https://github.com/valdisiljuconoks/TechFellow.ScheduledJobOverview

Or, this long writeup about Epi Scheduled Jobs: https://blog.tech-fellow.net/2020/12/07/episerver-scheduled-jobs-under-the-hood/ 

#248404
Edited, Feb 09, 2021 17:04
Vote:
 

Thank you Daniel, I took a look at both examples.  I've seen that tech-fellow blog article before and it's a fantastic write-up.

The github link had this:

let lastLog = _logRepo.GetAsync(job.ID, 0, 1).GetAwaiter().GetResult().PagedResult.FirstOrDefault()

Which was similar to what I had.  My mistake was calling this code in the Immediate Window in VS:

_jobLog.GetAsync(this.ScheduledJobId, 0, 30).Result;

It returned 0 results.  When I put 0 as the second parameter in code, it worked great.

I did some experimenting to understand what that second parameter does.  It looks like the method pulls the list of logs in descending date order (newest to oldest).  If you supply a number in that second parameter, it'll trim that number of elements from the top of the data returned.

			PagedScheduledJobLogResult lastRuns = _jobLog.GetAsync(this.ScheduledJobId, 0, 30).Result;			
			if (lastRuns != null)
			{
				var latestResult = lastRuns.PagedResult.Where(lr => lr.Status == ScheduledJobExecutionStatus.Succeeded).OrderByDescending(lr => lr.CompletedUtc).FirstOrDefault();
				if(latestResult != null)
				{
					_lastRun = latestResult.CompletedUtc.ToLocalTime();
				}				
			}			

This is my code now.  It'll pull the top 30 log entries (to be safe) and filter for the latest successful one.

Incidentally, this will allow me to do a lot less processing in my job.  I'm pulling some items from SharePoint into Epi.  I'm looking at the updated date on the SharePoint items to see if they need to be inserted/updated.  If the job was successfully run, it's most likely the item was already updated/imported.

#248408
Edited, Feb 09, 2021 18:42
Vote:
 

No specific advice for your problem either :) But just aside note - do not use GetXXXAsync() and .Result in the same line. Try not to. If you are constrained in sync context - at least call .GetAwaiter().GetResult().

As I wrote in the post - hope that Episerver will improve API and feed us in with this sometimes important metadata about job's previous executions.

#248411
Edited, Feb 09, 2021 21:14
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.