Skip to main content

Static Schedules

A static schedule is declared in code and registered when the app starts, instead of being created by a call at runtime. It fits fixed system routines: a nightly cleanup, a sync every hour, a weekly report.

There are two ways to declare one: an attribute on the handler, or a profile class.

Schedule Attributes​

The simplest way: put a schedule attribute on the handler.

[JobMasterDefinitionId("inventory.sync")]
[NaturalCronSchedule("every 6 minutes")]
public sealed class InventorySyncHandler : IJobMasterHandler
{
public async Task HandleAsync(JobContext job) { ... }
}
AttributeExpressionPackage
[NaturalCronSchedule("every day at 09:00")]NaturalCronbuilt in
[TimeSpanIntervalSchedule("00:06:00")]a fixed interval, hh:mm:ssbuilt in
[CronosSchedule("*/5 * * * *")]standard cronJobMaster.Cronos
[NCrontabSchedule("*/5 * * * *")]standard cronJobMaster.NCrontab

A handler can have more than one, for more than one schedule:

[NaturalCronSchedule("every day at 09:00")]
[NaturalCronSchedule("every day at 18:00")]
public sealed class DailyReportHandler : IJobMasterHandler { ... }

The attribute only carries the expression. Everything else comes from the handler's own attributes (priority, timeout, retries, worker lane, cluster, metadata):

[NaturalCronSchedule("every 6 minutes")]
[JobMasterPriority(JobMasterPriority.High)]
[JobMasterWorkerLane("inventory")]
public sealed class InventorySyncHandler : IJobMasterHandler { ... } // every run: High, "inventory" lane

Profiles​

A profile groups several schedules in one class, and lets each schedule set its own options instead of taking them only from the handler:

public sealed class MaintenanceProfile : IStaticRecurringSchedulesProfile
{
public static string ProfileId => "Maintenance";

public static void Config(RecurringScheduleDefinitionCollection collection)
{
collection
.Add<CleanupHandler>(TimeSpan.FromDays(1))
.Add<SyncHandler>(
NaturalCronExprCompiler.TypeId,
"every 1 hour between 09:00 and 18:00",
defId: "HourlySync",
priority: JobMasterPriority.Low,
timeout: TimeSpan.FromMinutes(10));
}
}
  • ProfileId identifies the profile; two profiles with the same ProfileId fail at startup.
  • Add<THandler>(...) adds one schedule. It takes the expression and, optionally, defId (to tell apart two schedules of the same handler), priority, timeout, maxNumberOfRetries, metadata, startAfter and endBefore. A value set here overrides the handler's attribute.
  • Profiles are found automatically at startup, like handlers.

A profile can't pass message data: a static schedule has no per-schedule payload. For a schedule that needs its own data (for example one per customer), create it from code; see Recurring Schedule.

Which Cluster​

A static schedule is registered on:

  1. the profile's ClusterId, if it sets one;
  2. otherwise the handler's [JobMasterClusterId];
  3. otherwise the default cluster.
public sealed class MaintenanceProfile : IStaticRecurringSchedulesProfile
{
public static string ProfileId => "Maintenance";
public static string ClusterId => "ops-cluster"; // every schedule of this profile
// ...
}

Without a profile ClusterId, one profile can register schedules on different clusters, each following its handler.

The worker lane works the same way: a profile's public static string? WorkerLane => "maintenance"; applies to all its schedules, and without it each schedule takes its handler's [JobMasterWorkerLane].

The cluster and the worker lane are resolved when the schedule is registered, at every startup, so a changed attribute takes effect on the next deploy. Priority, timeout and retries are resolved at every run.

When the Code Changes​

Static schedules are registered again at every startup, from what the code declares:

  • A changed expression or option replaces the stored one.
  • A schedule that no running app declares anymore (its schedule attribute or profile Add was removed) stops running and is retired, through a heartbeat:
    • every running app renews the static schedules it declares, every minute;
    • once no running app has renewed a schedule for about 5 minutes (for example after every app was deployed without it), no more jobs run for it;
    • after an hour without renewal, it becomes Inactive;
    • like any finished schedule, it's then deleted by the cluster's DataRetentionTtl, if one is set.

Static vs. From Code​

From codeStatic
Typical usePer-entity: a subscription renewal, a per-customer reportSystem routines: cleanup, backups, syncs
Message dataYes, per scheduleNo
Created byA call to IJobMasterScheduler at runtimeAn attribute or a profile, registered at startup
Removed byCancelling it, or its endBeforeRemoving it from the code