Scheduled Commands
Some things should happen later: an order that cancels itself if it is not paid in time, a reminder, a nightly import. Wow schedules commands as part of a commit, so a schedule is written with the events that set it, or not at all, and it survives restarts and handovers like the log does. This page schedules a command from an aggregate, from a saga, and runs a recurring job.
When a schedule is due
ScheduleRule says when:
| Rule | Is due |
|---|---|
ScheduleRule.at(instant) | At an instant |
ScheduleRule.after(duration) | After a delay counted from the commit that sets it |
ScheduleRule.cron(expression, timeZone, misfire) | Each time a cron expression matches, six fields or a macro such as @daily, read in timeZone (UTC by default) |
A schedule belongs to an aggregate, under a key. Setting the key again replaces it, and cancelling it removes it.
Schedule from a decision
A command function schedules with the DecideScope. When the order's last product is confirmed, it schedules its own cancellation once the payment grace period has passed:
/**
* Confirms one product. A confirmation the order does not wait for changes nothing: one read again from the
* stock stream, or one that arrives after another product's shortage cancelled the order (that reservation is
* released when the inventory hears of the cancellation).
*/
// Decision: the last confirmation produces two events, the item's and the order's.
@OnCommand(
produces = [OrderStockItemConfirmed::class, OrderStockConfirmed::class],
schedules = [CancelOrder::class],
)
fun onCommand(command: ConfirmOrderStock, paymentPolicy: PaymentPolicy, scope: DecideScope): Decision {
val waiting = state.status == OrderStatus.AWAITING_VALIDATION &&
command.productId in state.products() &&
command.productId !in state.confirmedProducts
if (!waiting) {
return Decision.NoOp
}
val confirmed = OrderStockItemConfirmed(command.productId)
if (state.confirmedProducts + command.productId != state.products()) {
return Decision.events(confirmed)
}
scope.schedule(
PAYMENT_GRACE_PERIOD,
ScheduleRule.after(paymentPolicy.gracePeriod),
CancelOrder(PAYMENT_TIMEOUT)
)
return Decision.events(confirmed, OrderStockConfirmed)
}scope.schedule(key, rule, command, target)sendscommandtotarget, this aggregate unless another ref is given. A ref names its tenant too, so one of another tenant's aggregate sends the command into that tenant, as a saga'stargetTenantdoes, but to a type of a fixed tenant, which refuses another: which tenants a schedule reaches is the domain's to decide, so build the ref from what the aggregate holds, or check one from the command first. The schedule is committed with the decision's events, as the system eventwow.schedule.ScheduleSet; a decision that only schedules commits just that. The event holds the command until it is sent, and wherever the event is read (a trace, an events query) or published, the command is masked as a body of its command type is, with only what the node's schema of that type declares (Where masking stops); the command the timer sends is the one scheduled, whole. A cron rule whose expression or time zone cannot be read, or that names no time that ever comes (0 0 0 30 2 *), fails the command (wow.command.failed), and nothing is committed: a rule from input is checked by the domain before it is scheduled, so that it rejects it with a code of its own.scope.cancelSchedule(key)cancels it; for a key the aggregate keeps no schedule of, it changes nothing, aswow.schedule.CancelScheduledoes. The order cancelspayment-grace-periodwhen it is paid. A command already sent is not recalled: its target's state guards it.- The function declares the scheduled commands' classes in
@OnCommand(schedules = [...]). The compiler cannot read the calls in a function's body, and the Manifest lists them by full type name. The declaration is what the Manifest says; the kernel does not refuse a schedule it leaves out.
Schedule from a saga
A saga records the schedule in its SagaContext, from any of its handlers: context.schedule(key, rule, request) asks the request's target to schedule its command for itself, and context.cancelSchedule(key, target) cancels it. The runtime sends both after the commands the handler returns, in the order the handler called them, as the system commands wow.schedule.SetSchedule and wow.schedule.CancelSchedule. The target must exist; otherwise it rejects the wow.schedule.SetSchedule, which the saga's @OnRejected handler of SetSchedule gets. The handler declares the scheduled commands' classes in @Sends(schedules = [...]): scheduling a class it does not declare fails the handling for good, and the aggregate parks at once (Write a Saga). So does a cron rule that cannot be read or names no time that ever comes; a target that receives such a wow.schedule.SetSchedule rejects it (wow.command.invalid). A target stores what a schedule's command's class reads, each value as Wow's JSON writes it, which leaves a write-only value out, so a schedule refuses a command that holds one that is not null (The Manifest): the saga's handling fails, and a target that receives such a wow.schedule.SetSchedule rejects it (wow.command.invalid), as it rejects one whose command does not read as its class (wow.command.malformed) when the schedule is set, where it would fail only when it fired. It rejects as invalid, too, a command holding a value the two writings do not pair (an identity a generator gives at each writing, a class written as an array whose items are not its properties), and one whose stored command its class does not read back (a constructor parameter with no property), and a saga's or a decide function's schedule of such a command fails as one of a write-only value does.
How a scheduled command is delivered
- It is sent at least once, never early, possibly a little late: after a handover or a backlog, and within the timers' poll interval,
wow.timers.poll-interval(1 second by default). - It is decided once. Its request id is the timer and the slot (
wow:timer#slot), so sending it again is a duplicate the target recognizes. No caller's request id starts withwow:, so none can be committed first under a timer's. The target reads which slot it was sent for fromcommand.schedule. - The target decides it on its state then. A rejection is a normal guard, not a failure: a cancellation that arrives after the order was paid is rejected, the timer is done, and the rejection goes to no
@OnRejectedhandler. - A technical failure to send it is retried,
wow.timers.max-attemptstimes (10 by default), then parked underwow.timers.sends, where an operator retries or skips it (Parked aggregates). A busy target is tried again a second later without spending an attempt (How a command is retried). - It carries the scope of the commit that scheduled it, and that commit as its causation.
- Deleting an aggregate cancels its pending schedules. A scheduled command is not part of the wait of the command that scheduled it.
Run a recurring job
A job that is not about one aggregate, such as a nightly import, is an @OnSchedule handler of a saga. It takes a ScheduleTick and, if it needs it, the SagaContext, and returns the commands it derives, as an @OnEvent handler does. Through the context it may also schedule and call. When its return type does not name the commands, it declares them in @Sends(derives = [...]), as any saga handler does. Every morning, the inventory receives the suppliers' deliveries of the day before into stock:
@Blocking
@OnSchedule(name = "receive-deliveries", cron = "0 0 6 * * *")
fun onSchedule(tick: ScheduleTick): List<CommandRequest<ReceiveStock>> {
val today = Instant.ofEpochMilli(tick.due).atZone(ZoneOffset.UTC).toLocalDate()
val days = (tick.skipped + 1 downTo 1).map { today.minusDays(it) }
return days.flatMap { day -> deliveries.deliveredOn(tick.tenantId, day) }.map { delivery ->
// The stock of the product, in the job instance's tenant: ReceiveStock names its aggregate type. Keyed by
// the delivery, so two deliveries of one product stay two requests in whatever order the feed lists them.
ReceiveStock(delivery.quantity).toCommandRequest(delivery.productId) { requestId(delivery.id) }
}
}- The tick.
tenantIdis the instance's tenant andjobthe job's name.slotis the time that was due, counted from 1 since the instance was last enabled or rescheduled, andduewhen, in epoch milliseconds.skippedcounts the slots before it that were missed while no node ran the job.partitionis the instance's log partition, for aSHARDEDjob only. - The commands. A job's commands go to its instance's tenant, as a saga's go to the tenant of its commit:
body.toCommandRequest(id)names the aggregateidof the type that decides the body, in that tenant, as in the sample, andtargetTenantin its block sends one to another tenant. Their ids come from the tick's execution, so handling the tick again sends the same requests. - The instance. Each job is kept by the context's built-in aggregate
<context>.schedule: aSYSTEMorTENANTinstance's id is the job's name, and aSHARDEDjob has one instance per partition. Each time the rule is due it commits a tick, and the job's subscription,<processor>.<function>(or the function's@Name), hands it to the handler, retried and parked as any subscription is. A job whose handler a new version removes, the last of its context's included, is disabled at its instances' next tick, and their schedule cancelled.
@OnSchedule sets the job:
| Setting | Means |
|---|---|
name | The job's name, unique in its context: letters, digits, _ and - |
cron | Six fields, as Spring's CronExpression reads them: 0 0 6 * * * is 06:00 every day. A macro such as @daily is read too |
timeZone | The time zone cron is read in, UTC by default |
scope | SYSTEM, the default: one instance, in the default tenant, enabled by the node that owns its partition. TENANT: one instance per tenant, created by the system command wow.schedule.EnableJob in that tenant; the host never starts one, and each tenant may override the rule, its time zone and its misfire. SHARDED: one instance per log partition, in the default tenant, enabled and run by the partition's owner for its own partition's aggregates, each ticked once per slot with tick.partition set |
misfire | COALESCE, the default: of the slots missed while no node ran the job, only the latest runs, and skipped counts the others. Its due is that slot's own time, a time the expression names, and wow.timers.expires-after judges it late from that time; beyond 100,000 missed slots skipped is an estimate. CATCH_UP: each missed slot runs |
The host configures a job under wow.schedules.<name>, and only there, without a new build:
wow:
schedules:
receive-deliveries:
cron: "0 30 5 * * *"
retry:
max-attempts: 3cron,time-zoneandmisfireoverride the annotation's rule.enabled: falsedisables aSYSTEMorSHARDEDjob's instances, each time a node acquires their partition, a node's start among them. Set back, the next acquisition enables the instances it disabled, but not one an operator disabled. While it is false, an operator'senable-joblasts until the instance is next brought in line, when a node next acquires its partition.retry.*andparked.*are the retries of the job's subscription, over its handler's@Retryandwow.subscription.*(Failures and Retries). A job's subscription configured underwow.subscriptionsinstead, or settings for a job no handler runs, stop the node at startup.
An instance's own rule comes before the host's: a tenant's of a TENANT job, or one an operator set with reschedule-job. It is kept by the instance, so it outlives a restart. So does an operator's disable: disable-job holds the instance (its JobDisabled is held) until an operator's enable-job, and no acquisition of its partition, nor enabled: true, enables it again. A disable that is not held is the host's: the configuration's, or that of a job a new version removed. A job's own handler that disables an instance disables it as the host does, not held, since the commands it derives keep the tick's operator.
A job's instances are brought in line by the node that acquires their partition, by that node's definition of the job: a job a new release adds is enabled once a node of that release acquires its instance's partition, and a changed default applies then. During a rolling upgrade, a node of the earlier release that acquires the partition brings the instance in line by its own definition, and logs what it changed; once the roll is over, the last owner's definition applies.
The job's instances also take commands over HTTP, POST /<context>/schedule/{id}/wow.schedule.<command>: enable-job, reschedule-job, disable-job, the job's name as the id. They are Wow's system commands, so the public port never allows them by default: a service refuses them (403 wow.auth.forbidden) unless an authorizer decides them, CommandAuthorizer.operational { … } or CoSec's, and then it decides. Under CoSec the action is <context>:wow.schedule.EnableJob (and the other job commands) on schedule/<job>, which a policy grants to the operators (Action names). The jobs' own commands, the sagas' and the commands ops sends on the internal port are not refused.
Test it
A test of a decision that schedules expects the schedule with the aggregate's events (Test Domain Code). A job is tested with scheduleSpec, against the real job aggregate: enable the instance, move the clock, and check the commands its handler derived, with the expectations of a saga's test:
@Test
fun `the morning job should receive yesterday's deliveries`() = scheduleSpec(
handlers,
start = Instant.parse("2026-10-20T00:00:00Z"),
) {
enable("receive-deliveries")
advanceTime(6.hours) {
expectCommands(
stock.ref(TENANT, "mug") to ReceiveStock(40),
stock.ref(TENANT, "cap") to ReceiveStock(12),
)
}
asked.assert().containsExactly(LocalDate.parse("2026-10-19"))
}advanceTimemoves the clock and hands over the ticks that fell due; given an expectation block, it fails when none did, so a wrong rule does not pass quietly. Without one, it fails only when a handler or a job command throws.tickslists the ticks handed over.rescheduleanddisablesend the instance's other commands, andenabletakes atenantId, aruleor apartitionfor the other scopes.whenTickhands the handler a tick of the test's own, without the job aggregate: a slot the clock would take long to reach, or a tick withskippedset.