Episerver : How To Set Up A Scheduled Task

A Scheduled Job / scheduled Task is the way Episerver provides developers to run some functionality periodically, daily, weekly monthly etc…. This s great for scraping content, updating exchange rates and all sort of other tasks and luckily creating a scheduled task is pretty straightforward:

1. Create a new class called whatever you want and inherit from JobBase.

2. Decorate the class with the ScheduledPlugIn attribute and add a display name.

3. The class has to override a public static method called “Execute”. This is the thing that will be called when the scheduled job is called.

4. You will also have to override the ToString() method. This method will be used to display a message back to a content editor after schedule job completed.

Skeleton Code

[ScheduledPlugIn(DisplayName = "Bulk Content Import",
SortIndex = 100)]
public class ContentImport : JobBase
{
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private int contentProcessed;
private int contentNotProcessed;
public ContentImport()
{
IsStoppable = true;
contentProcessed = 0;
contentNotProcessed = 0;
}
private long Duration { get; set; }
public override string Execute()
{
var tmr = Stopwatch.StartNew();
try
{
// Do Work here
}
catch(Exception ex)
{
contentNotProcessed = contentNotProcessed++;
Logger.Error(ex)
}
tmr.Stop();
Duration = tmr.ElapsedMilliseconds;
return ToString();
}
public override string ToString()
{
var logMesssage = contentNotProcessed > 0 ? "Please check the logs for the failed products." : string.Empty;
return string.Format(
"Processed {0} in {1}ms on {2}. {3}",
contentProcessed,
Duration,
Environment.MachineName,
logMesssage);
}
}

After you have added in your custom code you can then set-up and manually trigger the event by going into your Episerver admin and hitting the manual start button, as seen in the image:

Episerver_Admin_Scheduled_Job

Why is the HttpContext null inside my scheduled task?

In the Episerver architecture scheduled tasks are run interdependently in their own windows service. This means that all scheduled tasks run outside of IIS. This means that you will not have access to the HttpContext within a scheduled task which can be a pain at times.

One common property I always need is the applications path on the server for content migration. Luckily, Microsoft have provided a non HttpContext version to get a lot of these properties. If you need a value like the ApplicationPhysicalPath you can use HostingEnvironment in System.Web.Hosting, like so:

HostingEnvironment.ApplicationPhysicalPath.

Conclusion

In today’s guide we’ve covered what an Episerver scheduled task is, a means to run code on a scheduled basis outside of the website. Scheduled tasks can be very useful to send out emails, perform content migrations as well as a whole heap of other usefulness.

To create a scheduled task you need to inherit from JobBase and implement the required fields. When building a scheduled task I recommend reading How To Debug An Episerver Scheduled Task Or InitializationModule as a lot of developers I bump into struggle with this topic.

Code Sample

As always a fully working code sample can be downloaded from my Github page here.

What scheduled tasks have you worked on before ?  Leave your examples in the comments section below.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

1 reply

Trackbacks & Pingbacks

  1. […] written previously in How To Set Up A Scheduled Task about the basic’s of a scheduled […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *