View Source Kiq.Reporter behaviour (Kiq v0.7.3)

Job handling is performed by reporters, which are customized GenStage consumers.

This module specifies the reporter behaviour and provies an easy way to define new reporters via use Kiq.Reporter. The using macro defines all necessary GenStage functions and defines no-op handlers for all events.

Example Custom Reporter

Custom reporters may be defined within your application. One common use-case for custom reporters would be reporting exceptions to a tracking service. Here we will define a reporter for Honeybadger:

defmodule MyApp.Reporters.Honeybadger do
  @moduledoc false

  use Kiq.Reporter

  @impl Kiq.Reporter
  def handle_failure(job, error, stacktrace, state) do
    metadata = Map.take(job, [:jid, :class, :args, :queue, :retry_count])

    :ok = Honeybadger.notify(error, metadata, stacktrace)

    state
  end
end

Next you'll need to specify the reporter inside your application's Kiq module, using extra_reporters:

defmodule MyApp.Kiq do
  @moduledoc false

  alias MyApp.Reporters.Honeybadger

  use Kiq, queues: [default: 50], extra_reporters: [Honeybadger]
end

This guarantees that your reporter will be supervised by the reporter supervisor, and that it will be re-attached to the reporter producer upon restart if the reporter crashes.

Notes About State

Handler functions are always invoked with the current state. The return value of each handler function will be used as the updated state after all events are processed.

Summary

Callbacks

Emitted when job processing has been intentionally aborted.

Emitted when an exception ocurred while processing a job.

Emitted when a worker starts processing a job.

Emitted when the worker has completed the job, regardless of whether it succeded or failed.

Emitted when a worker has completed a job and no failures ocurred.

Types

@type options() :: [config: Kiq.Config.t(), name: identifier()]
@type state() :: any()

Callbacks

Link to this callback

handle_aborted(job, meta, state)

View Source
@callback handle_aborted(job :: Kiq.Job.t(), meta :: Keyword.t(), state :: state()) ::
  state()

Emitted when job processing has been intentionally aborted.

Link to this callback

handle_failure(job, error, stack, state)

View Source
@callback handle_failure(
  job :: Kiq.Job.t(),
  error :: Exception.t(),
  stack :: list(),
  state :: state()
) :: state()

Emitted when an exception ocurred while processing a job.

Link to this callback

handle_started(job, state)

View Source
@callback handle_started(job :: Kiq.Job.t(), state :: state()) :: state()

Emitted when a worker starts processing a job.

Link to this callback

handle_stopped(job, state)

View Source
@callback handle_stopped(job :: Kiq.Job.t(), state :: state()) :: state()

Emitted when the worker has completed the job, regardless of whether it succeded or failed.

Link to this callback

handle_success(job, meta, state)

View Source
@callback handle_success(job :: Kiq.Job.t(), meta :: Keyword.t(), state :: state()) ::
  state()

Emitted when a worker has completed a job and no failures ocurred.