Scheduled jobs in EPiServer CMS 6

May 13, 2010

There are a few hidden bits in EPiServer CMS 6 that hasn’t been talked much about, one of them being the enhancements to the scheduled jobs. These new features include the ability to interrupt the execution of a long running job and for these jobs to send status updates to the UI.

To take advantage of these features your scheduled job first needs to inherit from the JobBase class in the EPiServer.BaseLibrary.dll. It will still need the SchedulesPlugIn attribute to be recognized as a scheduled job by EPiServer CMS. The next step is to set the IsStoppable property to true in the constructor. This is so that EPiServer knows that this job can be interrupted and that it should listen to status updates. Note that the status update feature only is available for jobs that can be interrupted. This shouldn’t cause any major issues but I would have preferred if this was separated from the stoppable feature.

using System;
using EPiServer.BaseLibrary.Scheduling;
using EPiServer.PlugIn;

namespace CodeExamples
{
  [ScheduledPlugIn(DisplayName="Interruptable job")]
  public class MyInterruptableJob : JobBase
  {
    public MyInterruptableJob() : base()
    {
      // Make the job interruptable
      this.IsStoppable = true;
    }

    public override string Execute()
    {
      throw new NotImplementedException();
    }
  }
}

Once you have compiled and deployed your job it should show up with a “Stop Job” button in the EPiServer Admin mode.

Screen shot showing job with stop button

Next step is to add the ability to actually stop your job when a user clicks the “Stop job” button. This is done by overriding the Stop method of the JobBase class. Below is the full example of a job that uses this method to interrupt the execution.

using System;
using EPiServer.BaseLibrary.Scheduling;
using EPiServer.PlugIn;

namespace CodeExamples
{
  [ScheduledPlugIn(DisplayName="Interruptable job")]
  public class MyInterruptableJob : JobBase
  {
    private bool _stop;

    public MyInterruptableJob() : base()
    {
      // Make the job interruptable
      this.IsStoppable = true;
    }

    public override string Execute()
    {
      // Keep count of total items
      int count = 0;

      while (count < 10)
      {
        // Check if the job should be stopped
        if (!_stop)
        {
          // Simulate long running job
          System.Threading.Thread.Sleep(5000);
        }
        count++;
      }

      // Return message indicating finished status
      return string.Format("Job completed after executing {0} items.", count);
    }

    public override void Stop()
    {
      _stop = true;
    }
  }
}

The final touch is to send a status message that informs the user of what is going on. This is done by calling  the OnStatusChanged method with a string describing the current status.

  public override string Execute()
  {
    // Keep count of total items
    int count = 0;
    while (count < 10)
    {
      // Check if the job should be stopped
      if (!_stop)
      {
        // Simulate long running job
        System.Threading.Thread.Sleep(5000);

        // Set status message
        this.OnStatusChanged(string.Format("Executed {0} item(s).", count));
      }
      count++;
    }

    // Return message indicating finished status
    return string.Format("Job completed after executing {0} items.", count);
  }

When running this job you should now see status messages being updated every fifth second.

Screen shot of status messages

Note that you should avoid excessive use of status messages as every status message is saved to the database. The UI is only updated every 5 seconds as well so more frequent status updates will be lost for users anyway.

Finally I should note that this code is just example code to illustrate these new features and not a recommendation on how you should implement your scheduled jobs. I will leave that for a(nother) rainy day.

Edit: Fixed double html encoded quotes.

Advertisements

9 Responses to “Scheduled jobs in EPiServer CMS 6”

  1. Per Nergard Says:

    Nice. This can come in handy very soon.


  2. Good post about an interesting feature!

    Can’t believe I’ve missed this one. Where did you read about it? In Reflector perhaps? :)


  3. Well, just saw the added features when running the new Link Validator, which currently is the only EPiServer job that uses them. Then it was just a matter of launching the EPiServer documentation, *cough*, Reflector, *cough*, and figure out how to use it yourself!

    There seems to be a few features in CMS 6 that EPiServer hasn’t been bothered telling anyone about! Makes it a bit more interesting to use :)

  4. Tom Stenius Says:

    Nice post Henrik! Keep it up.

  5. Paul Smith Says:

    Nice post. I have asked the EPiServer Documentation Team to make sure this gets documented.

    /Paul


  6. Thanks for sharing! :) Glad to hear the documentation gets updated as well. Next time I get the question “Any improvments in the scheduled jobs in CMS6?”, I can answer yes :)


  7. […] post: Scheduled jobs in EPiServer CMS 6 This entry was posted in EPiServer articles and tagged been-talked, cms, few-hidden. Bookmark the […]

  8. Pav Says:

    Can you set a start and end date when creating a scheduled Job?


Comments are closed.

%d bloggers like this: