Durable.Query (Durable v2.0.2)

View Source

Query functions for workflow executions.

Every function returns {:ok, result} or {:error, reason}, and has a twin ending in ! that returns the result or raises ArgumentError:

{:ok, runs} = Durable.Query.list_executions(status: :failed)
runs = Durable.Query.list_executions!(status: :failed)

A wrong option is {:error, {:invalid_option, key}} and an id that names nothing is {:error, :not_found}. Durable.Query.Opts has the whole contract.

Summary

Functions

Returns a %{parent_workflow_id => child_count} map for the given parent ids, in a single query. Used to render a "N children" drill-down affordance on top-level run rows without an N+1. Parents with no children are omitted.

Like child_counts/2, but returns the counts or raises ArgumentError.

Counts runs per status, applying every filter except :status — what a "Failed (12) · Running (3)" view switcher needs in one query.

Like count_by_status/1, but returns the counts or raises ArgumentError.

Counts workflow executions matching filters, the filters of list_executions/1.

Like count_executions/1, but returns the count or raises ArgumentError.

Counts the keys list_keys/1 would list, ignoring :limit and :offset.

Like count_keys/1, but returns the count or raises ArgumentError.

Returns workflow execution counts grouped by status.

Like dashboard_counts/1, but returns the counts or raises ArgumentError.

Gets a workflow execution by ID.

Gets step executions for a workflow, oldest first.

Gets logs for a specific step, from its latest attempt.

Lists child workflow executions for a parent workflow.

Lists workflow executions with optional filters.

Like list_executions/1, but returns the runs or raises ArgumentError.

Lists workflow executions with total count for pagination.

Workflow keys: one row per workflow and key that a run was started with, most recently started first.

Like list_keys/1, but returns the keys or raises ArgumentError.

Lists distinct workflow definitions, derived from execution history.

Like list_workflows/1, but returns the workflows or raises ArgumentError.

Functions

child_counts(parent_ids, opts \\ [])

@spec child_counts(
  [String.t()],
  keyword()
) ::
  {:ok, %{required(String.t()) => non_neg_integer()}}
  | {:error, Durable.Query.Opts.reason()}

Returns a %{parent_workflow_id => child_count} map for the given parent ids, in a single query. Used to render a "N children" drill-down affordance on top-level run rows without an N+1. Parents with no children are omitted.

Options

  • :durable - The Durable instance name (default: Durable)

Anything but a list of run ids is {:error, {:invalid_option, :parent_ids}}.

Examples

{:ok, %{^parent_id => 3}} = Durable.Query.child_counts([parent_id, other_id])
%{^parent_id => 3} = Durable.Query.child_counts!([parent_id, other_id])

child_counts!(parent_ids, opts \\ [])

@spec child_counts!(
  [String.t()],
  keyword()
) :: %{required(String.t()) => non_neg_integer()}

Like child_counts/2, but returns the counts or raises ArgumentError.

count_by_status(filters \\ [])

@spec count_by_status(keyword()) ::
  {:ok, %{required(atom()) => non_neg_integer()}}
  | {:error, Durable.Query.Opts.reason()}

Counts runs per status, applying every filter except :status — what a "Failed (12) · Running (3)" view switcher needs in one query.

{:ok, %{failed: 12, running: 3}} = Durable.Query.count_by_status(queue: :default)
%{failed: 12, running: 3} = Durable.Query.count_by_status!(queue: :default)

count_by_status!(filters \\ [])

@spec count_by_status!(keyword()) :: %{required(atom()) => non_neg_integer()}

Like count_by_status/1, but returns the counts or raises ArgumentError.

count_executions(filters \\ [])

@spec count_executions(keyword()) ::
  {:ok, non_neg_integer()} | {:error, Durable.Query.Opts.reason()}

Counts workflow executions matching filters, the filters of list_executions/1.

{:ok, 3} = Durable.Query.count_executions(status: :failed)
3 = Durable.Query.count_executions!(status: :failed)

count_executions!(filters \\ [])

@spec count_executions!(keyword()) :: non_neg_integer()

Like count_executions/1, but returns the count or raises ArgumentError.

count_keys(filters \\ [])

@spec count_keys(keyword()) ::
  {:ok, non_neg_integer()} | {:error, Durable.Query.Opts.reason()}

Counts the keys list_keys/1 would list, ignoring :limit and :offset.

{:ok, 12} = Durable.Query.count_keys(workflow_name: "process_article")
12 = Durable.Query.count_keys!(workflow_name: "process_article")

count_keys!(filters \\ [])

@spec count_keys!(keyword()) :: non_neg_integer()

Like count_keys/1, but returns the count or raises ArgumentError.

dashboard_counts(opts \\ [])

@spec dashboard_counts(keyword()) ::
  {:ok, %{required(atom()) => non_neg_integer()}}
  | {:error, Durable.Query.Opts.reason()}

Returns workflow execution counts grouped by status.

Returns a map like %{pending: 5, running: 3, completed: 100, ...}.

Options

  • :durable - The Durable instance name (default: Durable)

Examples

{:ok, %{running: 3, completed: 100}} = Durable.Query.dashboard_counts()
%{running: 3, completed: 100} = Durable.Query.dashboard_counts!()

dashboard_counts!(opts \\ [])

@spec dashboard_counts!(keyword()) :: %{required(atom()) => non_neg_integer()}

Like dashboard_counts/1, but returns the counts or raises ArgumentError.

get_execution(workflow_id, opts \\ [])

@spec get_execution(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, Durable.Query.Opts.reason()}

Gets a workflow execution by ID.

Options

  • :include_steps - Include step executions (default: false)
  • :include_logs - Include logs in step executions (default: false)
  • :durable - The Durable instance name (default: Durable)

Examples

{:ok, run} = Durable.Query.get_execution(workflow_id, include_steps: true)
{:error, :not_found} = Durable.Query.get_execution("no-such-run")
run = Durable.Query.get_execution!(workflow_id)

get_execution!(workflow_id, opts \\ [])

@spec get_execution!(
  String.t(),
  keyword()
) :: map()

Like get_execution/2, but returns the run or raises ArgumentError.

get_step_executions(workflow_id, opts \\ [])

@spec get_step_executions(
  String.t(),
  keyword()
) :: {:ok, [map()]} | {:error, Durable.Query.Opts.reason()}

Gets step executions for a workflow, oldest first.

{:ok, steps} = Durable.Query.get_step_executions(workflow_id)
{:error, :not_found} = Durable.Query.get_step_executions("no-such-run")
steps = Durable.Query.get_step_executions!(workflow_id)

get_step_executions!(workflow_id, opts \\ [])

@spec get_step_executions!(
  String.t(),
  keyword()
) :: [map()]

Like get_step_executions/2, but returns the steps or raises ArgumentError.

get_step_logs(workflow_id, step_name, opts \\ [])

@spec get_step_logs(String.t(), atom() | String.t(), keyword()) ::
  {:ok, [map()]} | {:error, Durable.Query.Opts.reason()}

Gets logs for a specific step, from its latest attempt.

{:ok, logs} = Durable.Query.get_step_logs(workflow_id, :charge)
{:error, :not_found} = Durable.Query.get_step_logs(workflow_id, :no_such_step)
logs = Durable.Query.get_step_logs!(workflow_id, :charge)

get_step_logs!(workflow_id, step_name, opts \\ [])

@spec get_step_logs!(String.t(), atom() | String.t(), keyword()) :: [map()]

Like get_step_logs/3, but returns the logs or raises ArgumentError.

list_child_executions(parent_workflow_id, opts \\ [])

@spec list_child_executions(
  String.t(),
  keyword()
) :: {:ok, [map()]} | {:error, Durable.Query.Opts.reason()}

Lists child workflow executions for a parent workflow.

Options

  • :status - Filter by status
  • :durable - The Durable instance name (default: Durable)

Examples

{:ok, children} = Durable.Query.list_child_executions(parent_id, status: :running)
{:error, :not_found} = Durable.Query.list_child_executions("no-such-run")
children = Durable.Query.list_child_executions!(parent_id)

list_child_executions!(parent_workflow_id, opts \\ [])

@spec list_child_executions!(
  String.t(),
  keyword()
) :: [map()]

Like list_child_executions/2, but returns the runs or raises ArgumentError.

list_executions(filters \\ [])

@spec list_executions(keyword()) ::
  {:ok, [map()]} | {:error, Durable.Query.Opts.reason()}

Lists workflow executions with optional filters.

Filters

  • :workflow - Filter by workflow module
  • :workflow_name - Filter by workflow name
  • :status - Filter by status
  • :queue - Filter by queue
  • :current_step - Runs currently at (or that stopped at) this step
  • :trigger - What started the run: :api, :schedule, :parent or :rerun
  • :trigger_ref - What it points at: a schedule's name, or the run a rerun copied
  • :error_contains - Case-insensitive text anywhere in the run's error
  • :key - Runs started with this workflow key (the holder has holds_key: true)
  • :tag - Runs started with this tag (Durable.start(..., tags: [...]))
  • :input - Map of dotted input paths to exact values, e.g. %{"order_id" => "ORD-1042"}
  • :id, :parent_workflow_id - One run, or the children of one
  • :id_prefix - Runs whose id starts with this text
  • :from, :to - Runs started in this window (DateTime)
  • :top_level_only - Leave out child runs
  • :limit - Maximum results (default: 50)
  • :offset - Offset for pagination (default: 0)
  • :durable - The Durable instance name (default: Durable)

A filter of the wrong type is {:error, {:invalid_option, key}}: a negative :limit, a status no run has, a :workflow that isn't a module.

Examples

{:ok, runs} = Durable.Query.list_executions(status: [:failed, :cancelled], limit: 20)
{:error, {:invalid_option, :limit}} = Durable.Query.list_executions(limit: -1)
runs = Durable.Query.list_executions!(tag: "account:7")

list_executions!(filters \\ [])

@spec list_executions!(keyword()) :: [map()]

Like list_executions/1, but returns the runs or raises ArgumentError.

list_executions_with_total(filters \\ [])

@spec list_executions_with_total(keyword()) ::
  {:ok, {[map()], non_neg_integer()}} | {:error, Durable.Query.Opts.reason()}

Lists workflow executions with total count for pagination.

Returns {:ok, {executions, total_count}}.

Accepts the same filters as list_executions/1.

{:ok, {runs, total}} = Durable.Query.list_executions_with_total(limit: 25, offset: 50)
{runs, total} = Durable.Query.list_executions_with_total!(limit: 25, offset: 50)

list_executions_with_total!(filters \\ [])

@spec list_executions_with_total!(keyword()) :: {[map()], non_neg_integer()}

Like list_executions_with_total/1, but returns the pair or raises ArgumentError.

list_keys(filters \\ [])

@spec list_keys(keyword()) :: {:ok, [map()]} | {:error, Durable.Query.Opts.reason()}

Workflow keys: one row per workflow and key that a run was started with, most recently started first.

[%{workflow_module: "Elixir.MyApp.ProcessArticle", workflow_name: "process_article",
   key: "article:42", runs: 2, last_started_at: ~U[...],
   holder_id: "3f2a91c4-…", holder_status: :running,   # nil when no run holds it
   readded: 3, dropped: 1, last_readded_at: ~U[...]}]  # in the last 24 hours

Filters

  • :workflow_name - One workflow
  • :key_prefix - Keys that start with this text
  • :holder_status - Status or statuses of the run holding the key
  • :limit (default 50), :offset
  • :durable - The Durable instance name (default: Durable)

Examples

{:ok, keys} = Durable.Query.list_keys(key_prefix: "article:")
keys = Durable.Query.list_keys!(holder_status: :running)

list_keys!(filters \\ [])

@spec list_keys!(keyword()) :: [map()]

Like list_keys/1, but returns the keys or raises ArgumentError.

list_workflows(opts \\ [])

@spec list_workflows(keyword()) ::
  {:ok, [map()]} | {:error, Durable.Query.Opts.reason()}

Lists distinct workflow definitions, derived from execution history.

Returns one row per {workflow_module, workflow_name} pair that has ever been executed, with aggregate stats. There is no compile-time registry of definitions in the engine — the executor only resolves modules by name on demand — so the dashboard derives the catalog from the persisted executions.

Each row contains:

  • :workflow_module (string)
  • :workflow_name (string)
  • :total_runs (non_neg_integer)
  • :running_count (non_neg_integer)
  • :waiting_count (non_neg_integer)
  • :failed_count (non_neg_integer)
  • :last_run_at (DateTime.t() | nil) — most recent inserted_at

  • :last_status (atom | nil) — status of the most recent run

Sorted by last_run_at DESC NULLS LAST.

Options

  • :durable - The Durable instance name (default: Durable)

Examples

{:ok, workflows} = Durable.Query.list_workflows()
workflows = Durable.Query.list_workflows!()

list_workflows!(opts \\ [])

@spec list_workflows!(keyword()) :: [map()]

Like list_workflows/1, but returns the workflows or raises ArgumentError.