Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
161 lines (115 sloc) 5.04 KB

JobSharp

NuGet License

An easy-to-use C# jobs runner.


Features

  • Individual jobs scheduling using Crontab expressions.
  • Easy selection of what jobs run in each Windows Service instance.
  • Fault Tolerant. An failure in a job will not affect the others jobs running.
  • Auto discovering jobs. Just implement IJob interface and your job will be discovered.
  • Easy installation and command-line via Topshelf.
  • Run a specific job via -job:<job name> command-line option.
  • Fully tested on Windows.
  • Working on Visual Studio and Xamarin Studio.
  • Unit tests and functional tests .
  • 100% code documentation.
  • FxCop and StyleCop validated.
  • Good (and well used) design patterns.

Setup

  • In VS|XS add a new "Console Application" project to your solution.
  • Install-Package JobSharp
  • Create a folder called "Jobs" and create your jobs inside it. To create the job, follow the instructions in "Creating a job"
  • Add an "Application Configuration File" (app.config) to your project.
    • Add the key <add key="JobSharp:CronTab" value="1000" /> to the appSettings section.
    • Follow the steps described in "Configuring the job"
  • Replace the content of Program.cs file with the code described in step "Configuring Windows Service"
  • Configure the jobs logging as describe in "Configuring the logging"
  • Install Windows Service as describe in "Installing Windows Service"
  • Run the application follow the steps described in "Running"

Creating a job

class MyFirstJob : IJob
{
    public void Run()
    {
        // Put your job's logic or call your domain logic here.
        LogService.Write("My first job using JobSharp is running ;)");
    }
}

Configuring the job

Add your new job "JobSharp:Jobs" key in the appSettings section (use comma to add more jobs):

<add key="JobSharp:Jobs" value="MyFirstJob" />

Define when the job must run:

<add key="MyFirstJob:Crontab" value="*/2 * * * *" />

This configuration will make the job runs every 2 minutes.

Configuring Windows Service

Replace the content of the file Program.cs:

using JobSharp;
using Skahal.Infrastructure.Logging.Log4net;
using Topshelf;

class Program
{
    static void Main(string[] args)
    {
        WindowsService.Install(
		new Log4netLogStrategy ("JobSharpLog"), 
		c =>
        {
            c.RunAsLocalSystem();

            c.SetDescription("A very simple JobSharp Sample");
            c.SetDisplayName("JobSharp Sample");
            c.SetServiceName("JobSharpSample");
            c.StartAutomatically();
            c.EnableShutdown();
        });        
    }
}

Configuring the logging

The default configuration use Log4net as the log strategy.

To configure log4net take a look on JobSharp.Sample's App.config

If Log4net does not meet what you need, I doubt ;), you can implement your own ILogStrategy and inject it on WindowsService.Install method call.

Installing Windows Service

Open a prompt window on directory with your console application .exe file and type: <your console application name>.exe install

Running

You can run it as a Windows Service or just as a command-line.

More about command-line options in Topshelf docs.

Sample

There is a sample project called JobSharp.Sample in the solution. This sample has the basic bootstrap for a project using JobSharp.

You can fork the full source code to see how a JobSharp powered Windows Service looks like.


FAQ

Having troubles?

Roadmap

  • Create a configuration section handler to move the configuration from appSettings section to a specific JobSharp section.

How to improve it?

License

Licensed under the The MIT License (MIT). In others words, you can use this library for developement any kind of software: open source, commercial, proprietary and alien.