CargueroTaskBunny.Job behaviour (CargueroCargueroTaskBunny v0.0.7) View Source

Behaviour module for implementing a CargueroTaskBunny job.

CargueroTaskBunny job is an asynchronous background job whose execution request is enqueued to RabbitMQ and performed in a worker process.

defmodule HelloJob do
  use CargueroTaskBunny.Job

  def perform(%{"name" => name}) do
    IO.puts "Hello " <> name

    :ok
  end
end

HelloJob.enqueue(%{"name" => "Cloud"})

Failing

CargueroTaskBunny treats the job as failed when...

  • the return value of perform is not :ok or {:ok, something}
  • the perform timed out
  • the perform raises an exception while being executed
  • the perform throws :exit signal while being executed.

CargueroTaskBunny will retry the failed job later.

Timeout

By default CargueroTaskBunny terminates the job when it takes more than 2 minutes. This prevents messages blocking a worker.

If your job is expected to take longer than 2 minutes or you want to terminate the job earlier, override timeout/0.

defmodule SlowJob do
  use CargueroTaskBunny.Job

  def timeout, do: 300_000

  def perform(_) do
    slow_work()
    :ok
  end
end

Retry

By default CargueroTaskBunny retries 10 times every five minutes for a failed job. You can change this by overriding max_retry/0 and retry_interval/1.

For example, if you want the job to be retried five times and gradually increase the interval based on failed times, you can write logic like the following:

defmodule HttpSyncJob do
  def max_retry, do: 5

  def retry_interval(failed_count) do
    [1, 5, 10, 30, 60]
    |> Enum.map(&(&1 * 60_000))
    |> Enum.at(failed_count - 1, 1000)
  end

  ...
end

Link to this section Summary

Functions

Enqueues a job with payload.

Similar to enqueue/3 but raises an exception on error.

Callbacks

Callback for the max number of retries CargueroTaskBunny can make for a failed job.

Callback executed when a process gets rejected.

Callback to process a job.

Callback for the retry interval in milliseconds.

Callback for the timeout in milliseconds for a job execution.

Link to this section Functions

Link to this function

enqueue(job, payload, options \\ [])

View Source

Specs

enqueue(atom(), any(), keyword()) :: :ok | {:error, any()}

Enqueues a job with payload.

You might want to use the shorter version if you can access to the job.

# Following two calls are exactly same.
RegistrationJob.enqueue(payload)
CargueroTaskBunny.enqueue(RegistrationJob, payload)

Options

  • delay: Set time in milliseconds to schedule the job enqueue time.
  • host: RabbitMQ host. By default it is automatically selected from configuration.
  • queue: RabbitMQ queue. By default it is automatically selected from configuration.
Link to this function

enqueue!(job, payload, options \\ [])

View Source

Specs

enqueue!(atom(), any(), keyword()) :: :ok

Similar to enqueue/3 but raises an exception on error.

Link to this section Callbacks

Specs

max_retry() :: integer()

Callback for the max number of retries CargueroTaskBunny can make for a failed job.

Default value is 10. Override the function if you want to change the value.

Specs

on_reject(any()) :: :ok

Callback executed when a process gets rejected.

It receives in input the whole error trace structure plus the orginal payload for inspection and recovery actions.

Specs

perform(any()) ::
  :ok | {:ok, any()} | {:error, term()} | {:reject, any()} | :reject

Callback to process a job.

It can take any type of argument as long as it can be serialized with Jason, but we recommend you to use map with string keys for a consistency.

def perform(name) do
  IO.puts name <> ", it's not a preferred way"
end

def perform(%{"name" => name}) do
  IO.puts name <> ", it's a preferred way :)"
end

Specs

retry_interval(integer()) :: integer()

Callback for the retry interval in milliseconds.

Default value is 300_000 = 5 minutes. Override the function if you want to change the value.

CargueroTaskBunny will set failed count to the argument. The value will be more than or equal to 1 and less than or equal to max_retry.

Specs

timeout() :: integer()

Callback for the timeout in milliseconds for a job execution.

Default value is 120_000 = 2 minutes. Override the function if you want to change the value.