JoobQ.Middleware behaviour (joobq_ex v0.8.0)

Copy Markdown View Source

Code around every job's run, given to an instance as middleware: [MyApp.Timing, {MyApp.Tenant, key: "t"}] (JoobQ.Config): the first listed is the outermost, the worker's perform/1 the innermost call.

defmodule MyApp.Timing do
  @behaviour JoobQ.Middleware

  @impl JoobQ.Middleware
  def call(job, next, _opts) do
    started = System.monotonic_time()
    result = next.()
    MyApp.Metrics.timing(job.type, System.monotonic_time() - started)
    result
  end
end

call(job, next, opts) gets the job, next (a function of no argument that runs the rest of the chain and answers what it answered) and the options given with the module ([] without). What call/3 answers is the run's result, with the shapes perform/1 answers (:ok, {:ok, output}, {:error, reason}): it may run code around next, change what next answered, not call it at all, or rescue what it raises. What a middleware raises, exits or throws fails the job as a handler's error would.

The job a middleware is given as its argument has no lease token (lease_token: nil): through it a middleware cannot settle or renew the job, and JoobQ.Job.heartbeat/1 answers false for it. Options are left out of inspect/1 of the instance's configuration, but keep secrets out of them all the same.

Summary

Callbacks

call(t, function, term)

@callback call(JoobQ.Job.t(), function(), term()) :: term()