Skip to main content

Scheduling Jobs

This page continues from Getting Started: the same cluster and HelloJobHandler, now scheduled in different ways and with a few basic settings.

When to Run​

Immediately​

Runs the job as soon as a worker picks it up:

var msg = WriteableMessageData.New().SetStringValue("Name", "John Doe");

await scheduler.OnceNowAsync<HelloJobHandler>(msg);

After a Delay​

Runs the job after a time span, counted from now:

await scheduler.OnceAfterAsync<HelloJobHandler>(TimeSpan.FromMinutes(5), msg);

At a Specific Time​

Runs the job at a given time. Pass it in UTC:

var runAt = new DateTime(2026, 12, 24, 18, 0, 0, DateTimeKind.Utc);

await scheduler.OnceAtAsync<HelloJobHandler>(runAt, msg);

Each call returns a receipt that tells you where the job was saved; you don't need it to get started (see Schedule Receipts).

Passing Data to the Job​

Message data is the job's payload. Set values when scheduling:

var msg = WriteableMessageData.New()
.SetStringValue("Name", "John Doe")
.SetJson("Order", new OrderPayload { Id = 42, Total = 99.90m });

and read them in the handler:

var name = job.MsgData.TryGetStringValue("Name"); // null when missing
var order = job.MsgData.TryGetJson<OrderPayload>("Order");

Keep payloads small: store ids and load the rest in the handler. More types and options: Message Data.

Basic Handler Settings​

Set defaults for every job of a handler with attributes:

[JobMasterDefinitionId("hello")]
[JobMasterTimeout(30)] // seconds
[JobMasterMaxNumberOfRetries(5)]
public sealed class HelloJobHandler : IJobMasterHandler
{
public async Task HandleAsync(JobContext job)
{
await SendGreetingAsync(job.MsgData.TryGetStringValue("Name"), job.CancellationToken);
}
}
  • [JobMasterDefinitionId]: the id JobMaster stores with each job to find its handler. Without it, the class's full name is used, so renaming or moving the class breaks jobs already scheduled. Setting it on every handler is recommended.
  • [JobMasterTimeout]: how long a run may take. Without it, the cluster's default (1 minute) applies. When the timeout passes, job.CancellationToken is cancelled: pass it to your async calls so the handler stops on time, and the run is retried like any failure. A handler that ignores the token runs until it finishes.
  • [JobMasterMaxNumberOfRetries]: how many times a failed job is retried. Without it, the cluster's default (3) applies.

The same settings can be passed on the call, and the call wins over the attribute:

await scheduler.OnceNowAsync<HelloJobHandler>(msg, timeout: TimeSpan.FromMinutes(2));
A job can run more than once

JobMaster runs every job at least once. After a retry, or when a worker stops in the middle of a run, the same job runs again. Write handlers so that running them twice is safe, for example by checking whether the work is already done.

Priority, worker lanes, metadata and the other settings are covered in Job Configuration.

Next​

Recurring Jobs: run a handler on a schedule.