25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-24 17:29:28 +01:00
xmpp.chapril.org-ejabberd/src/ejabberd_redis_sup.erl

107 lines
3.3 KiB
Erlang
Raw Normal View History

2017-04-06 16:56:37 +02:00
%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% Created : 6 Apr 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%
%%%
2019-01-08 22:53:27 +01:00
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
2017-04-06 16:56:37 +02:00
%%%
%%% 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]).
2019-06-14 11:33:26 +02:00
-export([get_pool_size/0, config_reloaded/0]).
2017-04-06 16:56:37 +02:00
%% Supervisor callbacks
-export([init/1]).
-include("logger.hrl").
%%%===================================================================
%%% API functions
%%%===================================================================
2019-06-14 11:33:26 +02:00
start() ->
case is_started() of
true -> ok;
2017-04-06 16:56:37 +02:00
false ->
2019-06-14 11:33:26 +02:00
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, _}} -> ok;
{error, Why} = Err ->
?ERROR_MSG("Failed to start ~s: ~p", [?MODULE, Why]),
Err
end
2017-04-06 16:56:37 +02:00
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.
2019-06-14 11:33:26 +02:00
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
2017-04-06 16:56:37 +02:00
config_reloaded() ->
2019-06-14 11:33:26 +02:00
case is_started() of
2017-04-06 16:56:37 +02:00
true ->
lists:foreach(
fun(Spec) ->
supervisor:start_child(?MODULE, Spec)
end, get_specs()),
PoolSize = get_pool_size(),
lists:foreach(
fun({Id, _, _, _}) when Id > PoolSize ->
2019-06-14 11:33:26 +02:00
case supervisor:terminate_child(?MODULE, Id) of
ok -> supervisor:delete_child(?MODULE, Id);
_ -> ok
end;
2017-04-06 16:56:37 +02:00
(_) ->
ok
end, supervisor:which_children(?MODULE));
false ->
2019-06-14 11:33:26 +02:00
ok
2017-04-06 16:56:37 +02:00
end.
%%%===================================================================
%%% Supervisor callbacks
%%%===================================================================
init([]) ->
ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 20),
2019-06-14 11:33:26 +02:00
{ok, {{one_for_one, 500, 1}, get_specs()}}.
2017-04-06 16:56:37 +02:00
%%%===================================================================
%%% 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() ->
2019-06-14 11:33:26 +02:00
ejabberd_option:redis_pool_size() + 1.
2017-04-06 16:56:37 +02:00
2019-06-14 11:33:26 +02:00
is_started() ->
whereis(?MODULE) /= undefined.