Durable.Scheduler.API (Durable v2.0.2)

View Source

CRUD operations for scheduled workflows.

This module provides functions to create, read, update, and delete scheduled workflows that run on cron expressions.

Usage

# Create a schedule
Durable.schedule(MyApp.DailyReport, "0 9 * * *", name: "daily_report")

# List schedules
{:ok, schedules} = Durable.list_schedules(enabled: true)

# Update a schedule
Durable.update_schedule("daily_report", cron: "0 10 * * *")

# Delete a schedule
Durable.delete_schedule("daily_report")

Summary

Functions

Deletes a scheduled workflow by name.

Disables a scheduled workflow.

Enables a scheduled workflow.

Gets a scheduled workflow by name.

Like get_schedule/2, but returns the schedule or raises ArgumentError.

Lists scheduled workflows with optional filters.

Like list_schedules/1, but returns the schedules or raises ArgumentError.

Registers schedules from a module's @schedule attributes.

Registers schedules from multiple modules.

Creates a new scheduled workflow.

Triggers a scheduled workflow immediately.

Turns off schedules that came from @schedule in modules and that those modules no longer declare. Called at boot, after registering them.

The next count times a schedule fires, in UTC, from its cron expression and time zone — whether or not it's enabled. Empty for a cron or a time zone that can't be worked out.

Like upcoming_runs/2, but returns the times or raises ArgumentError.

Updates a scheduled workflow.

Types

schedule_opts()

@type schedule_opts() :: [
  name: String.t(),
  workflow: String.t(),
  input: map(),
  timezone: String.t(),
  queue: atom() | String.t(),
  enabled: boolean(),
  durable: atom()
]

Functions

delete_schedule(name, opts \\ [])

@spec delete_schedule(
  String.t(),
  keyword()
) :: :ok | {:error, :not_found}

Deletes a scheduled workflow by name.

Examples

:ok = Durable.delete_schedule("daily_report")

disable_schedule(name, opts \\ [])

@spec disable_schedule(
  String.t(),
  keyword()
) :: {:ok, Durable.Storage.Schemas.ScheduledWorkflow.t()} | {:error, term()}

Disables a scheduled workflow.

Examples

{:ok, schedule} = Durable.disable_schedule("daily_report")

enable_schedule(name, opts \\ [])

@spec enable_schedule(
  String.t(),
  keyword()
) :: {:ok, Durable.Storage.Schemas.ScheduledWorkflow.t()} | {:error, term()}

Enables a scheduled workflow.

Examples

{:ok, schedule} = Durable.enable_schedule("daily_report")

get_schedule(name, opts \\ [])

@spec get_schedule(
  String.t(),
  keyword()
) ::
  {:ok, Durable.Storage.Schemas.ScheduledWorkflow.t()}
  | {:error, Durable.Query.Opts.reason()}

Gets a scheduled workflow by name.

Examples

{:ok, schedule} = Durable.get_schedule("daily_report")
{:error, :not_found} = Durable.get_schedule("nonexistent")
schedule = Durable.get_schedule!("daily_report")

get_schedule!(name, opts \\ [])

Like get_schedule/2, but returns the schedule or raises ArgumentError.

list_schedules(filters \\ [])

@spec list_schedules(keyword()) ::
  {:ok, [Durable.Storage.Schemas.ScheduledWorkflow.t()]}
  | {:error, Durable.Query.Opts.reason()}

Lists scheduled workflows with optional filters.

Filters

  • :enabled - Filter by enabled status
  • :workflow_module - Filter by module
  • :queue - Filter by queue
  • :limit - Maximum results (default: 100)
  • :offset - Offset for pagination
  • :durable - Durable instance name

A filter of the wrong type is {:error, {:invalid_option, key}}, such as a negative :limit or an :enabled that isn't a boolean. See Durable.Query.Opts.

Examples

{:ok, schedules} = Durable.list_schedules(enabled: true)
schedules = Durable.list_schedules!(queue: :reports, limit: 10)

list_schedules!(filters \\ [])

@spec list_schedules!(keyword()) :: [Durable.Storage.Schemas.ScheduledWorkflow.t()]

Like list_schedules/1, but returns the schedules or raises ArgumentError.

register(module, opts \\ [])

@spec register(
  module(),
  keyword()
) :: :ok | {:error, term()}

Registers schedules from a module's @schedule attributes.

This is called automatically during startup for modules listed in the scheduled_modules config option.

Uses upsert semantics: updates cron/timezone/input/queue but preserves the enabled status for existing schedules. A new cron or time zone takes effect now rather than after the old next run has fired.

Examples

:ok = Durable.Scheduler.API.register(MyApp.DailyReport)

register_all(modules, opts \\ [])

@spec register_all(
  [module()],
  keyword()
) :: :ok | {:error, term()}

Registers schedules from multiple modules.

Examples

:ok = Durable.Scheduler.API.register_all([MyApp.Reports, MyApp.Cleanup])

schedule(module, cron_expression, opts \\ [])

@spec schedule(module(), String.t(), schedule_opts()) ::
  {:ok, Durable.Storage.Schemas.ScheduledWorkflow.t()} | {:error, term()}

Creates a new scheduled workflow.

Arguments

  • module - The workflow module
  • cron_expression - Cron expression (e.g., "0 9 *" for 9am daily)
  • opts - Options

Options

  • :name - Schedule name (defaults to workflow name)
  • :workflow - Workflow name (defaults to first workflow in module)
  • :input - Input data for each execution
  • :timezone - Timezone for cron (default: "UTC")
  • :queue - Queue to run on (default: :default)
  • :key - Workflow key each run starts with, so a fire while the last run still holds it follows the workflow's on_duplicate: (by default it starts nothing). See the workflow keys guide
  • :enabled - Whether schedule is active (default: true)
  • :durable - Durable instance name (default: Durable)

Examples

{:ok, schedule} = Durable.schedule(MyApp.DailyReport, "0 9 * * *")

{:ok, schedule} = Durable.schedule(
  MyApp.Reports,
  "0 9 * * MON-FRI",
  name: "weekday_report",
  workflow: "generate_report",
  timezone: "America/New_York",
  queue: :reports
)

trigger_schedule(name, opts \\ [])

@spec trigger_schedule(
  String.t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Triggers a scheduled workflow immediately.

This starts a new workflow execution without waiting for the next scheduled time. The schedule's next_run_at is NOT updated.

Options

  • :input - Override the schedule's input
  • :durable - Durable instance name

Examples

{:ok, workflow_id} = Durable.trigger_schedule("daily_report")
{:ok, workflow_id} = Durable.trigger_schedule("daily_report", input: %{force: true})

turn_off_removed(modules, opts \\ [])

@spec turn_off_removed(
  [module()],
  keyword()
) :: :ok

Turns off schedules that came from @schedule in modules and that those modules no longer declare. Called at boot, after registering them.

It only speaks for modules, so nodes with different scheduled_modules don't turn off each other's. Schedules made with Durable.schedule/3 are left alone, and a schedule that's declared again comes back on.

upcoming_runs(schedule, count \\ 5)

The next count times a schedule fires, in UTC, from its cron expression and time zone — whether or not it's enabled. Empty for a cron or a time zone that can't be worked out.

{:ok, [~U[2026-10-17 09:00:00.000000Z], ~U[2026-10-18 09:00:00.000000Z], ...]} =
  Durable.Scheduler.API.upcoming_runs(schedule)

[next | _] = Durable.Scheduler.API.upcoming_runs!(schedule, 3)

A count that isn't a whole number from 0 to 1,000 is {:error, {:invalid_option, :count}}, and anything but a schedule is {:error, :not_found}.

upcoming_runs!(schedule, count \\ 5)

Like upcoming_runs/2, but returns the times or raises ArgumentError.

update_schedule(name, changes)

@spec update_schedule(
  String.t(),
  keyword()
) :: {:ok, Durable.Storage.Schemas.ScheduledWorkflow.t()} | {:error, term()}

Updates a scheduled workflow.

Updatable Fields

  • :cron_expression (or :cron) - New cron expression
  • :timezone - New timezone
  • :input - New input data
  • :queue - New queue
  • :key - New workflow key (nil for none)
  • :enabled - Enable/disable

Examples

{:ok, schedule} = Durable.update_schedule("daily_report", cron: "0 10 * * *")
{:ok, schedule} = Durable.update_schedule("daily_report", enabled: false)