Last active
November 8, 2017 08:43
-
-
Save derantell/35514601d1af5fc052e6050a4cea5c54 to your computer and use it in GitHub Desktop.
EPiServer scheduled job template
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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace
<...>
with real values until the thing compiles.