PhxMediaLibrary.AsyncProcessor.Oban (PhxMediaLibrary v0.6.1)

Copy Markdown View Source

Oban-based async processor for reliable background processing.

Requires oban as a dependency and proper Oban configuration in your app. Unlike the default Task-based processor, Oban jobs are persisted to the database, survive application restarts, and support automatic retries with configurable backoff.

Setup

  1. Add :oban to your dependencies and configure it:

    # mix.exs
    {:oban, "~> 2.18"}
    
    # config/config.exs
    config :my_app, Oban,
      repo: MyApp.Repo,
      queues: [media: 10]
  2. Tell PhxMediaLibrary to use the Oban adapter:

    config :phx_media_library,
      async_processor: PhxMediaLibrary.AsyncProcessor.Oban

Queue Configuration

The worker uses the :media queue by default. Adjust concurrency to match your server's CPU/memory capacity:

# Low-traffic app
queues: [media: 5]

# High-traffic app with beefy servers
queues: [media: 20]

Retry Behaviour

The ProcessConversions worker is configured with max_attempts: 3. Failed jobs use Oban's default exponential backoff. You can monitor failed jobs via Oban's built-in dashboard or Oban.Web.

How It Works

When media is uploaded and conversions are defined, the processor enqueues an Oban job with the media ID, the conversion names, and the mediable_type so the worker can look up the originating model module and retrieve the full Conversion definitions (with dimensions, quality, fit mode, etc.).

This avoids the pitfall of serializing only conversion names and losing all configuration — the previous implementation created empty Conversion structs with only a :name field.

Summary

Functions

Process conversions synchronously, bypassing the Oban queue.

Functions

process_sync(media, conversions)

Process conversions synchronously, bypassing the Oban queue.

Useful for tests or situations where you need conversions to complete before continuing (e.g. generating a thumbnail before returning a response).

Examples

PhxMediaLibrary.AsyncProcessor.Oban.process_sync(media, conversions)