Skip to content

Instantly share code, notes, and snippets.

@sclarson
Created March 31, 2011 15:59
Show Gist options
  • Save sclarson/896644 to your computer and use it in GitHub Desktop.
Save sclarson/896644 to your computer and use it in GitHub Desktop.
Stoppable EPiServer Schedule Job Example
using System.Linq;
using EPiServer;
using EPiServer.BaseLibrary.Scheduling;
using EPiServer.Core;
using EPiServer.PlugIn;
namespace BlendInteractive.AppCode.Plugins
{
[ScheduledPlugIn(DisplayName = "Delete Unpublished Faculty and Staff")]
public class DeleteUnpublishedPeople : JobBase
{
public DeleteUnpublishedPeople()
{
this.IsStoppable = true;
this.staffPages = 0;
this.facultyPages = 0;
this.stopped = false;
}
private int staffPages;
private int facultyPages;
private bool stopped;
public override void Stop()
{
this.stopped = true;
base.Stop();
}
public override string Execute()
{
var facultyList = Utilities.GetFilteredPageCollection(PageReference.RootPage, "Faculty Page").Where(p => p.Status != VersionStatus.Published);
var stafflist = Utilities.GetFilteredPageCollection(PageReference.RootPage, "Staff Page").Where(p => p.Status != VersionStatus.Published);
foreach (var person in facultyList)
{
DataFactory.Instance.MoveToWastebasket(person.PageLink);
facultyPages++;
this.OnStatusChanged();
if(stopped)
{
return Status();
}
}
foreach (var person in stafflist)
{
DataFactory.Instance.MoveToWastebasket(person.PageLink);
staffPages++;
this.OnStatusChanged();
if(stopped)
{
return Status();
}
}
this.OnStatusChanged();
return Status();
}
protected void OnStatusChanged()
{
this.OnStatusChanged("Deleted Pages: " + facultyPages + "Deleted Staff Pages: " + staffPages);
}
private string Status()
{
return "Deleted Pages: " + facultyPages + "<br />Deleted Staff Pages: " + staffPages + "<br />";
}
}
}
@gemortenblad
Copy link

Will when running base.Stop();, will the iteration be allowed to finish before the job is terminated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment