Mailglass.OptionalDeps.Oban (Mailglass v2.4.1)

Copy Markdown View Source

Gateway for the optional Oban dependency ({:oban, "~> 2.21"}).

available?/0 is dependency detection only: when Oban is present, it returns true and this gateway may safely reference Oban, Oban.Worker, and Oban.Job. It does not select an outbound adapter or authorize substitution. Selected :oban outbound work uses ready?/1 and insert/4 to fail-closed with typed errors when the dependency, configured instance, canonical queue, or transactional job insertion is unavailable.

Oban integration lands in (Outbound). This gateway is delivered in so Config/Telemetry can reference it without forward-reference pain.

addition — TenancyMiddleware

Mailglass.Oban.TenancyMiddleware (defined as a sibling module in this file, conditionally compiled when Oban.Worker is loaded) serializes Mailglass.Tenancy.current/0 into job args on enqueue and restores it via put_current/1 in perform/1. Mitigates process-dict-leakage risk across background boundaries. The module is absent when Oban is not loaded — mix compile --no-optional-deps --warnings-as-errors passes cleanly.

OSS Oban 2.21 has no first-class middleware behaviour (that lives in Oban Pro). Mailglass ships the middleware as a plain module exposing call/2 (the shape an adopter using Oban Pro can register directly) PLUS a wrap_perform/2 helper that adopters using OSS Oban invoke inside their worker's perform/1. Both paths converge on the same Mailglass.Tenancy.with_tenant/2 wrap.

Lint Enforcement

The Credo check NoBareOptionalDepReference flags direct Oban.* calls outside this module. All Oban interaction routes through the Outbound facade, which uses this gateway's canonical readiness and insertion seams.

Summary

Functions

Returns true when :oban is loaded in the current runtime for dependency detection. It is not a readiness result or an outbound fallback decision.

Gateway wrapper for Oban.insert/3 used from Ecto.Multi pipelines.

Gateway wrapper for the prefix-aware four-argument Oban Multi variant.

Gateway wrapper for Oban.insert_all/1.

Confirms canonical readiness: the configured default Oban instance can accept Mailglass's canonical worker queue. This is deliberately a producer-readiness check; successful insert/4 remains the transactional proof of job creation.

Functions

available?()

(since 0.1.0)
@spec available?() :: boolean()

Returns true when :oban is loaded in the current runtime for dependency detection. It is not a readiness result or an outbound fallback decision.

Backed by Code.ensure_loaded?/1, so purge-aware and safe to call from compile-time callbacks (e.g. Application.start/2).

insert(multi, name, job_builder)

(since 0.1.0)
@spec insert(Ecto.Multi.t(), atom(), (map() -> term())) :: Ecto.Multi.t()

Gateway wrapper for Oban.insert/3 used from Ecto.Multi pipelines.

Returns the original multi unchanged when Oban is not loaded.

insert(multi, name, job_builder, opts)

(since 2.4.1)
@spec insert(Ecto.Multi.t(), atom(), (map() -> term()), keyword()) :: Ecto.Multi.t()

Gateway wrapper for the prefix-aware four-argument Oban Multi variant.

The caller supplies the same step options used by Mailglass persistence so the Oban job is inserted in the configured schema inside the active Multi. If Oban is unavailable, this adds a failed transaction step rather than returning an unchanged Multi; selected durable work therefore fails closed.

insert_all(jobs)

(since 0.1.0)
@spec insert_all([term()]) :: term()

Gateway wrapper for Oban.insert_all/1.

ready?(canonical_queue)

@spec ready?(atom()) ::
  :ok
  | {:error,
     :dependency_unavailable
     | :instance_unavailable
     | :canonical_queue_unavailable}

Confirms canonical readiness: the configured default Oban instance can accept Mailglass's canonical worker queue. This is deliberately a producer-readiness check; successful insert/4 remains the transactional proof of job creation.