-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsup1a.erl
299 lines (279 loc) · 13.8 KB
/
gsup1a.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
% src/erlang/gsup1a.erl 2018-3-9 Alan U. Kennington.
% Investigating the Erlang/OTP gen_server concept with a supervisor.
%==============================================================================
% Supervisor module A.
% This module provides the basic user access functions to the Erlang shell.
-module(gsup1a).
% http://erlang.org/doc/reference_manual/modules.html#id78271
% http://erlang.org/doc/man/beam_lib.html#version-1
% Try beam_lib:version/1.
-vsn(1).
% The main start-up calls.
-export([start_link/0, start_link/1, unlink/0, stop/0, start_child/1]).
% Some more services for the Erlang shell.
-export([count_children/0, which_children/0, get_childspec/1,
terminate_child/1, restart_child/1, delete_child/1,
modInfo/0, modVersions/0]).
% Make the registered name of the server _different_ to the module name.
-define(SUPER_REG_NAME, gsup1reg). % The daemon process registration name.
-define(SUPER_SERVICE_MODULE, gsup1b). % Provides services to the daemon.
%==============================================================================
% Call chain:
% Erlang shell <==> SV-A <==> SV-module <- -> SV-daemon <==> SV-B <==> SV-C.
% However, SV-A also invokes functions in SV-C.
% Erlang shell: erl
% Module SV-A: gsup1a.erl
% SV-module: supervisor.erl
% SV-daemon: proc_lib.erl [the "gen_server" process]
% Module SV-B: gsup1b.erl [the "callback module"]
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% The supervisor:start_link/3 function starts a daemon process which
% has a receive-loop which waits for messages sent to it.
% See http://erlang.org/doc/man/supervisor.html
% The start_link functions are actually in proc_lib.erl.
% See http://erlang.org/doc/man/proc_lib.html#start-3
% The proc_lib.erl functions spawn processes using the spawn BIFs.
% http://erlang.org/doc/man/erlang.html#spawn-1
% http://erlang.org/doc/man/erlang.html#spawn_link-1
% http://erlang.org/doc/man/erlang.html#spawn_monitor-1
% http://erlang.org/doc/man/erlang.html#spawn_opt-2
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% The "SV-module <- -> SV-daemon" link uses inter-process messaging.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Test: [It worked the first time!!!]
% 1> gsup1a:start_link().
% {ok,<0.62.0>}
% 2> gs1a:alloc(gs1reg1).
% 1
% 3> i().
% [....]
%
% <0.62.0> supervisor:gsup1b/1 233 166 0
% gsup1reg gen_server:loop/7 10
% <0.63.0> gs1b:init/1 233 131 0
% gs1reg1 gen_server:loop/7 10
% [....]
% ok
% 4> gs1a:alloc(gs1reg1).
% 2
% 5> supervisor:which_children(gsup1reg).
% [{gs1id1,<0.63.0>,worker,[gs1b]}]
% 6> supervisor:count_children(gsup1reg).
% [{specs,1},{active,1},{supervisors,0},{workers,1}]
% 7> supervisor:restart_child(gsup1reg, gs1id1).
% {error,running}
% 8> supervisor:terminate_child(gsup1reg, gs1id1).
% gs1b:terminate/2 called for "shutdown" exit
% ok
% 9> supervisor:restart_child(gsup1reg, gs1id1).
% {ok,<0.72.0>}
% 10> gs1a:alloc(gs1reg1).
% 1
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Test with 10 children.
% 1> gsup1a:start_link(10).
% gsup1b:init/1: Nprocs = 10
% gsup1b:init/1: ChildSpecs = [#{type => worker,
% start => {gs1a,start_link,[gs1reg1]},
% shutdown => 5000,restart => permanent,
% modules => [gs1b],
% id => gs1id1},
% #{type => worker,
% start => {gs1a,start_link,[gs1reg2]},
% shutdown => 5000,restart => permanent,
% modules => [gs1b],
% id => gs1id2},
% [....]
% #{type => worker,
% start => {gs1a,start_link,[gs1reg10]},
% shutdown => 5000,restart => permanent,
% modules => [gs1b],
% id => gs1id10}]
% {ok,<0.62.0>}
% 2> i().
% [....]
%
% <0.62.0> supervisor:gsup1b/1 987 1142 0
% gsup1reg gen_server:loop/7 10
% <0.63.0> gs1b:init/1 233 119 0
% gs1reg1 gen_server:loop/7 10
% <0.64.0> gs1b:init/1 233 101 0
% gs1reg2 gen_server:loop/7 10
% <0.65.0> gs1b:init/1 233 101 0
% gs1reg3 gen_server:loop/7 10
% <0.66.0> gs1b:init/1 233 101 0
% gs1reg4 gen_server:loop/7 10
% <0.67.0> gs1b:init/1 233 101 0
% gs1reg5 gen_server:loop/7 10
% <0.68.0> gs1b:init/1 233 101 0
% gs1reg6 gen_server:loop/7 10
% <0.69.0> gs1b:init/1 233 101 0
% gs1reg7 gen_server:loop/7 10
% <0.70.0> gs1b:init/1 233 101 0
% gs1reg8 gen_server:loop/7 10
% <0.71.0> gs1b:init/1 233 101 0
% gs1reg9 gen_server:loop/7 10
% <0.72.0> gs1b:init/1 233 101 0
% gs1reg10 gen_server:loop/7 10
% [....]
% ok
% 3> supervisor:which_children(gsup1reg).
% [{gs1id10,<0.72.0>,worker,[gs1b]},
% {gs1id9,<0.71.0>,worker,[gs1b]},
% {gs1id8,<0.70.0>,worker,[gs1b]},
% {gs1id7,<0.69.0>,worker,[gs1b]},
% {gs1id6,<0.68.0>,worker,[gs1b]},
% {gs1id5,<0.67.0>,worker,[gs1b]},
% {gs1id4,<0.66.0>,worker,[gs1b]},
% {gs1id3,<0.65.0>,worker,[gs1b]},
% {gs1id2,<0.64.0>,worker,[gs1b]},
% {gs1id1,<0.63.0>,worker,[gs1b]}]
% 4> supervisor:count_children(gsup1reg).
% [{specs,10},{active,10},{supervisors,0},{workers,10}]
% 5> gs1a:alloc(gs1reg1).
% 1
% 6> gs1a:alloc(gs1reg1).
% 2
% 7> gs1a:alloc(gs1reg9).
% 1
% 8> gs1a:alloc(gs1reg1).
% 3
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
%==============================================================================
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% This function is called from the Erlang shell.
% So the name of this function doesn't matter.
% supervisor:start_link/3 registers the service module as ?SUPER_REG_NAME.
% See http://erlang.org/doc/man/supervisor.html#start_link-2
% Use start_link for supervised processes. Use start for standalone processes.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
start_link(Nprocs) when is_integer(Nprocs) andalso Nprocs >= 0 ->
SupName = {local, ?SUPER_REG_NAME},
Module = ?SUPER_SERVICE_MODULE,
Args = #{ nProcs => Nprocs }, % Passed to Module:init/1.
supervisor:start_link(SupName, Module, Args).
start_link() ->
start_link(1).
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Unlink the supervisor so that shell errors won't crash it.
% http://erlang.org/doc/man/erlang.html#whereis-1
% http://erlang.org/doc/man/erlang.html#unlink-1
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
unlink() ->
Pid = whereis(?SUPER_REG_NAME),
if is_pid(Pid) ->
unlink(Pid);
true -> ok
end.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% http://erlang.org/doc/man/proc_lib.html#stop-1
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
stop() ->
proc_lib:stop(?SUPER_REG_NAME).
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Start a single child process.
% http://erlang.org/doc/man/supervisor.html#start_child-2
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
start_child(Nproc) when is_integer(Nproc) andalso Nproc >= 1 ->
supervisor:start_child(?SUPER_REG_NAME, gsup1c:childSpec(Nproc)).
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Count the child processes.
% http://erlang.org/doc/man/supervisor.html#count_children-1
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
count_children() ->
supervisor:count_children(?SUPER_REG_NAME).
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Show info about child processes.
% http://erlang.org/doc/man/supervisor.html#which_children-1
% Quote:
% Returns a newly created list with information about all child specifications
% and child processes belonging to supervisor SupRef.
% Example:
% [{gs1id10,<0.81.0>,worker,[gs1b]},
% {gs1id9,<0.80.0>,worker,[gs1b]},
% {gs1id8,<0.79.0>,worker,[gs1b]},
% {gs1id7,<0.78.0>,worker,[gs1b]},
% {gs1id6,<0.77.0>,worker,[gs1b]},
% {gs1id5,<0.76.0>,worker,[gs1b]},
% {gs1id4,<0.75.0>,worker,[gs1b]},
% {gs1id3,<0.74.0>,worker,[gs1b]},
% {gs1id2,<0.73.0>,worker,[gs1b]},
% {gs1id1,<0.72.0>,worker,[gs1b]}]
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
which_children() ->
supervisor:which_children(?SUPER_REG_NAME).
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Get a child specification.
% http://erlang.org/doc/man/supervisor.html#get_childspec-2
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_childspec(Nproc) when is_integer(Nproc) andalso Nproc >= 1 ->
supervisor:get_childspec(?SUPER_REG_NAME, gsup1c:childId(Nproc));
get_childspec(ChildId) ->
supervisor:get_childspec(?SUPER_REG_NAME, ChildId).
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Terminate a single child process.
% http://erlang.org/doc/man/supervisor.html#terminate_child-2
% http://erlang.org/doc/man/supervisor.html#type-child_id
% Quote:
% If the supervisor is not "simple_one_for_one", "Id" must be the child
% specification identifier. The process, if any, is terminated and, unless
% it is a temporary child, the child specification is kept by the
% supervisor. The child process can later be restarted by the supervisor.
% The child process can also be restarted explicitly by calling
% "restart_child/2". Use "delete_child/2" to remove the child specification.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
terminate_child(Nproc) when is_integer(Nproc) andalso Nproc >= 1 ->
supervisor:terminate_child(?SUPER_REG_NAME, gsup1c:childId(Nproc));
terminate_child(ChildId) ->
supervisor:terminate_child(?SUPER_REG_NAME, ChildId).
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Restart a single child process.
% http://erlang.org/doc/man/supervisor.html#restart_child-2
% http://erlang.org/doc/man/supervisor.html#type-child_id
% Quote:
% Tells supervisor "SupRef" to restart a child process corresponding to the
% child specification identified by "Id". The child specification must
% exist, and the corresponding child process must not be running.
%
% If the child process start function returns "{ok,Child}" or
% "{ok,Child,Info}", the pid is added to the supervisor and the function
% returns the same value.
%
% If the child process start function returns "ignore", the pid remains set
% to "undefined" and the function returns "{ok,undefined}".
%
% If the child process start function returns an error tuple or an
% erroneous value, or if it fails, the function returns "{error,Error}",
% where "Error" is a term containing information about the error.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
restart_child(Nproc) when is_integer(Nproc) andalso Nproc >= 1 ->
supervisor:restart_child(?SUPER_REG_NAME, gsup1c:childId(Nproc));
restart_child(ChildId) ->
supervisor:restart_child(?SUPER_REG_NAME, ChildId).
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Delete a single child process.
% http://erlang.org/doc/man/supervisor.html#delete_child-2
% http://erlang.org/doc/man/supervisor.html#type-child_id
% This only works if the child process is not running.
% So you must call terminate_child/1 first.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
delete_child(Nproc) when is_integer(Nproc) andalso Nproc >= 1 ->
supervisor:delete_child(?SUPER_REG_NAME, gsup1c:childId(Nproc));
delete_child(ChildId) ->
supervisor:delete_child(?SUPER_REG_NAME, ChildId).
modInfo() ->
io:format("----------- gsup1a: -----------~n~p~n", [gsup1a:module_info()]),
io:format("----------- gsup1b: -----------~n~p~n", [gsup1b:module_info()]),
io:format("----------- gsup1c: -----------~n~p~n", [gsup1c:module_info()]),
io:format("----------- gs1a: -----------~n~p~n", [gs1a:module_info()]),
io:format("----------- gs1b: -----------~n~p~n", [gs1b:module_info()]),
io:format("----------- gs1c: -----------~n~p~n", [gs1c:module_info()]).
modVersions() ->
io:format("gsup1a: version ~p~n", [beam_lib:version(gsup1a)]),
io:format("gsup1b: version ~p~n", [beam_lib:version(gsup1b)]),
io:format("gsup1c: version ~p~n", [beam_lib:version(gsup1c)]),
io:format("gs1a: version ~p~n", [beam_lib:version(gs1a)]),
io:format("gs1b: version ~p~n", [beam_lib:version(gs1b)]),
io:format("gs1c: version ~p~n", [beam_lib:version(gs1c)]).