Hide menu Last updated: Apr 14 2016

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.

Implementing a scheduled job

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:

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

Tells me absolutely nothing about where and how to create a scheduled job!

Hi Bernard,

Sorry if there's any confusion with the documentation. This particular document does give a short example implementation of how you can create a scheduled job via VS extension and then implement a scheduled job by marking a class with the ScheduledPlugInAttribute and then inherit from EPiServer.BaseLibrary.Scheduling.JobBase base class. The code example is slightly incorrect as it should inherit from JobBase, not from ScheduledJobBase.

If anything is unclear with a particular doc, you can post some feedback on our forums: http://world.episerver.com/forum/.

I hope this helps!

I have updated the code example to inherit from JobBase instead of ScheduledJobBase.

@Asa Sundin

Documentation says 

[ObsoleteAttribute("Change base class to EPiServer.Scheduler.ScheduledJobBase")]

Also EPiServer.baselibrary is gone so should this now be inherited from "
"ScheduledJobBase"

Thanks,

Kashif

@Muhammad 

Thank you for pointing that out and catching that. I was the one mistaken, not Asa, so I will ask her to make that change. 

Text and code example have been updated to inherit from EPiServer.Scheduler.ScheduledJobBase.

Bernard

You need to understand that EpiServer uses reflection to scan all classes upon start up.  When it comes across a class that derives from ScheduledJobBase, it creates the scheduled job.