-
Notifications
You must be signed in to change notification settings - Fork 16
JobScheduler
The job scheduler uses cron expressions to define when jobs run. Cron parsing is handled by the Cronos library.
5-field (minute granularity):
* * * * *
| | | | |
| | | | +-- Day of week (0-6, Sunday=0)
| | | +---- Month (1-12)
| | +------ Day of month (1-31)
| +-------- Hour (0-23)
+---------- Minute (0-59)
6-field (second granularity):
* * * * * *
| | | | | |
| | | | | +-- Day of week (0-6, Sunday=0)
| | | | +---- Month (1-12)
| | | +------ Day of month (1-31)
| | +-------- Hour (0-23)
| +---------- Minute (0-59)
+------------ Second (0-59)
The library auto-detects the format based on the number of fields.
| Expression | Meaning |
|---|---|
* * * * * |
Every minute |
*/5 * * * * |
Every 5 minutes |
0 */2 * * * |
Every 2 hours |
0 9 * * 1-5 |
9 AM on weekdays |
*/5 * * * * * |
Every 5 seconds (6-field) |
*/15 * * * * * |
Every 15 seconds (6-field) |
All times are UTC.
Each scheduled job exposes its schedule via the IJobSchedule interface:
public interface IJobSchedule
{
string OriginalText { get; } // the cron expression as provided
string Description { get; } // human-readable (via CronExpressionDescriptor)
DateTimeOffset Next(); // next occurrence from now
DateTimeOffset Next(DateTimeOffset after);
DateTimeOffset? Previous(); // last occurrence (null if none in lookback window)
DateTimeOffset? Previous(DateTimeOffset atOrBefore);
}The Description property returns a human-readable string (e.g., "Every 5 seconds") generated by the CronExpressionDescriptor library.
Previous() scans backward over a 48-hour lookback window. Returns null if no matching time exists within that window.
After calling AddUpdateJob, you receive an IScheduledJob with:
| Property | Type | Description |
|---|---|---|
Name |
string |
Job name passed to AddUpdateJob
|
Schedule |
IJobSchedule |
The cron schedule (see above) |
Route |
string |
Message route, if set |
IsScheduleRunning |
bool |
Whether the schedule timer is active |
IsCallbackExecuting |
bool |
Whether a job enqueue callback is in progress |
IsAttached |
bool |
Whether the job is attached to a scheduler |
Window |
TimeSpan |
Duplicate detection window |
NextEvent |
DateTimeOffset |
Next scheduled fire time |
PrevEvent |
DateTimeOffset |
Last scheduled fire time |
Methods: StartSchedule(), StopSchedule(), UpdateSchedule(string cronExpression), UpdateSchedule(IJobSchedule schedule).
| Event | On | When |
|---|---|---|
OnJobQueueException |
IJobScheduler |
Fatal exception adding job to queue |
OnJobNonFatalFailureQueue |
IJobScheduler |
Job skipped (already running or already executed for time slot) |
OnJobQueue |
IJobScheduler |
Job successfully enqueued |
OnException |
IScheduledJob |
Per-job exception |
OnNonFatalFailureEnQueue |
IScheduledJob |
Per-job skip |
OnEnQueue |
IScheduledJob |
Per-job enqueue |
// List all registered jobs
var jobs = scheduler.GetAllJobs();
// Remove a job by name
bool removed = scheduler.RemoveJob("test job1");
// Check shutdown state
if (scheduler.IsShuttingDown) { /* ... */ }- Scheduler -- setup and usage
- Queue Creation -- custom queue options for scheduled jobs
- Time Settings -- clock sync and time providers
- Redundancy -- running multiple schedulers
For any issues please use the GitHub issues