shigoto_middleware (shigoto v1.9.10)

View Source

Composable middleware chain for job execution.

Middleware wraps job execution with before/after logic. Each middleware is a function that receives the job map and a Next function. Call Next() to continue the chain, or return early to short-circuit.

Writing Middleware

my_middleware(Job, Next) ->
    %% Before
    logger:info(\"starting job\", #{job_id => maps:get(id, Job)}),
    Result = Next(),
    %% After
    logger:info(\"finished job\", #{job_id => maps:get(id, Job)}),
    Result.

Middleware Order

Built-in middleware runs in this order:

  1. Logger metadata (sets structured log context)
  2. Seki resilience (rate limit, bulkhead, circuit breaker)
  3. Global middleware (from config)
  4. Worker middleware (from worker callback)
  5. Worker:perform/1

Summary

Functions

Build a composed function from a middleware list.

Run the full middleware chain for a job.

Types

middleware()

-type middleware() :: fun((map(), fun(() -> result())) -> result()).

result()

-type result() :: ok | {ok, term()} | {snooze, pos_integer()} | {error, term()}.

Functions

build_chain/3

-spec build_chain(map(), [middleware()], fun(() -> result())) -> fun(() -> result()).

Build a composed function from a middleware list.

run(Job, Worker, PerformFn)

-spec run(map(), module(), fun((map()) -> result())) -> result().

Run the full middleware chain for a job.