Skip to content

JobScheduler

Brian Lehnen edited this page Apr 9, 2026 · 1 revision

Job Scheduler Reference

The job scheduler uses cron expressions to define when jobs run. Cron parsing is handled by the Cronos library.

Cron Expression Format

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.

IJobSchedule API

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.

IScheduledJob Properties

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).

Scheduler Events
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
Managing Jobs
// 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) { /* ... */ }
Related Pages

Clone this wiki locally