1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2017 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%%============================================================================== |
16 |
|
|
17 |
|
-module(mod_global_distrib_worker_sup). |
18 |
|
-author('konrad.zemek@erlang-solutions.com'). |
19 |
|
|
20 |
|
-behaviour(supervisor). |
21 |
|
|
22 |
|
-export([start_link/0, get_worker/1, init/1]). |
23 |
|
|
24 |
|
-ignore_xref([start_link/0]). |
25 |
|
|
26 |
|
%%-------------------------------------------------------------------- |
27 |
|
%% API |
28 |
|
%%-------------------------------------------------------------------- |
29 |
|
|
30 |
|
-spec start_link() -> {ok, pid()} | {error, any()}. |
31 |
|
start_link() -> |
32 |
28 |
supervisor:start_link({local, ?MODULE}, ?MODULE, []). |
33 |
|
|
34 |
|
-spec get_worker(From :: jid:lserver()) -> atom(). |
35 |
|
get_worker(From) when is_binary(From) -> |
36 |
373 |
Name = mod_global_distrib_utils:any_binary_to_atom(From), |
37 |
373 |
case whereis(Name) of |
38 |
32 |
undefined -> supervisor:start_child(?MODULE, [Name]); |
39 |
341 |
_ -> ok |
40 |
|
end, |
41 |
373 |
Name. |
42 |
|
|
43 |
|
%%-------------------------------------------------------------------- |
44 |
|
%% supervisor API |
45 |
|
%%-------------------------------------------------------------------- |
46 |
|
|
47 |
|
init(_) -> |
48 |
28 |
Module = mod_global_distrib_worker, |
49 |
28 |
Child = {Module, {Module, start_link, []}, transient, 5000, worker, [Module]}, |
50 |
28 |
{ok, {{simple_one_for_one, 100, 1}, [Child]}}. |