Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Scheduled jobs in Episerver run in the background at preset time intervals and typically perform cleanup and updating tasks. A sample installation of Episerver has several predefined scheduled jobs that are administered in the admin view. You can customize and configure scheduled jobs and create your own.
During the initialization of Episerver, the system scans through jobs and checks for their next execution time. At the appointed time, the system executes the job. Alternatively, you can execute a job manually from admin view. Because scheduled jobs are executed on the site, the site's web server must be up and running. To ensure this, use the IIS feature "Application Initialization," or have a site supervisor periodically ping the site. Scheduled jobs are executed in an Anonymous context.
A standard Episerver installation comes with a set of built-in scheduled jobs, such as emptying the trash and managing the scheduled publishing of content. These jobs are available from the CMS administration view.
Scheduled jobs can be easily created using the Episerver Visual Studio extension. To implement a scheduled job, mark a class with the ScheduledPlugInAttribute. You should inherit the EPiServer.Scheduler.ScheduledJobBase base class. (If the class does not inherit the base class, it requires a static method named Execute without parameters that returns a string.)
Example: A basic scheduled with the possibility to stop a job by overriding Stop method, as well as the ability to report progress through the StatusChanged event.
using System;
using EPiServer.Core;
using EPiServer.PlugIn;
using EPiServer.Scheduler;
namespace MyEpiserverSite.Jobs
{
[ScheduledPlugIn(DisplayName = "ScheduledJobExample")]
public class ScheduledJobExample : ScheduledJobBase
{
private bool _stopSignaled;
public ScheduledJobExample()
{
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;
}
/// <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(String.Format("Starting execution of {0}", this.GetType()));
//Add implementation
//For long running jobs periodically check if stop is signaled and if so stop execution
if (_stopSignaled)
{
return "Stop of job was called";
}
return "Change to message that describes outcome of execution";
}
}
}
The example scheduled job as it appears in the admin view:
If several sites share a database, such as in a load-balanced scenario, you can control which site executes scheduled jobs. To do this, set the enableScheduler attribute to true on the applicationSettings configuration element on the site that should execute the jobs, and to false on the other sites.
If you configure several sites to run scheduled jobs, each job is scheduled for execution on all sites. However, during execution, the first site that starts executing a job marks it in the database as executing, so the other sites do not execute that job in parallel.
Last updated: Sep 21, 2015