Skip to content

Commit c4a0031

Browse files
committed
Support offline command execution
1 parent c6c4f45 commit c4a0031

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed

deps/rabbit/src/rabbit_cli_backend.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ run_command(ContextMap, Caller) when is_map(ContextMap) ->
2525
Context = map_to_context(ContextMap),
2626
run_command(Context, Caller);
2727
run_command(#rabbit_cli{} = Context, Caller) when is_pid(Caller) ->
28-
rabbit_cli_backend_sup:start_backend(Context, Caller).
28+
try
29+
rabbit_cli_backend_sup:start_backend(Context, Caller)
30+
catch
31+
exit:{noproc, _} ->
32+
start_link(Context, Caller)
33+
end.
2934

3035
map_to_context(ContextMap) ->
3136
Progname = maps:get(progname, ContextMap),

deps/rabbit/src/rabbit_cli_commands.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ cmd_noop(_) ->
369369
ok.
370370

371371
cmd_hello(Context) ->
372-
io:format("Regural prompt test; type Enter to submit~n"),
372+
io:format("Regular prompt test; type Enter to submit~n"),
373373
Name = io:get_line("Name: "),
374374
case Name of
375375
Name when is_list(Name) ->

deps/rabbit/src/rabbit_cli_frontend.erl

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,18 @@ connect_to_node(
176176
_ ->
177177
rabbit_cli_transport:connect()
178178
end,
179-
{ClientInfo, Priv1} = case Ret of
180-
{ok, Connection} ->
181-
{rabbit_cli_transport:get_client_info(
182-
Connection),
183-
Priv#?MODULE{connection = Connection}};
184-
{error, _Reason} ->
185-
{undefined,
186-
Priv#?MODULE{connection = none}}
187-
end,
179+
Priv1 = case Ret of
180+
{ok, Connection} ->
181+
Priv#?MODULE{connection = Connection};
182+
{error, Reason} ->
183+
?LOG_DEBUG(
184+
"CLI: failed to establish a connection to a RabbitMQ "
185+
"node: ~0p",
186+
[Reason]),
187+
Priv#?MODULE{connection = none}
188+
end,
189+
ClientInfo = rabbit_cli_transport:get_client_info(
190+
Priv1#?MODULE.connection),
188191
Context1 = Context#rabbit_cli{client = ClientInfo,
189192
priv = Priv1},
190193
run_command(Context1).
@@ -282,18 +285,31 @@ run_command(
282285
main_loop(Context1)
283286
end;
284287
run_command(#rabbit_cli{} = Context) ->
285-
%% TODO: If we can't connect to a node, try to parse args locally and run
286-
%% the command on this CLI node.
287288
%% FIXME: Load applications first, otherwise module attributes are
288289
%% unavailable.
289-
%% FIXME: run_command() relies on rabbit_cli_backend_sup.
290290
maybe
291291
process_flag(trap_exit, true),
292+
prepare_offline_exec(Context),
292293
ContextMap = context_to_map(Context),
293294
{ok, _Backend} ?= rabbit_cli_backend:run_command(ContextMap, self()),
294295
main_loop(Context)
295296
end.
296297

298+
prepare_offline_exec(_Context) ->
299+
?LOG_DEBUG("CLI: prepare for offline execution"),
300+
Env = rabbit_env:get_context(),
301+
rabbit_env:context_to_code_path(Env),
302+
rabbit_env:context_to_app_env_vars(Env),
303+
PluginsDir = rabbit_plugins:plugins_dir(),
304+
Plugins = rabbit_plugins:plugin_names(
305+
rabbit_plugins:list(PluginsDir, true)),
306+
Apps = [rabbit_common, rabbit | Plugins],
307+
lists:foreach(
308+
fun(App) -> _ = application:load(App) end,
309+
Apps),
310+
?LOG_DEBUG("CLI: ready for offline execution: ~p", [application:loaded_applications()]),
311+
ok.
312+
297313
context_to_map(Context) ->
298314
Fields = [Field || Field <- record_info(fields, rabbit_cli),
299315
%% We don't need or want to communicate anything that
@@ -311,7 +327,7 @@ record_to_map([], _Record, _Index, Map) ->
311327
main_loop(
312328
#rabbit_cli{priv = #?MODULE{connection = Connection,
313329
backend = Backend,
314-
pager = Pager} = Priv} = Context) ->
330+
pager = Pager} = Priv} = Context) ->
315331
?LOG_DEBUG("CLI: frontend main loop (pager: ~0p)...", [Pager]),
316332
Timeout = case is_port(Pager) of
317333
false ->

deps/rabbit/src/rabbit_cli_transport.erl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ complete_nodename(Nodename) ->
9090
get_client_info(#?MODULE{type = Proto}) ->
9191
{ok, Hostname} = inet:gethostname(),
9292
#{hostname => Hostname,
93-
proto => Proto}.
93+
proto => Proto};
94+
get_client_info(none) ->
95+
{ok, Hostname} = inet:gethostname(),
96+
#{hostname => Hostname,
97+
proto => none}.
9498

9599
run_command(#?MODULE{type = erldist, peer = Node}, ContextMap) ->
96100
Caller = self(),
@@ -101,9 +105,13 @@ run_command(#?MODULE{type = http, peer = Client}, ContextMap) ->
101105
gen_reply(#?MODULE{type = erldist}, From, Reply) ->
102106
gen:reply(From, Reply);
103107
gen_reply(#?MODULE{type = http, peer = Client}, From, Reply) ->
104-
rabbit_cli_http_client:gen_reply(Client, From, Reply).
108+
rabbit_cli_http_client:gen_reply(Client, From, Reply);
109+
gen_reply(none, From, Reply) ->
110+
gen:reply(From, Reply).
105111

106112
send(#?MODULE{type = erldist}, Dest, Msg) ->
107113
erlang:send(Dest, Msg);
108114
send(#?MODULE{type = http, peer = Client}, Dest, Msg) ->
109-
rabbit_cli_http_client:send(Client, Dest, Msg).
115+
rabbit_cli_http_client:send(Client, Dest, Msg);
116+
send(none, Dest, Msg) ->
117+
erlang:send(Dest, Msg).

0 commit comments

Comments
 (0)