How To Enable/Disable Episerver Scheduled Tasks In Code

If you need to run a background task within your Episerver website, then you'll likely create a scheduled task.  Scheduled tasks are a great way to run small bits of functionality on a regular basis, like an import task etc..  If you have ever needed, or wondered how to enable/disable a scheduled task in code, then this guide is for you.  

Also, another quick note before I continue which seems to bring a lot of confusion.  Episerver scheduled tasks will run in the same application pool as your website.  If you need to consistently run a lot of processor heavy tasks, which will consume a lot of your website's resources then I recommend that you look into using something like Hangfire instead.  If you need to run 5-10 jobs that take a few minutes to run once an hour, then you don't need to worry.  You might want to consider something like Hangfire, to processes busy orders within an e-commerce store for example.

How Do I Access My Scheduled Tasks In Code

If you want to start messing around with scheduled tasks in code, you'll need to know about the IScheduledJobRepository interface. The IScheduledJobRepository API will give you access to do things like delete, get and update the current tasks registered with Episerver.  To use the IScheduledJobRepository code, you can access it like this.

NOTE, using ServiceLocator in code is an anti-pattern, use constructor injection.

var scheduledJobRepository = ServiceLocator.Current.GetInstance<ScheduledJobRepository>();
var mySceduledTaks = scheduledJobRepository.List()

Like a lot of things Episerver, the code is pretty simple. If you want to disable a task, you can do something like this:

var repository = ServiceLocator.Current.GetInstance<ScheduledJobRepository>();
            var mytask = repository.List().FirstOrDefault(x => x.Name.Contains("My Scheduled Task Name"));
            mytask.IsEnabled = false;
            repository.Save(mytask);                  

Following this code, it's easy for you to start querying what jobs are running, disabling/enabling them where needed etc... There are also some useful properties on the scheduled task object, so you can query if they have been running.  Using the properties below, you could, for example, create a scheduled task that checked if all the other scheduled tasks ran successfully, if not, send an email for example.

            var lastExecutionTime = mytask.LastExecution;
            var isRunning = mytask.IsRunning;
            var hasLastExecutionFailed = mytask.HasLastExecutionFailed;
  

Info If you look at your Episerver database, you can see the data for these tasks within the tblSchedule table.

submit to reddit

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

Back to top