DurableObject.Scheduler behaviour (DurableObject v0.2.1)

Copy Markdown View Source

Behaviour for alarm scheduling backends.

Implementations must handle:

  • Scheduling alarms for future execution
  • Cancelling pending alarms
  • Resurrecting alarms after process/node crashes

Built-in Implementations

Configuration

Polling Scheduler (default)

config :durable_object,
  scheduler: DurableObject.Scheduler.Polling,
  scheduler_opts: [
    repo: MyApp.Repo,
    polling_interval: :timer.seconds(30)
  ]

Oban Scheduler

For applications already using Oban, the Oban scheduler leverages your existing Oban infrastructure for alarm delivery.

config :durable_object,
  scheduler: DurableObject.Scheduler.Oban,
  scheduler_opts: [oban_queue: :durable_object_alarms]

You must also add the queue to your Oban configuration:

config :my_app, Oban,
  repo: MyApp.Repo,
  queues: [durable_object_alarms: 5]

If your app uses a custom Oban instance name, specify it with oban_instance:

scheduler_opts: [oban_instance: MyApp.Oban, oban_queue: :durable_object_alarms]

Summary

Callbacks

Cancel a pending alarm. Returns :ok even if the alarm doesn't exist.

Cancel all alarms for an object.

Child spec for the scheduler's supervision tree (poller, etc). Return an empty list if no children are needed.

List all pending alarms for an object. Returns a list of {alarm_name, scheduled_at} tuples.

Schedule an alarm to fire after delay_ms milliseconds. If an alarm with the same name already exists, it should be replaced.

Types

alarm_name()

@type alarm_name() :: atom()

delay_ms()

@type delay_ms() :: non_neg_integer()

object_ref()

@type object_ref() :: {module :: module(), object_id :: String.t()}

Callbacks

cancel(object_ref, alarm_name, opts)

@callback cancel(object_ref(), alarm_name(), opts :: keyword()) :: :ok | {:error, term()}

Cancel a pending alarm. Returns :ok even if the alarm doesn't exist.

cancel_all(object_ref, opts)

@callback cancel_all(object_ref(), opts :: keyword()) :: :ok | {:error, term()}

Cancel all alarms for an object.

child_spec(opts)

@callback child_spec(opts :: keyword()) :: [Supervisor.child_spec()]

Child spec for the scheduler's supervision tree (poller, etc). Return an empty list if no children are needed.

list(object_ref, opts)

@callback list(object_ref(), opts :: keyword()) ::
  {:ok, [{alarm_name(), DateTime.t()}]} | {:error, term()}

List all pending alarms for an object. Returns a list of {alarm_name, scheduled_at} tuples.

schedule(object_ref, alarm_name, delay_ms, opts)

@callback schedule(object_ref(), alarm_name(), delay_ms(), opts :: keyword()) ::
  :ok | {:error, term()}

Schedule an alarm to fire after delay_ms milliseconds. If an alarm with the same name already exists, it should be replaced.