0

Classic requirement of checking system state and notifying users. Specifically, I'll be hitting the database every x-amount of time, getting some data, then sending out email notifications based on the results. Heck, this service might not even send out an email, but create a notification record in the database.

Seems like with IOC and configuration there could be a generic windows service that manages all this, along with metrics and management, in a simple manner.

In the past I've done email notifications by: 1) Running scripts as cron (at on Windows) jobs 2) running custom executables as cron/at jobs 3) using something like SQLServer's DatabaseMail. 4) Custom NT Services that run all the time monitoring things.

Is there any open source projects that manages this? It's the type of code I've written many, many times in various platforms, but don't want to spend the few days doing it now.

The only thing I found so far was Quartz.Net

Thanks

taudep
  • 2,871
  • 5
  • 27
  • 36

1 Answers1

0

I just create a Windows service and use the Reactive Extensions to schedule tasks. If you don't need as much flexibility as cron offers, this works fine. Here's an abstract hourly task runner. (uses log4net)

public abstract class HourlyTask : IWantToRunAtStartup
{
    protected readonly ILog Log = LogManager.GetLogger(typeof (HourlyTask).FullName);
    private IDisposable _Subscription;

    private void ExecuteWithLog()
    {
        Log.Debug("Triggering " + GetType());
        try
        {
            Execute();
        }
        catch (Exception exception)
        {
            Log.Error("Hourly execution failed", exception);
        }
    }

    public abstract void Execute();

    public void Run()
    {
        _Subscription = Observable
            .Interval(TimeSpan.FromMinutes(1))
            .Select(x => DateTime.Now.Hour)
            .DistinctUntilChanged()
            .Subscribe(i => ExecuteWithLog());
    }

    public void Stop()
    {
        if (_Subscription == null)
        {
            return;
        }
        _Subscription.Dispose();
    }
}

Then in your start up method you can just resolve all IWantToRunAtStartup instances and call Run() on them.

Ryan
  • 4,303
  • 3
  • 24
  • 24
  • Accepting this as the answer. I figured I just have to bite the bullet and write a service for this. – taudep Jan 17 '12 at 14:06