View Source Oban.Plugin behaviour (Oban v2.12.0)

Defines a shared behaviour for Oban plugins.

In addition to implementing the Plugin behaviour, all plugins must be a GenServer, Agent, or another OTP compliant module.

example

Example

Defining a basic plugin that satisfies the minimum behaviour:

defmodule MyPlugin do
  @behaviour Oban.Plugin

  use GenServer

  @impl Oban.Plugin
  def start_link(opts) do
    GenServer.start_link(__MODULE__, opts, name: opts[:name])
  end

  @impl Oban.Plugin
  def validate(opts) do
    if is_atom(opts[:mode])
      :ok
    else
      {:error, "expected opts to have a :mode key"}
    end
  end

  @impl GenServer
  def init(opts) do
    case validate(opts) do
      :ok -> {:ok, opts}
      {:error, reason} -> {:stop, reason}
    end
  end
end

Link to this section Summary

Callbacks

Starts a Plugin process linked to the current process.

Validate the structure, presence, or values of keyword options.

Link to this section Types

@type option() ::
  {:conf, Oban.Config.t()} | {:name, GenServer.name()} | {atom(), term()}

Link to this section Callbacks

@callback start_link([option()]) :: GenServer.on_start()

Starts a Plugin process linked to the current process.

Plugins are typically started as part of an Oban supervision tree and will receive the current configuration as :conf, along with a :name and any other provided options.

@callback validate([option()]) :: :ok | {:error, String.t()}

Validate the structure, presence, or values of keyword options.