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);
}
2 Answers 2
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)
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);
.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\$