AshOban.Signals (ash_oban v0.10.3)

Copy Markdown View Source

An Ash.Signals.Delivery that runs a listener in Oban jobs.

signals_in do
  from MyApp.Shop.Signals do
    on :order_placed, :send_receipt, delivery: {AshOban.Signals, queue: :mailers}
  end
end

When the signal is emitted, a job is inserted for each signal. The job runs the listener the way the inline delivery would have, with the actor, tenant and authorize? of the emitting action, and the signal_path that led to the signal. If the listener fails, the job is retried. With chunks, jobs are run together, and the listener runs once for each chunk, with every signal in it.

Jobs are inserted with the repo of the oban instance. When the emitting action uses the same repo and the signal is emitted :after_action, the jobs are inserted in the action's transaction, so they are only committed with the action's changes, and the listener runs after they are committed.

See the Signals guide for more.

Changing signals

A job stores its signal as it was when it was emitted, and may run after a deploy has changed the signal. Before it runs, the stored signal is loaded with the signal's current definition:

  • a field the signal no longer has is ignored;
  • a field the stored signal doesn't have is nil, or its default;
  • a required field the stored signal doesn't have, or a value the field's type can no longer load, fails the job.

So adding an optional field, or removing one, is safe. Renaming a field, adding a required one, or changing a field's type is not: to make a change like that, either give the listener an upgrader, which changes stored signals before they are loaded, or add a new signal for the new shape and emit both until the old signal's jobs have run. A job that fails to load is retried like any other, which leaves time to deploy an upgrader.

The same applies to listeners. A job for a listener that no longer exists, or that now listens to a different signal, fails, and is retried until it is discarded. Before removing or renaming a listener, or the signal it listens to, wait for its jobs to finish. During a rolling deploy, a job inserted by a new node may be picked up by an old one, fail, and run when it is retried on a new node.

Options

  • :queue (atom/0) - The queue to insert jobs into. The default value is :default.

  • :oban (term/0) - The name of the Oban instance to insert jobs into. The default value is Oban.

  • :priority - The priority of the jobs, from 0 (highest) to 9 (lowest). Valid values are between 0 and 9 The default value is 0.

  • :max_attempts (pos_integer/0) - How many times a job is attempted before it is discarded. The default value is 20.

  • :tags (list of String.t/0) - Tags added to the jobs. The default value is [].

  • :unique - Prevents inserting a job for the listener while another one for the same values of the given signal fields exists. By default, jobs are unique by signal_id, so emitting the same signal again doesn't insert a second job while the first one is incomplete. false turns uniqueness off.

  • :chunks (keyword/0) - Runs the listener with many signals at once, using an Oban Pro chunk worker. Create, update and destroy listeners handle a chunk's signals with one bulk action, and generic actions with batch?: true receive them as a list. Requires Oban Pro, and the AshOban extension on the listener's resource, which defines the worker.

  • :upgrader (module that adopts AshOban.Signals.Upgrader or a module and options) - Upgrades signals stored by an older version of the signal. See AshOban.Signals.Upgrader.

  • :actor_persister - The AshOban.ActorPersister used to store the actor in jobs. Defaults to config :ash_oban, :actor_persister. Without one, the listener runs without an actor.

  • :shared_context - Context passed to the listener's action as shared context. See AshOban.build_context/2.