Custom Recurrence Engines
If TimeSpanInterval, NaturalCron, Cronos and NCrontab don't cover your rules, plug in your own engine: implement two interfaces and register the compiler, the same way the Cronos / NCrontab packages work.
Before writing your own engine, make sure your schedule can't already be written with the existing ones. NaturalCron covers a lot, and its custom keywords let you plug your own rules into an expression. For example, business days without holidays is just every day at 09:00 except [holiday, saturday, sunday], with a holiday keyword you register at startup:
NaturalCronCustomKeywords.Configure(cfg =>
cfg.AddMatchable("holiday", dt => myHolidays.Contains(dt.Date))); // e.g. a HashSet<DateTime> kept in memory
Configure the keyword in every app of the cluster, since each of them reads the expression.
The example below runs on business days: every Monday to Friday at a given time, skipping your company's holidays. The expression is just the time, "09:00". It shows how an engine is built; for this particular rule, the NaturalCron keyword above is simpler.
1. Implement IRecurrenceCompiledExpr
The compiled expression calculates the next occurrence.
public sealed class BusinessDaysCompiledExpr : IRecurrenceCompiledExpr
{
private readonly TimeSpan timeOfDay;
private readonly IReadOnlySet<DateOnly> holidays;
public BusinessDaysCompiledExpr(string expression, TimeSpan timeOfDay, IReadOnlySet<DateOnly> holidays)
{
Expression = expression;
this.timeOfDay = timeOfDay;
this.holidays = holidays;
}
public string Expression { get; }
public string ExpressionTypeId => BusinessDaysCompiler.TypeId;
public DateTime? GetNextOccurrence(DateTime dateTime, string ianaTimeZoneId)
{
var next = dateTime.Date + timeOfDay;
if (next <= dateTime)
{
next = next.AddDays(1);
}
while (!IsBusinessDay(next))
{
next = next.AddDays(1);
}
return next;
}
public bool HasEnded(DateTime dateTime, string ianaTimeZoneId) => false; // never ends
private bool IsBusinessDay(DateTime day) =>
day.DayOfWeek is not (DayOfWeek.Saturday or DayOfWeek.Sunday)
&& !holidays.Contains(DateOnly.FromDateTime(day));
}
GetNextOccurrencereturns the first occurrence afterdateTime, ornullwhen there's none left.- Both
dateTimeand the result are wall-clock times in the schedule's time zone (ianaTimeZoneId, the cluster's unless the expression names its own). JobMaster converts to and from UTC, so the engine doesn't have to. HasEndedreturnstrueonce there's no occurrence left; the schedule then becomesCompleted.
2. Implement IRecurrenceExprCompiler
The compiler turns an expression string into a compiled expression. Its ExpressionTypeId is the name you schedule with.
public sealed class BusinessDaysCompiler : IRecurrenceExprCompiler
{
public const string TypeId = "BusinessDays";
private readonly IReadOnlySet<DateOnly> holidays;
public BusinessDaysCompiler(IReadOnlySet<DateOnly> holidays) => this.holidays = holidays;
public string ExpressionTypeId => TypeId;
public IRecurrenceCompiledExpr? TryCompile(string expression) =>
TimeSpan.TryParse(expression, CultureInfo.InvariantCulture, out var time) && time >= TimeSpan.Zero && time < TimeSpan.FromDays(1)
? new BusinessDaysCompiledExpr(expression, time, holidays)
: null;
public IRecurrenceCompiledExpr Compile(string expression) =>
TryCompile(expression) ?? throw new ArgumentException($"Invalid BusinessDays expression: '{expression}'. Expected a time, e.g. \"09:00\".");
}
3. Register It at Startup
Register the compiler before AddJobMasterCluster:
var holidays = new HashSet<DateOnly> { new(2026, 12, 25), new(2027, 1, 1) };
RecurrenceCompilerFactory.RegisterCompiler(new BusinessDaysCompiler(holidays));
builder.Services.AddJobMasterCluster(config => { ... });
A schedule stores only the type id and the expression. Register the same compiler, with the same settings (here the holidays), in every app of the cluster that schedules or runs these schedules.
4. Use It
Use the type id like any built-in engine:
await scheduler.RecurringAsync<PayrollHandler>(BusinessDaysCompiler.TypeId, "09:00");
Or in a profile: collection.Add<PayrollHandler>(BusinessDaysCompiler.TypeId, "09:00").
5. Optional: a Schedule Attribute
To use the engine with a schedule attribute, subclass RecurringScheduleAttribute:
public sealed class BusinessDaysScheduleAttribute(string expression) : RecurringScheduleAttribute(expression)
{
public override string ExpressionTypeId => BusinessDaysCompiler.TypeId;
}
[BusinessDaysSchedule("09:00")]
public sealed class PayrollHandler : IJobMasterHandler { ... }