aboutsummaryrefslogtreecommitdiff
path: root/src/ejabberd_redis_sup.erl
blob: 6906ef937d203dcccb6d3ba2ab8fea69b943e5ee (plain) (blame)
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
%%%-------------------------------------------------------------------
%%% Author  : Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% Created :  6 Apr 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2022   ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%-------------------------------------------------------------------
-module(ejabberd_redis_sup).

-behaviour(supervisor).

%% API
-export([start/0, stop/0, start_link/0]).
-export([get_pool_size/0, config_reloaded/0]).

%% Supervisor callbacks
-export([init/1]).

-include("logger.hrl").

%%%===================================================================
%%% API functions
%%%===================================================================
start() ->
    case is_started() of
	true -> ok;
	false ->
	    ejabberd:start_app(eredis),
	    Spec = {?MODULE, {?MODULE, start_link, []},
		    permanent, infinity, supervisor, [?MODULE]},
	    case supervisor:start_child(ejabberd_db_sup, Spec) of
		{ok, _} -> ok;
		{error, {already_started, Pid}} ->
                    %% Wait for the supervisor to fully start
                    _ = supervisor:count_children(Pid),
                    ok;
		{error, Why} = Err ->
		    ?ERROR_MSG("Failed to start ~ts: ~p", [?MODULE, Why]),
		    Err
	    end
    end.

stop() ->
    ejabberd_hooks:delete(config_reloaded, ?MODULE, config_reloaded, 20),
    _ = supervisor:terminate_child(ejabberd_db_sup, ?MODULE),
    _ = supervisor:delete_child(ejabberd_db_sup, ?MODULE),
    ok.

start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

config_reloaded() ->
    case is_started() of
	true ->
	    lists:foreach(
	      fun(Spec) ->
		      supervisor:start_child(?MODULE, Spec)
	      end, get_specs()),
	    PoolSize = get_pool_size(),
	    lists:foreach(
	      fun({Id, _, _, _}) when Id > PoolSize ->
		      case supervisor:terminate_child(?MODULE, Id) of
			  ok -> supervisor:delete_child(?MODULE, Id);
			  _ -> ok
		      end;
		 (_) ->
		      ok
	      end, supervisor:which_children(?MODULE));
	false ->
	    ok
    end.

%%%===================================================================
%%% Supervisor callbacks
%%%===================================================================
init([]) ->
    ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 20),
    {ok, {{one_for_one, 500, 1}, get_specs()}}.

%%%===================================================================
%%% Internal functions
%%%===================================================================
get_specs() ->
    lists:map(
      fun(I) ->
	      {I, {ejabberd_redis, start_link, [I]},
	       transient, 2000, worker, [?MODULE]}
      end, lists:seq(1, get_pool_size())).

get_pool_size() ->
    ejabberd_option:redis_pool_size() + 1.

is_started() ->
    whereis(?MODULE) /= undefined.