Skip to content

Latest commit

 

History

History
215 lines (168 loc) · 8.63 KB

reminders.md

File metadata and controls

215 lines (168 loc) · 8.63 KB

Reminders

Reminders are Solid Objects' durable equivalent of the Durable Objects Alarms API. One-shot and recurring alarms are actor-owned database records:

def schedule_evaluation
  schedule(
    at: 1.hour.from_now,
    every: 1.hour,
    missed: :latest
  ).evaluate(account_id:)
end

A recurring reminder that comes due late, for example after an outage, uses its missed: policy. The default is :latest. The policy can be a symbol or a string.

  • missed: :latest coalesces missed occurrences (the JS PRD's run_once). The scheduler enqueues one occurrence and advances to the first scheduled time after now.
  • missed: :all enqueues each missed occurrence (the JS PRD's catch_up). The scheduler advances one interval at a time, even into the past, so each missed occurrence becomes its own message on a later scheduler pass.
  • missed: :skip drops missed occurrences and waits for the next scheduled time. An occurrence that is late but still before the next scheduled time runs. Once a later scheduled time has also passed, the scheduler enqueues nothing and advances to the first scheduled time after now.

A one-shot reminder has no next scheduled time, so it fires once when it is due under every policy.

When a reminder fires

schedule(at:) stores at as the reminder's next_run_at. The scheduler treats a reminder as due when next_run_at is on or before now. Now is the time passed to run_due_reminders(now:) in a test, and otherwise the database clock, which the scheduler reads and converts to UTC. A due reminder becomes an ordinary internal mailbox message, so retry and retention of a fired reminder follow that message. Time zone and clock skew are not a documented guarantee.

A fired reminder is an internal message, so authorize_message is not called for it. An external enqueue of the same operation still is.

If the actor no longer declares the reminder's operation when it comes due, the scheduler enqueues nothing and pauses the reminder. Its status becomes "paused" and its error_class is "SolidObjects::UnknownMessage". A paused reminder is not claimed again. Scheduling the same name again arms it. A failure of an occurrence that was already enqueued does not pause the reminder: retries and dead letters stay on that message.

A schedule call needs an operation

schedule returns an object, and the reminder exists only after you call an operation on that object. A turn that calls schedule and then calls no operation on the result fails with SolidObjects::ScheduleWithoutOperation. The runtime does not retry that turn, and the message becomes a dead letter.

# Wrong. The turn fails.
schedule(at: 10.minutes.from_now, key: buyer)

# Correct.
schedule(at: 10.minutes.from_now, key: buyer).expire(buyer:)

You can keep the result and call the operation later in the same turn.

An observable cannot call schedule, with or without an operation. The turn that reads the observable fails with SolidObjects::QueryMutatedState.

A reminder is one named alarm per actor

The uniqueness key is (actor, reminder name). Scheduling a name that is already armed moves the existing alarm rather than adding a second one. The database enforces this with a unique index on (instance_id, name).

This is the same model as Orleans reminders and Durable Objects alarms. It makes a reminder safe to re-arm from a handler that may run more than once. Without a key the name is the operation, so this is a data-loss bug:

# Wrong. Every entry overwrites the previous entry's alarm.
def add(entry:)
  self.entries = entries + [ entry ]
  schedule(at: entry.fetch("wait_until")).deliver
end

Two entries leave one reminder. The earlier wake-up never happens, nothing raises, and nothing is logged except a solid_objects.reminder.replaced event.

An alarm per item, with key:

For watchdogs paired with an effect's give-up callback, see typing your on_failure handler to retain the original argument types without repeating the error hash contract.

For static checking of watchdog operation names and keyword arguments, opt into actor-specific RBS signatures. The Ruby schedule(...).recover_if_stuck(generation: ...) syntax stays the same.

Pass key: when an actor is waiting on several things at once. The key is your own identifier for the item, and it names that item's alarm, so each item gets one:

def add(entry:)
  self.entries = entries + [ entry ]
  schedule(at: entry.fetch("wait_until"), key: entry.fetch("id")).deliver
end

Two entries now leave two reminders. Scheduling the same key again moves that item's alarm and leaves the others alone, so a keyed reminder is as safe to re-arm as an unkeyed one. The operation still decides which handler runs; the key only decides which alarm is which.

A key must be non-empty, and the name it becomes must fit the 191-character column, which is checked on the composed name rather than the key alone so a long operation and a short key are caught too. JavaScript allows 255 UTF-16 code units for the same combined name. These existing schema limits remain different; use at most 191 ASCII characters for names shared across runtimes.

The key is separated from the operation by a colon, so an operation may not hold one. Otherwise an unkeyed deliver:item and a deliver keyed item would be one name, and the second would silently take the first one's alarm. A key may hold colons of its own, because the operation before the first one cannot.

One alarm for a whole queue

A key per item is not always what you want. An actor that only ever needs to know "what is next" can keep one alarm and let the handler drain everything now due before arming the next:

def add(entry:)
  self.entries = (entries + [ entry ]).sort_by { |item| item.fetch("wait_until") }
  arm_next
end

def deliver
  now = Time.current.to_i
  due, pending = entries.partition { |item| item.fetch("wait_until") <= now }
  due.each { |item| emit :send_push, **item.symbolize_keys }
  self.entries = pending
  arm_next
end

private

def arm_next
  earliest = entries.first
  return unless earliest

  schedule(at: Time.at(earliest.fetch("wait_until"))).deliver
end

That costs one reminder row instead of one per item, and a coalesced occurrence cannot strand an entry because the handler drains by time rather than by alarm. Prefer it when the queue is large and the items are interchangeable; prefer key: when an item needs its own alarm that can be moved on its own.

Cancel a reminder

unschedule cancels one reminder: by operation, by operation and key, or by the handle that the operation call on the schedule result returns. unschedule_all cancels every key of one operation.

def confirm(buyer:)
  self.holds = holds.except(buyer)
  unschedule(:expire, key: buyer)
end

def close_sale
  unschedule_all(:expire)
end

A cancellation is staged like a schedule. It commits with the state change of the same turn, and a turn that raises cancels nothing. A cancellation of a reminder that does not exist is not an error. An unknown operation raises UnknownMessage. A cancellation cannot recall an occurrence that the scheduler already turned into a message.

A reminder also stops when its handler does not re-arm it, and the destruction of an actor removes its reminders.

reminder returns the status of one reminder, or nil. reminders lists every key of one operation. Both include the changes that the current turn staged. A status reports next_run_at, interval_seconds, missed_policy, and status, and the run history of the reminder:

  • occurrence counts the times the scheduler enqueued the reminder. It starts at 0. A window that :skip drops and a retry of an enqueued message do not count.
  • last_run_at is the scheduler's now when it last enqueued the reminder, or nil if it never has. It is not when the operation finished.
  • error_class names the error that paused the reminder, or is nil.

Scheduling a name that already exists, including a one-shot that already fired, keeps occurrence and last_run_at, clears error_class, and sets the status to "scheduled". The read in the same turn shows the same history.

Self-scheduling actors should also have a low-frequency application reconciler. It may read SolidObjects::Instance.states_for, .without_pending_work, and .orphaned, but every repair must go through async. Never bulk-update actor state around the lease and fencing checks.

Suspended actors should be reported rather than silently resumed. Spread large repair batches with available_at: so reconciliation cannot stampede one mailbox or the worker fleet.