5
\$\begingroup\$

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);
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 11, 2011 at 20:30
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I wouldn't bother with all your various every overloads, if you want fluent style replace the everys with a single method .AndReoccur(int numberOfTimes, Timespan interval) also I'm pretty sure this site is supposed to be for compilable functioning code, maybe this fits on stackoverflow better? \$\endgroup\$ Commented Aug 12, 2011 at 1:59

2 Answers 2

3
\$\begingroup\$

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)
answered Aug 12, 2011 at 22:39
\$\endgroup\$
0
\$\begingroup\$

I'd like to be able to use a non-functional approach to running the api. Perhaps I'm a new developer and don't understand lambdas. Give me the option to go.

ctx.Run(//Task, or List of tasks, //How often, //Time to start, //Possible skip conditions - maybe skip weekends);
//Utilize enums that they can use
ctx.Run(Task.First(), Frequency.Daily, Time.Midnight);
//or pass a list of tasks perhaps
List<Task> tasks;
ctx.Run(tasks, Frequency.Hourly, Time.Custom(hour, min, sec), Time.Weekends);
answered Aug 12, 2011 at 2:51
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.