The Scheduler in DotNetNuke is a mechanism that enables developers to schedule tasks to run at defined intervals. It is implemented using the Provider pattern; therefore, it can easily be replaced without modifying core code. Creating a scheduled task is a fairly simple process. First, though, it's important to understand which types of tasks are suitable for the Scheduler.
Because the Scheduler is run under the context of the web application, it is prone to the same types of application recycles as a web application. In a web-hosting environment, it is a common practice to conserve resources by recycling the worker process for a site periodically. When this happens, the Scheduler stops running. Therefore, the tasks run by the Scheduler do not run 24 hours a day, 7 days a week. They are executed according to a defined schedule, but they can only be triggered when the worker process is alive. For this reason, you cannot specify that a task should run every night at midnight. It is not possible in the web environment to meet this type of use case. Instead, you can specify how often a task is run by defining the execution frequency for each task. The execution frequency is defined as every x minutes/ hours/days.
To create a scheduled task, you must create a class that inherits from DotNetNuke.Services.Scheduling .SchedulerClient. This class must provide a constructor and a DoWork method. An example of a scheduled task is shown in Listing 8-9. This sample scheduled task will move all event log files to a folder named with the current date. By configuring this scheduled task to run once per day, the log files will be automatically archived daily, which keeps the log file sizes manageable.
Listing 8-9: Scheduled Task Example
Public Class ArchiveEventLog Inherits DotNetNuke.Services.Scheduling.SchedulerClient Public Sub New(ByVal objScheduleHistoryItem As _ DotNetNuke.Services.Scheduling.ScheduleHistoryItem) MyBase.new() Me.ScheduleHistoryItem = objScheduleHistoryItem 'REQUIRED End Sub Public Overrides Sub DoWork() Try 'notification that the event is progressing 'this is optional Me.Progressing() 'OPTIONAL 'get the directory that logs are written to Dim LogDirectory As String LogDirectory = Common.Globals.HostMapPath + "Logs\" 'create a folder with today's date Dim FolderName As String FolderName = LogDirectory + Now.Month.ToString + "-" + _ Now.Day.ToString + "-" + Now.Year.ToString + "\" If Not IO.Directory.Exists(FolderName) Then IO.Directory.CreateDirectory(FolderName) End If 'get the files in the log directory Dim s As String() s = IO.Directory.GetFiles(LogDirectory) 'loop through the files Dim i As Integer For i = 0 To s.Length - 1 Dim OldFileInfo As New IO.FileInfo(s(i)) 'move all files to the new folder except the file 'used to store pending log notifications If OldFileInfo.Name <> _ "PendingLogNotifications.xml.resources" Then Dim NewFileName As String NewFileName = FolderName + OldFileInfo.Name 'check to see if the new file already exists If IO.File.Exists(NewFileName) Then Dim errMessage As String errMessage = "An error occurred archiving " + _ "log file to " + _ NewFileName + ". The file already exists." LogException(New _ BasePortalException(errMessage)) Else IO.File.Move(OldFileInfo.FullName, NewFileName) Me.ScheduleHistoryItem.AddLogNote("Moved " + _ OldFileInfo.FullName + _ " to " + FolderName + _ OldFileInfo.Name + ".") 'OPTIONAL End If End If Next Me.ScheduleHistoryItem.Succeeded = True 'REQUIRED Catch exc As Exception 'REQUIRED Me.ScheduleHistoryItem.Succeeded = False 'REQUIRED Me.ScheduleHistoryItem.AddLogNote(String.Format(_ "Archiving log files failed.", _ exc.ToString)) 'OPTIONAL 'notification that we have errored Me.Errored(exc) 'REQUIRED 'log the exception LogException(exc) 'OPTIONAL End Try End Sub End Class
After the class has been compiled into the bin directory, the task can be scheduled from the Scheduling module under the Host page (see Chapter 6 for details). It is important to include each of the lines of code in Listing 8-9 that is labeled REQUIRED. These collectively ensure both the exception handling and schedule management are handled uniformly throughout all scheduled tasks.