CODEGATOR

.NET info worth sinking your teeth into!
Welcome to CODEGATOR Sign in | Join | Help
in Search

Martin Cook

Yet another C# developer with a blog.

Job Library & Server (An Idea)

Background 

Sometimes my day job as a consultant occasionally involves scheduling small units of work to run periodically without any user-interaction. I know that the Microsoft O/S includes a scheduler but that has never really met my needs (go figure). In the past I have written schedulers for various customers in C++ and C#, so I decided to write another one for CODEGATOR and target my efforts specifically at .NET developers.

What I have so far is split into two projects: CG.Job & CG.Job.Server. The first project is a library that can be used to schedule and manage jobs. The second project is a Windows service that periodically checks for jobs in need of scheduling, and runs those jobs automatically. The details of my architecture are a little sketchy, since I'm in the process of porting old code and writing new code besides. Things will likely change, but for now, this is how I envision things working:

 

Overview 

I am currently using an abstract class named JobBase as a base for custom jobs. Developers who want to run their jobs can simply derive from my base class and code their own jobs. Something like this: 

[Serializable]

public sealed class ExampleJob : JobBase

{

    public override void Run()

    {

        System.Threading.Thread.Sleep(1000);

    }

}

Eventually, if I want to be able to run remote jobs (code on other machines), I'll have to do things a little differently. But for now, I've decided to start simple and get something up and running. So, to design custom jobs just derive from JobBase and override the Run method. Pretty easy, right? The only caveat will be that the CG.Job.Server will need to be able to load your custom assembly at runtime, so you'll probably need to put your assembly in the GAC. Maybe not, I'll think about that some more down the road...

I am currently using my CG.Storage library to handle the storage and retrieval of job objects. That way I can decide later whether I want to store them in a file, in a database, or whatever.

I am exposing a single class named JobAPI from the CG.Job library. I envision everyone using that API to schedule and manage jobs. Here is how I see that working:

ExampleJob job = new ExampleJob();

job.RunAt = DateTime.Now.AddSeconds(15);

JobApi.Create<ExampleJob>(job); 

Of course, there will be other methods for updating jobs, deleting jobs, etc.

 

Conclusion

The idea is, you can use the CG.Job library from your code to schedule your custom job objects, which will be serialize and stored into some central storage location known only to the CG.Storage library, where the CG.Job.Server Windows service will, at some later time, run that job on your behalf. I also figure I'll use my CG.Task library to actually run the jobs in separate threads.

I've put what I have so far in the downloads section of CODEGATOR. The code that's there actually runs, but you'll have to install the service manually using the 'InstallUtil' utility. You'll also have to create a database for the CG.Storage library, or re-configure the app.config to use the disk based storage provider. If you decide to use the databasde provider then the .SQL file is located in the CG.Storage project under the 'Data\Provider' folder.

One of these days I'll write a nifty .MSI installer for all of this code - after I get it written of course... LOL!

 

Sound cool? I think so. Now I just have to make it work....  Um, I'll see ya later; I've got some work to do. Big Smile

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Martin

I work as a software engineer specializing in designing and building object-oriented business solutions for Windows platforms using C#. I have been programming professionally for roughly 20 years.

This Blog

Syndication

Terms of Service | Privacy Statement