HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Task scheduler API

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
schedulerapitask

Problem

I'm spec'ing out a API for a task scheduler and I would be thankful for your thoughts. This is what I've got so far:

public void Configure(Context ctx) {
    // Single tasks
    ctx.Run(() => Tasks.First()).Every.Midnight;
    ctx.Run(() => Tasks.Second()).Every.Day(8,0));

    // Multiple tasks
    ctx.Run(() => {
        Tasks.Third();
        Tasks.Fourth();
    }).Every.Day(8,0);

   // Triggers:
   ..Every.Hour(20/*Minute*/);
   ..Every.Minute(10/*Second*/);
   ..Every.Second();
}


The run method accepts an Action parameter.

Obviously, I need to support custom triggers to allow users to configure it exactly as they want. It could be done by implementing an interface like:

interface ITask {
    bool ShouldRun(DateTime currentDate);
}

Solution

Instead of rolling your own API for repetitions, I'd adopt the terminology and semantics of RFC 5545 RRULEs which is widely used within Calendaring applications

Section 3.8.5.3 thoroughly documents the syntax and behavior of RRULEs (Recurrence rules). They look like

RRULE:FREQ=WEEKLY;COUNT=10;WKST=SU;BYDAY=TU,TH


which means

Weekly on Tuesday and Thursday for five weeks:

so you could implement these semantics (or find an existing RFC 5545 library that does it) and your API might look very similar to the RRULE syntax:

ctx.Run(...).Freq(WEEKLY).Count(10).ByDay(TU, TH)

Code Snippets

RRULE:FREQ=WEEKLY;COUNT=10;WKST=SU;BYDAY=TU,TH
ctx.Run(...).Freq(WEEKLY).Count(10).ByDay(TU, TH)

Context

StackExchange Code Review Q#4038, answer score: 2

Revisions (0)

No revisions yet.