Last active
September 15, 2022 23:34
SiteCron Job to index blog posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlogPostSearchIndexJob : IJob | |
{ | |
private readonly ISearchService _blogPostSearchService; | |
private readonly ILogService _logService; | |
public BlogPostSearchIndexJob() : | |
this(ServiceLocator.ServiceProvider.GetService<ISearchService>(), | |
ServiceLocator.ServiceProvider.GetService<ILogService>(), | |
{ | |
} | |
public BlogPostSearchIndexJob(ISearchService blogPostSearchService, ILogService logService) | |
{ | |
_blogPostSearchService = blogPostSearchService; | |
_logService = logService; | |
} | |
public void AddBlogPostContent(IJobExecutionContext jobContext) | |
{ | |
_logService.Info("BlogPostSearchIndexJob.AddBlogPostContent() - Start", this); | |
var job = GetJobDefinition(jobContext); | |
try | |
{ | |
//index content | |
var indexedCount =_blogPostSearchService.IndexContent(); | |
//log success message | |
UpdateLastRunLog(job, $" Last Job Execution ({DateTime.Now.ToString()}) completed successfully, Items Indexed: {indexedCount}"); | |
} | |
catch (Exception ex) | |
{ | |
_logService.Error($"Critical Error with blogPostSearchIndexJob.AddBlogPostContent(): {ex.InnerException}", ex, this); | |
//update job info to log error | |
UpdateLastRunLog(job, $" Last Job Execution ({DateTime.Now.ToString()}) errored: {ex.Message} "); | |
} | |
_logService.Info("blogPostSearchIndexJob.AddBlogPostContent() - End", this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment