Skip to content

Instantly share code, notes, and snippets.

@john-mckillip
Last active September 17, 2018 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-mckillip/6a5b76e385d3886e4283f3054172cbc5 to your computer and use it in GitHub Desktop.
Save john-mckillip/6a5b76e385d3886e4283f3054172cbc5 to your computer and use it in GitHub Desktop.
An Episerver scheduled job that pulls news items from a feed and adds them to an Episerver Find index
namespace YourNamespace.Business.ScheduledJobs
{
using System;
using System.Linq;
using EPiServer.DataAbstraction;
using EPiServer.Find;
using EPiServer.PlugIn;
using EPiServer.Scheduler;
[ScheduledPlugIn(
DisplayName = "Index News Feed Items",
Description = "",
SortIndex = 0,
DefaultEnabled = true,
InitialTime = "1.1:0:0",
IntervalLength = 24,
IntervalType = ScheduledIntervalType.Hours)]
public class IndexNewsItems : ScheduledJobBase
{
private bool _stopSignaled;
private static IClient _client;
private static readonly ILogger _logger = LogManager.GetLogger();
public IndexNewsItems(IClient client)
{
_client = client;
IsStoppable = true;
}
/// <summary>
/// Called when a user clicks on Stop for a manually started job, or when ASP.NET shuts down.
/// </summary>
public override void Stop()
{
_stopSignaled = true;
base.Stop();
}
/// <summary>
/// Called when a scheduled job executes
/// </summary>
/// <returns>A status message to be stored in the database log and visible from admin mode</returns>
public override string Execute()
{
//Call OnStatusChanged to periodically notify progress of job for manually started jobs
OnStatusChanged("Starting execution of News Item Indexer");
try
{
// Get a list of all new news items from a feed
// Add your own implementation of FeedReader.GetLatestNewsFeed()
var newsItems = FeedReader.GetLatestNewsFeed();
// Loop through the items, get the detail object and add it to the index.
foreach (var item in newsItems)
{
// Add your own implementation of FeedReader.GetNewsFeedDeatils(? id)
// Would return your custom news item model that will get indexed
var detail = FeedReader.GetNewsFeedDeatils(item.ReleaseID);
detail.IsHiddenFromSearch = false;
// Add the item to your Find index
_client.Index(detail);
}
}
catch (Exception ex)
{
_logger.Error("Index News Item Error: " + ex.ToString());
return "Error. Check logs for details.";
}
//For long running jobs periodically check if stop is signaled and if so stop execution
if (_stopSignaled)
{
Stop();
return "Stop of job was called";
}
return "Success!";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment