Skip to content

Instantly share code, notes, and snippets.

@fluxdigital
Last active September 15, 2022 23:34
Show Gist options
  • Save fluxdigital/23d224cccd5764be0ea783aac37e997c to your computer and use it in GitHub Desktop.
Save fluxdigital/23d224cccd5764be0ea783aac37e997c to your computer and use it in GitHub Desktop.
SiteCron Job to index blog posts
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