Products

Solutions

Resources

Partners

Community

About

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

The Community Blog is a personal opinion of community members and by no means the official standpoint of DNN Corp or DNN Platform. This is a place to express personal thoughts about DNNPlatform, the community and its ecosystem. Do you have useful information that you would like to share with the DNN Community in a featured article or blog? If so, please contact .

The use of the Community Blog is covered by our Community Blog Guidelines - please read before commenting or posting.


Creating DotNetNuke Scheduled Jobs

So for a long time now I have been promising an article on creating DotNetNuke Scheduled jobs and finally I'm actually getting it written.  This article is actually the first in a series of two articles.  This one will start out with the programming process involved, and the general "manual" setup process to actually get the job going.  The second article will focus on using the DotNetNuke API's to be able to validate and configure a scheduled job from within a module, making the user setup process easier.

Overview of Scheduled Jobs

Before I get into the specific code for a DotNetNuke scheduled job I want to talk a little bit about what a job is, and how you are going to interact with it.  At a foundation level a DotNetNuke Scheduled Job is simply a class that inherits from the SchedulerClient base class.  As such you override a "DoWork" method that performs whatever custom action you desire.

The base class exposes a ScheduleHistoryItem class that provides a mechanism for your process to report success/failure and a method to report helpful information to users via the "History" reporting that is available within DotNetNuke.

Now, this all seems pretty simple right?  Well there is one common gotcha with scheduled jobs that you want to be aware of, and that is the impacts of the "Scheduler Mode" property within Host Settings.  Now the first question comes to mind is how would that change things, well the key is that if the scheduler is set to "Timer Mode", which is my recommended setting, Scheduled jobs will NOT have access to an HttpContext as they are processing.  This can make file access a bit harder.  In my next article I'll give more details regarding the process to handle this.

General Format

The following is a sample scheduled job class with all comments and custom code removed.

using System; using System.Collections.Generic;

using DotNetNuke.Services.Scheduling;

namespace YourNamespace
{
public class YourJob : SchedulerClient
{
public YourJob (ScheduleHistoryItem oItem)
: base()
{
this.ScheduleHistoryItem = oItem;
}


public override void DoWork()
{
try
{
//Perform required items for logging
this.Progressing();

//Your code goes here
//To log note
//this.ScheduleHistoryItem.AddLogNote("note")

//Show success
this.ScheduleHistoryItem.Succeeded = true;
}
catch (Exception ex)
{
this.ScheduleHistoryItem.Succeeded = false;
InsertLogNote("Exception= " + ex.ToString());
this.Errored(ref ex);
DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
}
}
}
}

As you can see the format is pretty basic, thee are just a few key items to remember.

  • Always wrap your custom code inside of a Try/Catch block to ensure that you can handle and log the detailed information about any error that might occur
  • To enable logging and to show that your job has truly started, call the Progressing method early in your code
  • Use the available AddLogNote functionality to log additional information that might be helpful.  (For example Records Affected, or other types of diagnostic information)

From here you can build out your scheduled job as needed.

Installing/Configuring the Job

Once you have created your Scheduled Job and installed the dll, either via a module package or some other process, you simply need to add a new item to the DotNetNuke Schedule.  To complete this process you will simply go to "Host" -> "Scheduler" and select the "Add Item to Schedule" option.  The following is an outline of the values that you supply.

  • Full Class Name and Assembly - Your assembly and class name, in the format of FQCN, Assembly (Example YourNamespace.YourJob, YourNamespace where YourNamespace.dll was the compiled project)
  • Schedule Enabled - Check this to enable the item
  • Time Lapse and Retry - To configure the frequency, values you desire
  • Retain History - If set to none, any log writes you do will not be preserved, if using the AddLogNote option, you will want to retain at least a few values.

All of the other items are dependent upon your environment and specific needs.  Once you save the record, your job will be scheduled to execute for the first time.

Conclusion

DotNetNuke scheduled jobs are very simple to build and are a great method to handle batch or offline processing for many needs.  With a bit of planning and logging in place you can handle complex business needs easily.  Feel free to share feedback below.

This blog post is cross-posted from my personal blog.

Comments

Adrian Correa Moreno
Forgive my ignorance. Does it also work on DNN 7.2 ?
Adrian Correa Moreno Sunday, February 23, 2014 8:01 AM (link)
sareen kumar
I have a DNN Scheduler in my application which needs to run on every 2 minutes. I noticed that my Scheduler is not running specified time interval (i.e. about 15 mts) in every one hour. My Site application pool alive forever since we are using host-tracker service .
sareen kumar Tuesday, January 6, 2015 12:57 AM (link)
Karthikeyan Kandasamy
It's very useful thank you...

And the another link recommended by me to create and deploy Schedule
http://dotnetslackers.com/Community/blogs/kaushalparik/archive/2010/05/11/how-to-create-dotnetnuke-dnn-scheduler.aspx
Karthikeyan Kandasamy Friday, October 9, 2015 2:20 AM (link)
Karthikeyan Kandasamy
I don't know the work of below two lines. Particularly "this.Errored(ref ex);"
Anybody please let me explain...

this.Errored(ref ex);
DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
Karthikeyan Kandasamy Monday, February 15, 2016 12:53 AM (link)
Roman Yagodin
There is no InsertLogNote method in this sample.
Roman Yagodin Monday, April 18, 2016 3:29 AM (link)

Comment Form

Only registered users may post comments.

NewsArchives


Aderson Oliveira (22)
Alec Whittington (11)
Alessandra Daniels (3)
Alex Shirley (10)
Andrew Hoefling (3)
Andrew Nurse (30)
Andy Tryba (1)
Anthony Glenwright (5)
Antonio Chagoury (28)
Ash Prasad (37)
Ben Schmidt (1)
Benjamin Hermann (25)
Benoit Sarton (9)
Beth Firebaugh (12)
Bill Walker (36)
Bob Kruger (5)
Bogdan Litescu (1)
Brian Dukes (2)
Brice Snow (1)
Bruce Chapman (20)
Bryan Andrews (1)
cathal connolly (55)
Charles Nurse (163)
Chris Hammond (213)
Chris Paterra (55)
Clint Patterson (108)
Cuong Dang (21)
Daniel Bartholomew (2)
Daniel Mettler (181)
Daniel Valadas (48)
Dave Buckner (2)
David Poindexter (12)
David Rodriguez (3)
Dennis Shiao (1)
Doug Howell (11)
Erik van Ballegoij (30)
Ernst Peter Tamminga (80)
Francisco Perez Andres (17)
Geoff Barlow (12)
George Alatrash (12)
Gifford Watkins (3)
Gilles Le Pigocher (3)
Ian Robinson (7)
Israel Martinez (17)
Jan Blomquist (2)
Jan Jonas (3)
Jaspreet Bhatia (1)
Jenni Merrifield (6)
Joe Brinkman (274)
John Mitchell (1)
Jon Henning (14)
Jonathan Sheely (4)
Jordan Coopersmith (1)
Joseph Craig (2)
Kan Ma (1)
Keivan Beigi (3)
Kelly Ford (4)
Ken Grierson (10)
Kevin Schreiner (6)
Leigh Pointer (31)
Lorraine Young (60)
Malik Khan (1)
Matt Rutledge (2)
Matthias Schlomann (16)
Mauricio Márquez (5)
Michael Doxsey (7)
Michael Tobisch (3)
Michael Washington (202)
Miguel Gatmaytan (3)
Mike Horton (19)
Mitchel Sellers (40)
Nathan Rover (3)
Navin V Nagiah (14)
Néstor Sánchez (31)
Nik Kalyani (14)
Oliver Hine (1)
Patricio F. Salinas (1)
Patrick Ryan (1)
Peter Donker (54)
Philip Beadle (135)
Philipp Becker (4)
Richard Dumas (22)
Robert J Collins (5)
Roger Selwyn (8)
Ruben Lopez (1)
Ryan Martinez (1)
Sacha Trauwaen (1)
Salar Golestanian (4)
Sanjay Mehrotra (9)
Scott McCulloch (1)
Scott Schlesier (11)
Scott Wilkinson (3)
Scott Willhite (97)
Sebastian Leupold (80)
Shaun Walker (237)
Shawn Mehaffie (17)
Stefan Cullmann (12)
Stefan Kamphuis (12)
Steve Fabian (31)
Steven Fisher (1)
Tony Henrich (3)
Torsten Weggen (3)
Tycho de Waard (4)
Vicenç Masanas (27)
Vincent Nguyen (3)
Vitaly Kozadayev (6)
Will Morgenweck (40)
Will Strohl (180)
William Severance (5)
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out