How to create DotNetNuke(DNN) Scheduler
Did you ever need to write disconnected process which should be run at a defined interval time? Commonly, when we are in such need - we use to write window service which is based on some timer mechanism where we set some interval for the timer and in its elapsed method we execute the process we want to run at defined interval.
When you work with DotNetNuke and you come across with such functionality that need tobe implemented - you can consider creating "DotNetNuke Scheduler". While creating a DNN Scheduler - you are actually creating a simple class library which would be built and hosted in the DNN Schedule environment to be executed at the defined interval time.
The possible scenario in which one need to look at such schedule functionality is:
[1] You may need to send some set of fixed newletter everyday at defiend time to your customers.
[2] You may need to read some of third party product feed to read regular update and modify your product database.
[3] You may need to send some system generated reports to your third party vendor at some regular intervals.
To Create a DNN Scheduler; you need to:
[1] Create a class library project with a class which inherits DotNetNuke.Services.Scheduling.SchedulerClient.
[2] Write the process which you wish to be executed at some interval and call it in DoWork() method. (You can consider the DoWork() method as entry point of the disconnected process execution.
[3] Build the class library project and get th dll.
[4] Copy the dll inside /bin directory of your DNN solution.
[5] Make an entry in DNN Schedule (Host menu > Schedule submenu) service and host the dll by giving proper namespace and the elapsed time.
Let us explore above step in some details.
First step is to create a class library for which you need to add a new class library project inside your DNN solution.

Once you add the class library; just rename/modify the namespce, classname and the class filename which is appropriate to your DNN Solution. Like I modified the namespace and classname as: YouProjectName.ModuleName.TestSchedule
The namespace is important to define correctly as it will be used later on to define the entry in DNN Schedule.
Next, you need to add DotNetNuke.Library (DotNetNuke.dll) reference to your class library by right click class library project in the solution explorer and "Add Reference.." option. Once you add the DotNetNuke.Library reference you would be able to access DNN namespaces and classes which would be require to access DNN related functions and methods.
After adding reference, you need to inherit "DotNetNuke.Services.Scheduling.SchedulerClient" abstract class and implement DoWork() overridable method.


Here, I am also adding a parameterized constructor to properly record the schedule history:

For ease, I am creating a simple "Hello World" function which would create a new txt file at the defined path specifying current date and time.
public override void DoWork()
{
try
{
//--start the pocessing
this.Progressing();
//--call the function/process that you wish to be executed
ExecuteHelloWorld();
//--intimate the schedule mechanism to write log note in schedule history
this.ScheduleHistoryItem.AddLogNote("HelloWorld Service Start. Success.");
this.ScheduleHistoryItem.Succeeded = true;
}
catch (Exception Ex)
{
//--intimate the schedule mechanism to write log note in schedule history
this.ScheduleHistoryItem.Succeeded = false;
this.ScheduleHistoryItem.AddLogNote("HelloWorld Service Start. Failed. " + Ex.ToString());
this.Errored(ref Ex);
}
}
//--function which needs tobe executed at specified interval
public void ExecuteHelloWorld()
{
try
{
System.IO.File.WriteAllText(@"C:\\_SCHEDULER\\test" + Guid.NewGuid().ToString() + ".txt",
"Hello World. The Time is - " + DateTime.Now.ToString());
}
catch (Exception Ex)
{
throw Ex;
}
}
If you look at the above code - The processing always start from DoWork() method where I am calling the ExecuteHelloWorld() function. This is the function which will be executed at the defiend interval. Here, I am also logging in the Schedule history which would help us to know the status of the schedule execution.
Thats it!. You are done with the coding part. Next, all you need to do is; build the class library project get the dll, copy inside /bin directory of DNN solution and host it under DNN Schedule. For that Go to "Host" menu > "Schedule" submenu option. Once you are there, you would see there are some schedule entries already exists in the list which are being used in DotNetNuke environment


You would see a list of scheduled processes with its name, current status (enabled or disabled), the frequency of the execution, the next run as well as link to see the "Execution History". To add our process; Click on the "Add Item to Schedule" link. You will be redirected to "Edit Schedule" page where in you need to add your process to schedule.

[1] Give "Friendly Name" of the schedule process which will be seen in the schedule list.
[2] Provide full classname with namespace along with the assembly name. For example, in our case it will be YouProjectName.ModuleName.TestSchedule,TestSchedule. Please note that you dont need to write ".dll" extension; just full classname and name of assembly (dll) followed by a comma separator. This entry should be correct.
[3] Check the checkbox to enable the schedule.
[4] Provide the interval time to execute the process
[5] Provide the retry interval time; incase of failure.
[6 ] Specify the number of records in "Retain Schedule History" for History listing.
[7] You can specify "Run On Event" which will show Global.asax events; incase if you want schedule to execute the process on a specific event of global.asax. For example, APPLICATION_START event.
[8 ] Check the checkbox "Catch Up Enabled"; incase if you want the schedule process tobe executed in a scenario like the webserver is ever out of service, when the webserver is back in service this event will run once for each frequency that was missed during the downtime.
Click on the Update and you will see the newly hosted process in DNN Schedule list. You can click on "History" link to see the history of the execution. On History page, you will be presented with detailed Hisory records which we logged in our class library. This will helps to know the status of scheduled process.

Here, some important note is that A DotNetNuke Scheduler can have 2 running modes:
[1] Request Mode - in which DotNetNuke Scheduler will execute and run each time ONLY when a Browser Request is made.
[2] Timer Mode - in which DotNetNuke Scheduler will execute and keep running behind the scene on the specified elapse time.
By default, "Request Mode" is set which you can change from "Host" Menu > "Host Setting" submenu > Other Setting section > and Scheduler Mode Dropdown.

Thats it!. I hope this would help a lot to one who needs to implement continuous background process execution in DNN environment.
Oh Yes!,, The output of our test Schedule :)
