Skip to main content

Advanced Scheduling

scheduler.OnceNowAsync<InvoiceHandler>(...) needs the handler type, so the app that schedules the job must reference the handler's project, and with it everything the handler depends on (EF Core, SDKs, other services). That's fine when one app both schedules and runs the job. When they're separate apps and you want true assembly separation, for example a REST API that only produces jobs and a worker service that runs them (producers and consumers), the producer shouldn't need any of that.

scheduler.Advanced schedules a job from a job definition instead of a handler type: an id, plus optional default settings. The producer only knows the definition; the consumer has a handler with the same id.

Settings Come from the Definition​

A job's settings are resolved when it's scheduled, in the producer. The producer doesn't have the handler, so the handler's attributes ([JobMasterTimeout], [JobMasterPriority], …) can't be read there and are not used. Only the definition's settings apply, and whatever the definition leaves out falls back to the cluster's default or JobMaster's own.

So put the settings on the definition. On the consumer's handler only the id matters.

Option 1: a Definition Built in the Producer​

The producer builds the definition where it schedules:

var definition = new JobDefinitionConfig(
"orders.process",
priority: JobMasterPriority.High,
timeout: TimeSpan.FromMinutes(2));

await scheduler.Advanced.OnceNowAsync(definition, msg);

The consumer's handler has the same id:

[JobMasterDefinitionId("orders.process")]
public sealed class ProcessOrderHandler : IJobMasterHandler
{
public async Task HandleAsync(JobContext job) { ... }
}

To avoid typos without sharing more, keep the id as a constant in a small shared file: public static class JobDefinitionIds { public const string ProcessOrder = "orders.process"; }.

Option 2: a Definition in a Shared Project​

Put the definition in a small project that both apps reference, with no dependency on the handler:

// Shared contracts project
public sealed class ProcessOrderDefinition : JobDefinitionConfigAttribute, IStaticJobDefinitionConfig
{
public static JobDefinitionConfig Config { get; } = new JobDefinitionConfig(
"orders.process",
priority: JobMasterPriority.High,
timeout: TimeSpan.FromMinutes(2));
}

The producer schedules with the definition type, and can still override a setting for one call:

await scheduler.Advanced.OnceNowAsync<ProcessOrderDefinition>(msg);
await scheduler.Advanced.OnceNowAsync<ProcessOrderDefinition>(msg, priority: JobMasterPriority.Critical);

The consumer puts the same definition on its handler, as an attribute:

[ProcessOrderDefinition]
public sealed class ProcessOrderHandler : IJobMasterHandler
{
public async Task HandleAsync(JobContext job) { ... }
}

Both apps now share one definition: the compiler catches a typo in the id, and the settings can't drift apart between them.

Which Option?​

  • Option 1 when a shared project isn't worth it, and the id is a documented contract between the two apps.
  • Option 2 when you want the compiler to keep both apps in line: same id, same settings.

Where Each Setting Comes From​

1. Call2. Definition3. Default
OnceNowAsync<TDefinition>(...)Any option (clusterId, priority, workerLane, timeout, maxNumberOfRetries, metadata)Its ConfigThe cluster's default, or JobMaster's
OnceNowAsync(definition, ...)clusterId onlyThe JobDefinitionConfig you passThe cluster's default, or JobMaster's

The defaults are the same as for any job (see the table): priority Medium, the cluster's timeout and retries, no lane, the default cluster.

Every scheduling method has an Advanced counterpart: OnceNow, OnceAt, OnceAfter and Recurring, each with an …Async version.

Recurring Schedules​

scheduler.Advanced.RecurringAsync creates schedules from code from a definition too:

await scheduler.Advanced.RecurringAsync<ProcessOrderDefinition>(NaturalCronExprCompiler.TypeId, "every day at 06:00");

Unlike a one-off job, each run of a recurring schedule is created on a consumer, which has the handler: a setting the definition leaves out can still come from the handler's attributes there.

Static schedules (schedule attributes and profiles) still need the handler type, so they're declared in the consumer.

Rules​

These fail at startup, with all errors listed together:

  • A handler with both a definition attribute ([ProcessOrderDefinition]) and the individual attributes ([JobMasterDefinitionId], [JobMasterTimeout], [JobMasterPriority], [JobMasterWorkerLane], [JobMasterMaxNumberOfRetries], [JobMasterClusterId]): use one or the other.
  • Two handlers with the same definition id.
  • A definition whose clusterId isn't configured, in any app that loads the definition type, producer and consumer alike.
  • A definition class without its static Config (on .NET 7 and later the compiler already catches it).

Injecting Only the Advanced Scheduler​

A service that only schedules from definitions can inject IJobMasterSchedulerAdvanced directly instead of going through IJobMasterScheduler.Advanced:

public sealed class OrdersController(IJobMasterSchedulerAdvanced scheduler) { ... }