NaturalCron 1.1.0: define your own schedule keywords
I introduced a new cool feature in NaturalCron 1.1.0: you can now customise your own keyword rule and add it to the NaturalCron engine. It is usually added once on app startup, before any parsing.
For those who don't know, NaturalCron is a scheduling library for .NET where you write schedules in plain words instead of cron strings, e.g. every 30 minutes between 09:00 and 18:00.
Version 1.1.0 adds custom keywords: you define a word once at app startup, with your own logic, and then use it in any expression.
NaturalCronCustomKeywords.Configure(cfg =>
{
// A day keyword (the default time unit is Day).
cfg.AddMatchable("holiday", dt => myHolidays.Contains(dt.Date));
// An hour keyword.
cfg.AddMatchable("evenHour", dt => dt.Hour % 2 == 0, NaturalCronTimeUnit.Hour);
});
Then:
every holiday at 09:00
every day at 09:00 except holidays
every day on [holiday, 15th] at 09:00
A keyword can also be a class, so it can come from your DI container and read from a cache.
Also new in 1.1.0:
except, which works with built-in values too:every day at 09:00 except [saturday, sunday]- A shorthand for
every:every 15th at 18:00,every 1stMonday at 09:00,every 9am
GitHub: https://github.com/hugoj0s3/NaturalCron NuGet: https://www.nuget.org/packages/NaturalCron
Feedback welcome, especially on the syntax.