Skip to content

Commit 3043dc6

Browse files
Shovel: introduce operating modes
Sometimes you want a plugin to act as a library and not an application. That is, for its modules to be available at compile time and on a running node but, say, the actual runtime parameter handling and supervision of shovels to be handled by another plugin. Since we do not currently have a concept of "library plugins" or "library dependencies", this approach demonstrates one example of how some plugins can be used as libraries.
1 parent e4bc525 commit 3043dc6

File tree

6 files changed

+81
-8
lines changed

6 files changed

+81
-8
lines changed

deps/rabbitmq_shovel/include/rabbit_shovel.hrl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
66
%%
77

8+
-define(SHOVEL_APP, rabbitmq_shovel).
9+
810
-record(endpoint,
911
{uris,
1012
resource_declaration

deps/rabbitmq_shovel/priv/schema/rabbitmq_shovel.schema

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
%% RabbitMQ Shovel plugin
33
%% ----------------------------------------------------------------------------
44

5+
{mapping, "shovel.operating_mode", "rabbitmq_shovel.operating_mode", [
6+
[{datatype, {enum, [standard, alternative, library]}}]
7+
]}.
8+
59
{mapping, "shovel.topology.predeclared", "rabbitmq_shovel.topology.predeclared", [
610
[{datatype, {enum, [true, false]}}]
711
]}.

deps/rabbitmq_shovel/src/rabbit_shovel_dyn_worker_sup_sup.erl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ start_link() ->
2626
{ok, Pid0} -> Pid0;
2727
{error, {already_started, Pid0}} -> Pid0
2828
end,
29-
Shovels = rabbit_runtime_parameters:list_component(<<"shovel">>),
29+
IsStandard = rabbit_shovel_operating_mode:is_standard(),
30+
Shovels = case IsStandard of
31+
false ->
32+
%% when operating in a non-standard mode, do not start any shovels
33+
[];
34+
true ->
35+
rabbit_runtime_parameters:list_component(<<"shovel">>)
36+
end,
3037
[start_child({pget(vhost, Shovel), pget(name, Shovel)},
3138
pget(value, Shovel)) || Shovel <- Shovels],
3239
{ok, Pid}.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
6+
%%
7+
8+
-module(rabbit_shovel_operating_mode).
9+
10+
-include("rabbit_shovel.hrl").
11+
12+
-export([
13+
operating_mode/0,
14+
is_standard/0,
15+
is_alternative/0
16+
]).
17+
18+
-type operating_mode() :: 'standard' | atom().
19+
20+
%%
21+
%% API
22+
%%
23+
24+
-spec operating_mode() -> operating_mode().
25+
operating_mode() ->
26+
application:get_env(?SHOVEL_APP, operating_mode, standard).
27+
28+
-spec is_standard() -> boolean().
29+
is_standard() ->
30+
operating_mode() =:= standard.
31+
32+
-spec is_alternative() -> boolean().
33+
is_alternative() ->
34+
operating_mode() =/= standard.

deps/rabbitmq_shovel/src/rabbit_shovel_parameters.erl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,20 @@
3939
{enables, recovery}]}).
4040

4141
register() ->
42-
rabbit_registry:register(runtime_parameter, <<"shovel">>, ?MODULE).
42+
case rabbit_shovel_operating_mode:is_standard() of
43+
true ->
44+
rabbit_registry:register(runtime_parameter, <<"shovel">>, ?MODULE);
45+
false ->
46+
?LOG_DEBUG("Shovel: skipping runtime parameter registration, operating mode: ~ts", [rabbit_shovel_operating_mode:operating_mode()])
47+
end.
4348

4449
unregister() ->
45-
rabbit_registry:unregister(runtime_parameter, <<"shovel">>).
50+
case rabbit_shovel_operating_mode:is_standard() of
51+
true ->
52+
rabbit_registry:unregister(runtime_parameter, <<"shovel">>);
53+
false ->
54+
?LOG_DEBUG("Shovel: skipping runtime parameter deregistration, operating mode: ~ts", [rabbit_shovel_operating_mode:operating_mode()])
55+
end.
4656

4757
validate(_VHost, <<"shovel">>, Name, Def0, User) ->
4858
Def = rabbit_data_coercion:to_proplist(Def0),
@@ -65,10 +75,20 @@ pget2(K1, K2, Defs) -> case {pget(K1, Defs), pget(K2, Defs)} of
6575
end.
6676

6777
notify(VHost, <<"shovel">>, Name, Definition, _Username) ->
68-
rabbit_shovel_dyn_worker_sup_sup:adjust({VHost, Name}, Definition).
78+
case rabbit_shovel_operating_mode:is_standard() of
79+
true ->
80+
rabbit_shovel_dyn_worker_sup_sup:adjust({VHost, Name}, Definition);
81+
false ->
82+
?LOG_DEBUG("Shovel: ignoring a runtime parameter update, operating mode: ~ts", [rabbit_shovel_operating_mode:operating_mode()])
83+
end.
6984

7085
notify_clear(VHost, <<"shovel">>, Name, _Username) ->
71-
rabbit_shovel_dyn_worker_sup_sup:stop_child({VHost, Name}).
86+
case rabbit_shovel_operating_mode:is_standard() of
87+
true ->
88+
rabbit_shovel_dyn_worker_sup_sup:stop_child({VHost, Name});
89+
false ->
90+
?LOG_DEBUG("Shovel: ignoring a cleared runtime parameter, operating mode: ~ts", [rabbit_shovel_operating_mode:operating_mode()])
91+
end.
7292

7393
%%----------------------------------------------------------------------------
7494

deps/rabbitmq_shovel/src/rabbit_shovel_sup.erl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ start_link() ->
2121
end.
2222

2323
init([Configurations]) ->
24+
IsStandard = rabbit_shovel_operating_mode:is_standard(),
25+
StaticShovelSpecs = make_child_specs(IsStandard, Configurations),
2426
Len = dict:size(Configurations),
2527
ChildSpecs = [
2628
#{
@@ -39,11 +41,15 @@ init([Configurations]) ->
3941
type => supervisor,
4042
modules => [rabbit_shovel_dyn_worker_sup_sup]
4143
}
42-
| make_child_specs(Configurations)
44+
| StaticShovelSpecs
4345
],
44-
{ok, {#{strategy => one_for_one, intensity => 2 * Len, period => 2}, ChildSpecs}}.
46+
Opts = #{strategy => one_for_one, intensity => 2 * Len, period => 2},
47+
{ok, {Opts, ChildSpecs}}.
4548

46-
make_child_specs(Configurations) ->
49+
make_child_specs(false = _StandardOperatingMode, _Configurations) ->
50+
%% when operating in a non-standard mode, do not start any shovels
51+
[];
52+
make_child_specs(true, Configurations) ->
4753
dict:fold(
4854
fun (ShovelName, ShovelConfig, Acc) ->
4955
[

0 commit comments

Comments
 (0)