Abidar 1.0 Beta 1
Last month I wrote a couple of blog posts about building a task scheduler system for ASP.NET (part 1 and part 2). Following to those posts and reader feedback I was convinced to extend that idea as an open source project. Here Dave Burke, my dear friend and one of the most respectful men on the community, was the main person who collaborated with me on the topic.
Even though turning that basic sample of that post series to a general framework seemed very straightforward and easy but I finally decided to start an open source project called Abidar on CodePlex and licensed under The MIT License. I wrote a blog post announcing this project and described its purpose and goals shortly (Abidar is the name of a beautiful mountain near Sanandaj, great Kurdistan).
I had promised to release a Beta as soon as I could finish my work with Waegis and now it’s time to talk about it!
However, today I found 1-2 hours to work on this project and add my own code as a separate project and polish it to some extent. I had to drop some features that were integrated to my own projects but I think that the final result is a good library to start with. Of course there is a enough open space to extend this to a better framework but the current code should be suitable for many circumstances.
As a short documentation for this project, let me write shortly about the steps that you need to follow in order to be able to use this library. For the future versions I will try to make the install process simpler but for now it’s easy enough to leave it as is!
Background
The structure of Abidar is pretty simple. There is a public class, TaskScheduler, that you need to work with as well as a public interface called ITask. You shouldn’t worry about anything else in Abidar API.
The purpose of TaskScheduler class is to get a configuration XML node and help you to start and stop tasks. ITask interface is there to let you implement your own task logic with the help of XML configuration that is passed to it.
In a nutshell you can apply Abidar in a few steps:
- Create your own task by implementing ITask interface.
- Add appropriate configuration to an XML configuration file and load the appropriate XML node.
- Create an instance of TaskScheduler class and pass the configuration node to it.
- Add appropriate code to your application to load, start and stop tasks when you desire.
I try to show this step by step in more details. There is a simple sample ASP.NET project provided with Abidar source code that you can use as a reference.
Implement the Task
Obviously the first step is to implement your task logic. Here you need to implement ITask interface. This interface has a single method called Execute that gets a single XmlNode parameter. This node is actually your configuration node for your task. You can use this node in order to pass custom parameters and properties to your task and use them to implement your logic.
For example, I implement a very simple task that clears the ASP.NET cache every time that it runs.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Abidar.Components;
using System.Xml;
using System.IO;
using System.Collections;
namespace Abidar.TestSite
{
public class ClearCacheTask : ITask
{
#region ITask Members
public void Execute(XmlNode configuration)
{
foreach (DictionaryEntry entry in HttpRuntime.Cache)
{
HttpRuntime.Cache.Remove(entry.Key.ToString());
}
}
#endregion
}
}
Configuration
In the second step you need to configure your application in order to add your implemented task. Here you should add appropriate XML configuration and load them in your application.
The current version of Abidar requires you to pass a single XML node, the parent node of your tasks, to it. You need to have a root <Tasks /> element that contains one or multiple <Task /> elements. Each <Task /> element must have five required attributes as well as any optional attribute or child element that you like to have:
- name: The name of your task.
- type: Your task type. It doesn’t matter how do you want to define it. It’s important to have a string value that can be used by System.Reflection API.
- interval: This is the time interval for your task in milliseconds. I preferred to have milliseconds here but may change this unit upon user requests.
- enabled: A boolean value that lets you enable or disable your tasks easily.
- priority: An integer value from 0 to 2 that lets to change the priority of the task. This is something that you can modify based on your application requirements. I found this necessary for some big applications.
So having this background, I can create a file, say task.config, with the following configuration:
<?xml version="1.0"?>
<Tasks>
<Task name="ClearCacheTask"
type="Abidar.TestSite.ClearCacheTask, Abidar.TestSite"
interval="5000"
enabled="true"
priority="1" />
</Tasks>
With this configuration my task runs every 5 seconds.
Schedule the Task
The last step is to schedule your task. Here you need to choose when and where do you want to start or stop your tasks but most likely you’d like to do this when your ASP.NET application is starting/stopping. So the most appropriate place to add your code is in Global.asax file inside Application_Start and Application_StopEnd methods.
To schedule your tasks, you need to grab an XmlNodeList object that contains the list of <Task /> elements in your configuration file and pass this object to the public constructor of TaskScheduler class. This class has two methods called StartTasks and StopTasks that manage the rest for you.
So here is my code to achieve this.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml;
namespace Abidar.TestSite
{
public class Global : System.Web.HttpApplication
{
private TaskScheduler _scheduler = null;
protected void Application_Start(object sender, EventArgs e)
{
XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath(@"\config\tasks.config"));
XmlNodeList nodes = xml.SelectNodes("Tasks/Task");
this._scheduler = new TaskScheduler(nodes);
this._scheduler.StartTasks();
}
protected void Application_End(object sender, EventArgs e)
{
this._scheduler.StopTasks();
}
}
}
If I run this application then it clears my ASP.NET cache every five minutes.
How About Longer Intervals?
There is a common question about such a task scheduler system and that is the possibility of running tasks for longer intervals. I want to address this question here and make it clear. I have to answers for this.
The first answer is that logically you’re able to turn a task with long intervals to a task with shorter intervals. I have experienced it for many tasks. This should be easy to do and is also much better than having a task that runs on long intervals. I haven’t seen any task that was not convertible easily.
But the second answer is an applicable and experienced solution and that is the use of a keep alive mechanism. Here there are many written posts but I just refer you to an oldie but goodie post by Dave that describes the solution simply.
Roadmap
The current version of Abidar (version 1.0 Beta 1) is just what I could extract from my existing projects. There are some points that need more attention. At the moment I can list following changes/features for the roadmap:
- Simplifying the install/usage steps as much as possible.
- Adding the capability to define configuration elements in Web.Config. This should be the default option for configuration.
- Adapting Windows Workflow Foundation and applying as much as possible. This was suggested by a commenter on my blog.
Download
Abidar 1.0 Beta 1 is available on CodePlex and you can download its binary and source code packages separately. There is a plan for Beta 2 but I don’t have a fixed timeline for now.
[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.
10 Comments : 07.18.08
Feedbacks
@Dave,
Thank you, man! I just use CopySourceAsHtml add-in for Visual Studio. I think it's the best option for code highlighting :-)
Using CopySourceAsHtml with Visual Studio 2008 developers.de/.../using-copysourc
Pingback from Dew Drop - July 19, 2008 | Alvin Ashcraft's Morning Dew
Great seeing this... just in time for a project I'm about to start :)
Abidar Task Scheduler Application Taking Shape
Pingback from Reflective Perspective - Chris Alcock » The Morning Brew #140
Hi guys,
sorry for the shameless plug but one alternative you might want to check out is Quartz.NET quartznet.sourceforge.net it's a job scheduling system ported from Java land and being a mature project it has quite a lot of features.
Abidar looks cool and easy to grasp though.
Having a powerful and extensible configuration system is a common facet of any application. Almost all
Hi ,
it is a newbie question ,
Is it possible to use session or application variable in the execute function?
i try using
System.Web.HttpContext.Current.Application["dtNow"] = DateTime.Now.ToString();
but unfortunately System.Web.HttpContext.Current points null
so not able to access the Session or Application variable. :(
please mail me the suggestion at askitanna@gmail.com
or add the suggestion here as another comment
thanks
annamalai
p.s. dont know where to ask question, so simply using this section

#1
Dave Burke
07.18.2008 @ 11:37 AM
Like I said before, this is your achievement. I'm only along with the ride. I also wanted to mention how good looking your technical posts are how you intermingle code with text. Well done!