navigation
 Thursday, August 09, 2007

I've been looking at the "Scheduler" entry on the DNN Host menu for quite a while now, thinking about looking into it further and wondering how I might be able to take advantage of the promise that's there.

First, the bad news: the scheduler only checks if there's a scheduled activity that needs to be run when the DNN server gets hit. So, for instance, if you have a task that you want to run every ten minutes, and nobody hits your website for three hours in the middle of the night, the task will not run until the next time there's a web request to your site. There are some DNN ways to work around this - go to http://www.dotnetnuke.com/, and search for KeepAlive, which is a module that will automatically access your website just prior to Application_End.

Now the good news: writing a scheduled service is as easy as can be, once you know the tricks. Here's the code for a complete Hello World implementation, which you should add as a new class to your App_Code folder for the project you're working on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using DotNetNuke.Services.Scheduling;

namespace Falafel.WebPlanner
{
public class TestClass1 : SchedulerClient
{
public TestClass1(ScheduleHistoryItem objScheduleHistoryItem) : base()
{
this.ScheduleHistoryItem = objScheduleHistoryItem;
}

public override void DoWork()
{
try
{
this.ScheduleHistoryItem.Succeeded = true;
this.ScheduleHistoryItem.AddLogNote("Notification processing completed at " +
                  DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
}
catch (Exception exc)
{
this.ScheduleHistoryItem.Succeeded = false;
this.ScheduleHistoryItem.AddLogNote("Scheduled service failed");
this.Errored(ref exc);
}
}
}
}

Let's look at a couple of the things that are going on here. First, notice that the constructor includes a reference to a "ScheduleHistoryItem. This is used by the DNN system to pass success/failure information, as well as any messages that need to be logged. The other feature is the DoWork() method. This is the method that takes care of business for you, the method that is called every time the scheduler fires the event. Using the DoWork method as a springboard, you can accomplish anything you like programmatically.

After compiling this code into your application, the last thing you need to do is to schedule the event. Start your site from within the IDE, and log in as host. Go to the Host -> Schedule menu entry, and select "Add Item To Schedule".  The entry in the first text box is a little odd, asking you for the full class name and assembly. In the case of the example above, I enter Falafel.WebPlanner.TestClass1, App_SubCode_Falafel_WebPlanner. Set the period for the schedule to call for the time period you'd like (for the purposes of this demonstration, I chose every 15 seconds), and also (for demo purposes) select to retain schedule history for 10 iterations. Click the "Schedule Enabled" checkbox, and update.

Just to prove the point, if there's no activity on your system (and there probably should not be - you're doing this on a development system, aren't you?), leave the site alone for a minute or two. After waiting for this respectful length of time, go back to the main Host -> Schedule page, locate your entry, and click on history. You shouldn't see any more than one entry. If you click around on your website for a while, then inspect the history again, yo'll see that the scheduled events have fired on a (fairly) regular basis. Leave the site alone again for a while, and you'll notice that the events also have not fired.

In summary, the DNN scheduler is a pretty simple yet very useful tool to run scheduled tasks if either a) your site is an active one, or b) you use some method of keeping your site alive.

posted on August 9, 2007  #    by Rick Miller  Comments [0]
Related posts:
Caching craziness with DotNetNuke 4.3.5
Installing DotNetNuke 4.0 on your local machine
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, i, strike, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview