Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@derantell
Last active November 8, 2017 08:43
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 derantell/35514601d1af5fc052e6050a4cea5c54 to your computer and use it in GitHub Desktop.
Save derantell/35514601d1af5fc052e6050a4cea5c54 to your computer and use it in GitHub Desktop.
EPiServer scheduled job template
using EPiServer;
using EPiServer.Logging.Compatibility;
using EPiServer.PlugIn;
using EPiServer.Scheduler;
using EPiServer.ServiceLocation;
using System;
using System.Collections.Generic;
namespace <namespace> {
// The docs: http://world.episerver.com/documentation/developer-guides/CMS/scheduled-jobs/
[ScheduledPlugIn(
DisplayName = <name>,
Description = <description>)]
public class <jobclass> : ScheduledJobBase {
public override string Execute() {
var log = SetupLog();
try {
// Job code here
}
catch (Exception e) {
log.Log("Job failed");
log.Log(e.ToString());
throw new Exception(log.ToString(), e);
}
return log.ToString();
}
public override void Stop() {
stopSignaled = true;
}
public <jobclass>() {
Epi = ServiceLocator.Current.GetInstance<IContentRepository>();
// Set to false if job is not stoppable
IsStoppable = true;
}
JobLog SetupLog() {
return new JobLog(m => {
OnStatusChanged(m);
EpiLog.Info(m);
});
}
class JobLog {
public JobLog(Action<string> logFunc) {
this.logFunc = logFunc;
messages = new List<string>();
}
public void Log(string format, params object[] args) {
var message = string.Format(format, args);
logFunc(message);
messages.Add(message);
}
public override string ToString() {
return string.Join("<br>", messages);
}
Action<string> logFunc;
List<string> messages;
}
static readonly ILog EpiLog =
LogManager.GetLogger(typeof(<jobclass>));
readonly IContentRepository Epi;
bool stopSignaled = false;
}
}
@derantell
Copy link
Author

Replace <...> with real values until the thing compiles.

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