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) { ... }
}
| Attribute | Expression | Package |
|---|---|---|
[NaturalCronSchedule("every day at 09:00")] | NaturalCron | built in |
[TimeSpanIntervalSchedule("00:06:00")] | a fixed interval, hh:mm:ss | built in |
[CronosSchedule("*/5 * * * *")] | standard cron | JobMaster.Cronos |
[NCrontabSchedule("*/5 * * * *")] | standard cron | JobMaster.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));
}
}
ProfileIdidentifies the profile; two profiles with the sameProfileIdfail 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,startAfterandendBefore. 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:
- the profile's
ClusterId, if it sets one; - otherwise the handler's
[JobMasterClusterId]; - 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
Addwas 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 code | Static | |
|---|---|---|
| Typical use | Per-entity: a subscription renewal, a per-customer report | System routines: cleanup, backups, syncs |
| Message data | Yes, per schedule | No |
| Created by | A call to IJobMasterScheduler at runtime | An attribute or a profile, registered at startup |
| Removed by | Cancelling it, or its endBefore | Removing it from the code |