How to develop and deploy DNN Schedule Task

Every now and then we all run into development of functionality in ASP.Net that requires do some scheduled tasks that need to be done at some pre-defined time and frequency. Same is true for development in DNN. Fortunately DNN framework provides a built in framework that allows you to develop custom scheduled tasks and then deploy them into the portal. The process is very straight farward. There are some caveats that you need to be aware of when developing schedules tasks in DNN. I will focus on those caveats in this post exclusively.

DNN Schedule Task Development

To get started you will need a .Net assembly developed that has a class that derives from ScheduleClient class. If your schedule task is part of an existing module then you can add a new class to the same module project. Otherwise you will need to create a new library project. Here is how new class definition may look like.

using DotNetNuke.Services.Scheduling;
public class ReminderScheduler : SchedulerClient
{
  public ReminderScheduler()
  { }

  public ReminderScheduler(ScheduleHistoryItem historyItem)
            : base()
  {
            this.ScheduleHistoryItem = historyItem;
  }
  public override void DoWork()
  {
  	this.Progressing();
    //   ... Add schedular implementation here ...
    this.ScheduleHistoryItem.Succeeded = true;
    this.Completed();
  }
}

As you can see that I have included Scheduling namespace in this class. You will need it for SchedulerClient definition. DNN uses reflection to create object of scheduler's class and passes ScheduleHistoryItem as constructor argument. It is important that implement the parameterized constructor for your class so can set the instance of this ScheduleHistoryItem object in your implementation. You will need this ScheduleHistoryItem object for lot of things during the implementation. For example to record log messages for your task you can use this object to record messages. DoWork is the method that gets executed when scheduler needs to run. You put all the implementation related to your scheduled task in this method.

You indicate progress of your task by calling Progressing method and then indicate finish by calling Completed method. It is best practice to call this method to inidcate start and finish to let DNN framework do its book keeping and clean up. You will set property Succeeded on ScheduleHistoryItem object. Now you can see why it is important to have parameterized constructor implemented. If you do not do so then any attempt to use ScheduleHistoryItem object will end up exception thrown from your scheduled task implementation.

No HttpContext

This is very important part of the implementation that you will have to understand. You may think that since scheduled task is deployed in your DNN portal there will always be web context to work with. That is not the case. These scheduled tasks are dispatched on separate threads. The context of these threads is not your web application. So if you try to use objects like HttpContext.Current you will end up with exception. Therefore it is very important that whatever implementation you put in place for your task, you do not rely on web context.

Deploying Schedule Task In DNN

You will have to log in with host account to access the menu item used to deploy schedule task(s). Click on Schedule option and then on that page click on Add Item to Schedule. You will be presented with UI that looks as below (as of version 5.6.2).

dnn schedule task

Fill the information as asked in that interface. It is very important that you provide fully qualified name of the class for your schedule task. Rest of the imformation is very self explanatory. Once your tasks is deployed it will run as per the schedule you specified.

comments powered by Disqus

Blog Tags