Skip to content

Instantly share code, notes, and snippets.

@brendanhay
Created December 16, 2011 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendanhay/1486513 to your computer and use it in GitHub Desktop.
Save brendanhay/1486513 to your computer and use it in GitHub Desktop.
Auto-generate rabbit-shovel configuration for all queues
#!/usr/bin/env escript
-export([main/1]).
main([Src, Dest]) ->
Path = "./shovel.config",
Config = config(Src, Dest),
write(Config, Path),
io:format("wrote ~p shovels to ~s~n", [length(Config), Path]);
main(_Any) ->
io:format("Usage: ~s <source> <destination>~n", [escript:script_name()]),
halt(1).
config(Src, Dest) ->
Bindings = [re:split(B, "\t") || B <- bindings()],
Shovel = fun(Exchange, Queue) -> shovel(Src, Dest, Exchange, Queue) end,
split(Bindings, Shovel, []).
bindings() -> re:split(os:cmd("rabbitmqctl list_bindings"), "\n").
write(Config, Path) ->
Term = {rabbitmq_shovel, [{shovels, Config}]},
file:write_file(Path, io_lib:fwrite("~p.\n", [Term])).
split([], _Fun, Acc) ->
Acc;
split([[Exchange, <<"exchange">>, Queue, <<"queue">>, _, _]|T], Fun, Acc) ->
split(T, Fun, [Fun(Exchange, Queue)|Acc]);
split([_|T], Fun, Acc) ->
split(T, Fun, Acc).
shovel(Src, Dest, Exchange, Queue) ->
Declarations = [
{'queue.declare', [{queue, Queue}, durable]},
{'exchange.declare', [{exchange, Exchange}, {type, <<"direct">>}, durable]},
{'queue.bind', [{exchange, Exchange}, {queue, Queue}]}
],
{list_to_atom(binary_to_list(Queue)), [
{sources, [
{broker, Src},
{declarations, Declarations}
]},
{destinations, [
{broker, Dest},
{declarations, Declarations}
]},
{queue, Queue},
{prefetch_count, 10}
]}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment