Packages
rambla
0.6.0
1.5.0
1.4.2
1.4.1
1.4.0
1.3.0
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
0.16.6
0.16.5
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.0
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.1
0.13.0
0.12.2
0.12.1
0.11.1
0.11.0
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
Easy publishing to many different targets. Supported back-ends: - Rabbit [Amqp](https://hexdocs.pm/amqp/) - Redis [Redix](https://hexdocs.pm/redix) - Http [:httpc](http://erlang.org/doc/man/httpc.html) - Smtp [:gen_smtp](https://hexdocs.pm/gen_smtp) - Slack [Envío](https://hexdocs.pm/envio)
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/rabbit_queue.ex
defmodule Mix.Tasks.Rambla.Rabbit.Queue do
@shortdoc "Operations with queues in RabbitMQ"
@moduledoc since: "0.6.0"
@moduledoc """
Mix task to deal with queues in the target RabbitMQ.
This is helpful to orchestrate target RabbitMQ when deploying
to docker. Allows to create, delete, purge and query status of
the queue.
Loads the setting from `config :rambla, :amqp` if no connection
is provided in parameters.
## Command line options
* -c - the connection string
* -o - the list of options without spaces, separated by comma
## Options
### Options for `create`
* `durable` - If set, keeps the Queue between restarts
of the broker. Defaults to false.
* `auto_delete` - If set, deletes the Queue once all
subscribers disconnect. Defaults to false.
* `exclusive` - If set, only one subscriber can consume
from the Queue. Defaults to false.
* `passive` - If set, raises an error unless the queue
already exists. Defaults to false.
* `no_wait` - If set, the declare operation is asynchronous.
Defaults to false.
* `arguments` - A list of arguments to pass when declaring
(of type AMQP.arguments/0). See the README for more information. Defaults to [].
### Options for `delete`
* `if_unused` - If set, the server will only delete the queue
if it has no consumers. If the queue has consumers, it’s
not deleted and an error is returned.
* `if_empty` - If set, the server will only delete the queue
if it has no messages.
* `no_wait` - If set, the delete operation is asynchronous.
"""
use Mix.Task
@switches [
connection: :string,
options: :string
]
@commands ~w|declare create delete purge status|
require Logger
@impl Mix.Task
@doc false
def run([command, name | args]) when command in @commands do
Logger.configure(level: :error)
Process.flag(:trap_exit, true)
{opts, _} =
OptionParser.parse!(args, aliases: [o: :options, c: :connection], strict: @switches)
connection =
case Keyword.get(opts, :connection, Application.get_env(:rambla, :amqp)) do
uri when is_binary(uri) ->
uri
params when is_list(params) ->
port = if params[:port], do: ":#{params[:port]}", else: ""
"amqp://#{params[:username]}:#{params[:password]}@#{params[:host]}#{port}"
nil ->
Mix.raise(
"The connection string must be either passed as -c option or set in `config :rambla, :amqp`"
)
end
with {:ok, conn} <- AMQP.Connection.open(connection),
{:ok, chan} = AMQP.Channel.open(conn),
opts =
opts
|> Keyword.get(:options, "")
|> String.replace(~r/:(?=\S)/, ": "),
{opts, _} <- Code.eval_string("[" <> opts <> "]"),
{:ok, result} <- do_command(chan, String.to_atom(command), name, opts) do
Mix.shell().info("Success. Results are: " <> inspect(result))
else
amqp_base_error ->
Mix.raise("Cannot execute command on target. Error:\n" <> inspect(amqp_base_error))
end
end
@doc false
def run(_),
do:
Mix.raise(
"Usage: mix rambla.rabbit.queue (" <> Enum.join(@commands, "|") <> ") name [opts]"
)
@spec do_command(
chan :: AMQP.Channel.t(),
command :: atom(),
name :: binary(),
opts :: keyword()
) :: {:ok, binary()} | {:error, any()}
defp do_command(chan, :create, name, opts),
do: do_command(chan, :declare, name, opts)
defp do_command(chan, command, name, opts) do
AMQP.Queue.__info__(:functions)
|> Keyword.get_values(command)
|> :lists.reverse()
|> case do
[3 | _] -> {:ok, apply(AMQP.Queue, command, [chan, name, opts])}
[2 | _] -> {:ok, apply(AMQP.Queue, command, [chan, name])}
_other -> {:error, {:unknown_command, command}}
end
end
end