refrakt/jobs

Background job processing using OTP processes.

Experimental. This module is lightly used and may change or move to its own package; see https://github.com/raskell-io/refrakt/issues/28.

Run expensive work outside the request cycle — emails, image processing, reports, webhooks.

Usage

import refrakt/jobs

// Start the job runner
let assert Ok(runner) = jobs.start(max_concurrency: 10)

// Enqueue a job
jobs.enqueue(runner, fn() {
  // Send welcome email, process image, etc.
  mailer.deliver(email, adapter)
})

// Enqueue with a name (for logging)
jobs.enqueue_named(runner, "send_welcome_email", fn() {
  mailer.deliver(email, adapter)
})

At most max_concurrency jobs run at once; further jobs wait in a queue. A job that crashes is logged and does not affect the runner.

Types

Messages the job runner handles.

pub opaque type JobMessage

A running job runner instance.

pub type JobRunner =
  process.Subject(JobMessage)

Values

pub fn enqueue(
  runner: process.Subject(JobMessage),
  work: fn() -> Nil,
) -> Nil

Enqueue a job for background execution.

pub fn enqueue_named(
  runner: process.Subject(JobMessage),
  name: String,
  work: fn() -> Nil,
) -> Nil

Enqueue a named job for background execution (name used in logs).

pub fn start(
  max_concurrency max: Int,
) -> Result(process.Subject(JobMessage), actor.StartError)

Start a job runner with a maximum concurrency.

✨ Search Document