errol v0.1.0 Errol.Wiring View Source

This module allows to declare consumers for your rabbitmq routing keys using the following DSL.

After your Wiring module is started (using start_link/1 that will be generated at compile time), it will spin up a supervised process (Errol.Consumer.Server) for each consumer.

Example

defmodule MyWiring do
  use Wiring

  connection "amqp://guest:guest@localhost"

  @exchange "/users"
  @exchange_type :topic

  group :account do
    pipe_before(&Errol.Middleware.Json.parse/2)

    consume("users_account_created", "users.account.created", &UsersConsumer.account_created/1)
    consume("users_account_updated", "users.account.updated", &UsersConsumer.account_updated/1)
    consume("users_profile_updated", "users.profile.updated", &UsersConsumer.profile_updated/1)
  end

  group :photos do
    consume("users_profile_photo_uploaded", "users.profile.photo.uploaded", fn message -> ... end)
  end
end

Link to this section Summary

Functions

Sets AMQP connection configuration

Declares a consumer that will be spin up as a Errol.Consumer.Server process and supervised when your wiring module is started using start_link/1

Allows to group multiple consumer declarations. This can be useful to scope certain middlewares to a specific set of consumers

Sets a middleware function that will be run after the message is consumed successfuly

Sets a middleware function that will be run before the message is consumed

Sets a middleware function that will be run if either a before or after middleware or the consumer callback fails

Link to this section Functions

Link to this macro connection(config) View Source (macro)
connection(config :: String.t() | keyword()) :: no_return()

Sets AMQP connection configuration

When using a keyword list, the following options can be used:

  • :username - The name of a user registered with the broker (defaults to “guest”);
  • :password - The password of user (defaults to “guest”);
  • :virtual_host - The name of a virtual host in the broker (defaults to “/“);
  • :host - The hostname of the broker (defaults to “localhost”);
  • :port - The port the broker is listening on (defaults to 5672);
  • :channel_max - The channel_max handshake parameter (defaults to 0);
  • :frame_max - The frame_max handshake parameter (defaults to 0);
  • :heartbeat - The hearbeat interval in seconds (defaults to 10);
  • :connection_timeout - The connection timeout in milliseconds (defaults to 60000);
  • :ssl_options - Enable SSL by setting the location to cert files (defaults to none);
  • :client_properties - A list of extra client properties to be sent to the server, defaults to [];
  • :socket_options - Extra socket options. These are appended to the default options. See http://www.erlang.org/doc/man/inet.html#setopts-2 and http://www.erlang.org/doc/man/gen_tcp.html#connect-4 for descriptions of the available options.

To enable SSL, supply the following in the ssl_options field:

  • cacertfile - Specifies the certificates of the root Certificate Authorities that we wish to implicitly trust;
  • certfile - The client’s own certificate in PEM format;
  • keyfile - The client’s private key in PEM format;

Example

  # You can pass a url
  connection "amqp://guest:guest@localhost"

  # or a keyword list
  connection host: "rabbit", port: 5432
Link to this macro consume(queue, routing_key, callback) View Source (macro)
consume(
  queue :: String.t(),
  routing_key :: String.t(),
  callback :: (Errol.Message.t() -> Errol.Message.t())
) :: no_return()

Declares a consumer that will be spin up as a Errol.Consumer.Server process and supervised when your wiring module is started using start_link/1.

Messages will be requeued one time on failure.

Example

# you can pass a reference to a function
consume("users_profile_updated", "users.profile.updated", &UsersConsumer.profile_updated/1)

# or an anonymous function
consume("users_profile_photo_uploaded", "users.profile.photo.uploaded", fn message -> ... end)
Link to this macro group(name, list) View Source (macro)

Allows to group multiple consumer declarations. This can be useful to scope certain middlewares to a specific set of consumers.

Example

  group :account do
    # This middleware will only run for consumers on this group
    pipe_before(&Errol.Middleware.Json.parse/2)

    consume("users_account_created", "users.account.created", &UsersConsumer.account_created/1)
    consume("users_account_updated", "users.account.updated", &UsersConsumer.account_updated/1)
    consume("users_profile_updated", "users.profile.updated", &UsersConsumer.profile_updated/1)
  end

  group :photos do
    consume("users_profile_photo_uploaded", "users.profile.photo.uploaded", fn message -> ... end)
  end
Link to this macro pipe_after(callback) View Source (macro)
pipe_after(
  callback ::
    (Errol.Message.t(), queue :: String.t() ->
       {:ok, Errol.Message.t()} | {:error, reason :: any()})
) :: no_return()

Sets a middleware function that will be run after the message is consumed successfuly.

Example

pipe_after &Notifications.notify_success/2
Link to this macro pipe_before(callback) View Source (macro)
pipe_before(
  callback ::
    (Errol.Message.t(), queue :: String.t() ->
       {:ok, Errol.Message.t()} | {:error | :reject, reason :: any()})
) :: no_return()

Sets a middleware function that will be run before the message is consumed.

Example

pipe_before &Errol.Middleware.Json.parse/2
Link to this macro pipe_error(callback) View Source (macro)
pipe_error(
  callback ::
    (Errol.Message.t(), {queue :: String.t(), error :: any()} ->
       {:ok, Errol.Message.t()} | {:error, reason :: any()})
) :: no_return()

Sets a middleware function that will be run if either a before or after middleware or the consumer callback fails.

Example

pipe_error &MyErrorLogger.log/2