Hide menu Last updated: Dec 27 2016
Area: Episerver CMS Applies to versions: 10 and higher
Other versions:

Scheduled jobs

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. 

How it works

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.

Default scheduled jobs

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. See the Episerver User Guide for more information on the built-in scheduled jobs.

Implementing a scheduled job

Scheduled jobs can be easily created using the Episerver Visual Studio extension. To implement a scheduled job, create a class that inherits from the EPiServer.Scheduler.ScheduledJobBase base class. Decorate the class with the ScheduledPlugInAttribute and define if the job should be enabled and what interval should be used initially. You can also assign a GUID to your job, making it possible to change the name or namespace of the job at a later time [New in Episerver CMS Core 10.3.0]. It is also possible to use a class that does not inherit the base class. Such a scheduled job is required to have a static method named Execute that doesn't take any arguments and 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", GUID = "d6619008-3e76-4886-b3c7-9a025a0c2603")]
    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:

Multi-site scenario

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.

Comments