View Source OddJob.Pool (OddJob v0.5.1)

The OddJob.Pool process

The OddJob.Pool is the supervisor at the top of a pool supervision tree, and is responsible for starting and supervising the OddJob.Pool.Supervisor, OddJob.Queue, OddJob.Async.ProxySupervisor, and OddJob.Scheduler.Supervisor. When you add a OddJob.child_spec/1 to your list of supervised children, or call OddJob.start_link/1, you are adding an OddJob.Pool to your application.

Public functions

This module defines the public functions start_link/1 and child_spec/1. It is recommended that you call these functions from the OddJob module namespace instead. See OddJob for documentation and usage.

Module-based pools

You may want to configure your pool at runtime, or wrap your logic in a custom API. Module-based pools are great for this. Invoking use OddJob.Pool defines a child_spec/1 function that can be used to start your pool under a supervisor.

Imagine you want to start a job pool with a dynamically configurable pool size and wrap it in a client API:

defmodule MyApp.Email do
  use OddJob.Pool

  def start_link(init_arg) do
    OddJob.start_link(name: __MODULE__, pool_size: init_arg)
  end

  # Client API

  def send_email(user) do
    OddJob.perform(__MODULE__, fn -> MyApp.Mailer.send(user) end)
  end
end

Now you can supervise your pool and set the pool size in a child spec tuple:

children = [
  {MyApp.Email, 20}
]

Supervisor.start_link(children, strategy: :one_for_one)

You can also skip the initial argument by passing MyApp.Email on its own:

# in my_app/application.ex

children = [
  MyApp.Email # Same as {MyApp.Email, []}
]

Supervisor.start_link(children, strategy: :one_for_one)

# in my_app/email.ex

defmodule MyApp.Email do
  use OddJob.Pool

  def start_link(_init_arg) do
    OddJob.start_link(name: __MODULE__)
  end
end

For convenience, use OddJob.Pool automatically defines an overridable start_link/1 function just like the one above, that ignores the initial argument and names the pool after the module, using the default configuration options. This means the above example is equivalent to:

defmodule MyApp.Email do
  use OddJob.Pool
end

You can pass any supervision start options to use OddJob.Pool:

use OddJob.Pool, restart: :transient, shutdown: :brutal_kill

The default options are the same as any Supervisor. See the Supervisor module for more info on supervision start options.

Link to this section Summary

Link to this section Types

Specs

child_spec() :: Supervisor.child_spec()

Specs

name() :: atom()

Specs

options() :: [{:name, name()} | start_option()]

Specs

start_option() ::
  {:pool_size, non_neg_integer()}
  | {:max_restarts, non_neg_integer()}
  | {:max_seconds, non_neg_integer()}