OSKHelpers.Cron
0.9.0
Prefix Reserved
dotnet add package OSKHelpers.Cron --version 0.9.0
NuGet\Install-Package OSKHelpers.Cron -Version 0.9.0
<PackageReference Include="OSKHelpers.Cron" Version="0.9.0" />
<PackageVersion Include="OSKHelpers.Cron" Version="0.9.0" />
<PackageReference Include="OSKHelpers.Cron" />
paket add OSKHelpers.Cron --version 0.9.0
#r "nuget: OSKHelpers.Cron, 0.9.0"
#:package OSKHelpers.Cron@0.9.0
#addin nuget:?package=OSKHelpers.Cron&version=0.9.0
#tool nuget:?package=OSKHelpers.Cron&version=0.9.0
OSKHelpers.Cron
OSKHelpers.Cron schedules the actions of an application from a crontab file, with the standard cron syntax interpreted
by Cronos. The scheduler reloads the file when it is modified, without a restart, and
raises an event (or awaits an asynchronous handler) at every scheduled time: the application decides what to execute.
It targets .NET Standard 2.0 and integrates with OSKHelpers (logging via
SimpleLog, default directory via Paths, settings via JsonSettings<T>).
dotnet add package OSKHelpers.Cron
Source code, issue tracking and the other packages: github.com/ObySamKenoby/OSKHelpers.
Compatibility: OSKHelpers.Cron 0.9.0 requires OSKHelpers 2.0.0 or later; the automatic update of the settings shown in Settings requires OSKHelpers 2.1.3.
The crontab file
By default the file is crontab (no extension) in Paths.DefaultConfigsDirectory. Every line schedules an action:
<minute> <hour> <day of month> <month> <day of week> <Action>
<@macro> <Action>
# Every 15 minutes from 8 to 18, Monday to Friday
*/15 8-18 * * 1-5 Import orders
# Every day at midnight
@daily Cleanup
# The same action can appear in more lines
30 12 * * 6 Import orders
- The action is the rest of the line and may contain spaces; by default actions are compared without distinction between upper and lower case (see Comparing the actions).
- Macros:
@yearly/@annually,@monthly,@weekly,@daily/@midnight,@hourly,@every_minute. - Empty lines and lines starting with
#are ignored; invalid lines are ignored by the scheduler and reported. - The times are interpreted in the local time zone (or in the one configured), including the daylight saving time changes.
Quick start
using OSKHelpers.Cron;
var scheduler = new CrontabScheduler(new Crontab());
// Events and handlers must be set before Start, which reads the file immediately
scheduler.CrontabReloaded += (sender, e) =>
{
if (e.Errors.Count > 0) { /* e.g. send a warning */ }
};
scheduler.ActionHandler = e => RunActionAsync(e.Action, e.CancellationToken);
scheduler.Start();
// ...
scheduler.Stop(); // waits for the running actions up to StopTimeoutSeconds (default 5 minutes)
Event or ActionHandler
ScheduledActionTriggeredis raised in a task of the thread pool: its handlers must be synchronous.ActionHandler(Func<ScheduledActionTriggeredEventArgs, Task>) is awaited by the scheduler: use it for asynchronous code.
Do not write an async handler for the event: it returns at its first await, so the scheduler considers the action
terminated while it is still running (overlapping executions are not prevented, Stop does not wait for it) and an exception
thrown after the await can terminate the process. If you prefer the event, wait for the asynchronous code in it:
scheduler.ScheduledActionTriggered += (sender, e) =>
{
RunActionAsync(e.Action, e.CancellationToken).GetAwaiter().GetResult();
};
Behaviour
- At every wake-up the scheduler executes the actions with a scheduled time since the previous wake-up: scheduled times before
the start are not recovered; the times missed by a late wake-up (e.g. after the system was suspended) produce a single
execution (
MissedOccurrences). - An action scheduled by more lines at the same time is executed once.
- If an action is still running at its next scheduled time, that time is skipped (
ScheduledActionSkipped). - Two lines schedule the same action when
Crontab.ActionComparerconsiders their actions equal (see Comparing the actions). NextOccurrenceUtcandNextOccurrenceActionsshow the next scheduled time and its actions.- Do not call
Stopfrom the handlers of the scheduler. Disposestops the scheduler only ifStop/StopAsynchas not been called after the lastStart: the actions still running after the stop timeout are not awaited again, so a hosted service can stop the scheduler inExecuteAsyncand dispose it with the service.
Use in a hosted service
public class Worker : BackgroundService
{
private readonly CrontabScheduler _scheduler;
public Worker()
{
_scheduler = new CrontabScheduler(new AppCrontab());
_scheduler.ActionHandler = e => RunActionAsync(e.Action, e.CancellationToken);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_scheduler.Start();
try
{
await Task.Delay(Timeout.Infinite, stoppingToken);
}
catch (OperationCanceledException)
{
}
await _scheduler.StopAsync();
}
}
Checking the file
CheckCrontab reads the file and returns its validity and descriptive lines (errors prefixed by ERROR: , warnings by
WARNING: ), including the coverage of every action: number of lines, next execution, executions in the next 24 hours and
7 days, least frequent actions.
var check = new Crontab().CheckCrontab();
check.Lines.ForEach(Console.WriteLine);
return check.IsValid ? 0 : 1;
Custom validation
Derive from Crontab to add the rules of the application:
ValidateActionrejects a line (used also by the scheduler, which ignores it): e.g. an unknown action;AddCoverageLineswrites the coverage table: override it to show a table of your own (e.g. all the configured actions with their state, usingGetCoveragewith the list of the actions andGetLeastCovered) or to omit it;CheckCrontabCustomLogicadds lines to the check and can make the file invalid: e.g. configured actions not scheduled.
public class AppCrontab : Crontab
{
protected override string ValidateAction(CrontabEntry entry)
=> KnownActions.Contains(entry.Action)
? string.Empty
: $"Line {entry.LineNumber}: the action '{entry.Action}' does not exist.";
protected override bool CheckCrontabCustomLogic(IReadOnlyList<CrontabEntry> entries, List<string> lines)
{
var notScheduled = GetCoverage(entries, DateTime.UtcNow, Zone, KnownActions, ActionComparer)
.Where(c => !c.IsScheduled)
.Select(c => c.Action)
.ToList();
if (notScheduled.Count > 0)
{
lines.Add($"{WARNINGPREFIX}actions not scheduled: {string.Join(", ", notScheduled)}.");
}
return true; // warnings do not make the file invalid
}
protected override void AddCoverageLines(IReadOnlyList<CrontabEntry> entries, DateTime nowUtc, TimeZoneInfo zone,
List<string> lines)
{
foreach (var item in GetCoverage(entries, nowUtc, zone, KnownActions, ActionComparer))
{
lines.Add($" {item.Action,-30} {item.OccurrencesNextDay,8}{(item.IsScheduled ? "" : " [NOT SCHEDULED]")}");
}
}
}
When the list of the valid actions changes, call Invalidate() to reload the file at the next wake-up.
Comparing the actions
By default two actions are the same when their texts are equal without distinction between upper and lower case
(Crontab.DefaultActionComparer). Pass a custom IEqualityComparer<string> to the constructor of Crontab to recognize as the
same action texts written in different ways, e.g. with different spaces or quotes: the scheduler then executes them once per
wake-up and does not start one of them while another is still running, minimizing the overlapping executions. The comparer is
also used by the coverage of CheckCrontab; an action scheduled by more lines is reported with the text of its first line.
public class AppCrontab : Crontab
{
public AppCrontab(string filePath) : base(filePath, null, new CommandComparer())
{
}
}
The comparer is set once, in the constructor (ActionComparer); its GetHashCode must be consistent with Equals and it must
be thread-safe.
Settings (ISettingsCron)
Implement ISettingsCron in the settings class of the application and configure the scheduler with Setup; with
[AutoUpdate] the changes to the settings file are applied without a restart.
| Property | Meaning | Recommended default |
|---|---|---|
CronFilePath |
Path of the file (relative to Paths.DefaultConfigsDirectory); empty = default |
empty |
CronCheckIntervalSeconds |
Maximum wait between two checks of the file | CrontabScheduler.MAXWAITSECONDS (60) |
CronStopTimeoutSeconds |
Maximum wait for the running actions when stopped; null = no limit |
CrontabScheduler.DEFAULTSTOPTIMEOUTSECONDS (300) |
CronTimeZone |
Id of the time zone; empty = local | empty |
The time zone ids differ between Windows (W. Europe Standard Time) and Linux (Europe/Rome) and are not converted
automatically on .NET Standard 2.0.
public class AppSettings : JsonSettings<AppSettings>, ISettingsCron
{
public static AppSettings Current { get; private set; }
[AutoUpdate] public string CronFilePath { get; set; }
[AutoUpdate] public int CronCheckIntervalSeconds { get; set; }
[AutoUpdate] public int? CronStopTimeoutSeconds { get; set; }
[AutoUpdate] public string CronTimeZone { get; set; }
protected override void CreateDefaultData()
{
CronFilePath = string.Empty;
CronCheckIntervalSeconds = CrontabScheduler.MAXWAITSECONDS;
CronStopTimeoutSeconds = CrontabScheduler.DEFAULTSTOPTIMEOUTSECONDS;
CronTimeZone = string.Empty;
}
// ...
}
AppSettings.Load();
var scheduler = new CrontabScheduler(new AppCrontab(), AppSettings.Current); // same as Setup(AppSettings.Current)
AppSettings.Current.AutoUpdated += (sender, result) => scheduler.Setup(AppSettings.Current);
AppSettings.Current.StartAutoUpdate();
scheduler.Start();
[AutoUpdate], AutoUpdated and StartAutoUpdate require OSKHelpers 2.1.3; with earlier versions call Setup once at startup.
Setup checks all the values before applying any of them: with an invalid value nothing is applied, the reasons are written to
the log and the result is false.
Release notes
- 0.9.0 - First release. The coverage table of the check can be replaced by overriding
AddCoverageLines;Disposedoes not stop again a scheduler already stopped; the actions are compared withCrontab.ActionComparer, which can be passed to the constructor ofCrontab.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Cronos (>= 0.13.0)
- OSKHelpers (>= 2.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.9.0 | 80 | 11/18/2026 |
0.9.0 - First release. Requires OSKHelpers 2.0.0 (2.1.3 for the automatic update of the settings with [AutoUpdate]). Crontab reads the crontab file (five standard fields or a macro, followed by the action), reloads it when it is modified and validates it with CheckCrontab (overridable with ValidateAction, AddCoverageLines and CheckCrontabCustomLogic). CrontabScheduler raises ScheduledActionTriggered and awaits ActionHandler at every scheduled time, skips an action still running (ScheduledActionSkipped), raises CrontabReloaded, can be configured with ISettingsCron and is not stopped again by Dispose after Stop. The actions are compared with Crontab.ActionComparer, a custom comparer passed to the constructor of Crontab (default: case-insensitive), to minimize the overlapping executions of actions written in different ways.