aboutsummaryrefslogtreecommitdiff
path: root/src/odbc/ejabberd_odbc_sup.erl
blob: d828449ecb00d01c0c9e7bf2e4623dde4427ce8b (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
%%%----------------------------------------------------------------------
%%% File    : ejabberd_odbc_sup.erl
%%% Author  : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : ODBC connections supervisor
%%% Created : 22 Dec 2004 by Alexey Shchepin <alexey@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2010   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., 59 Temple Place, Suite 330, Boston, MA
%%% 02111-1307 USA
%%%
%%%----------------------------------------------------------------------

-module(ejabberd_odbc_sup).
-author('alexey@process-one.net').

%% API
-export([start_link/1,
	 init/1,
	 get_pids/1,
	 get_random_pid/1
	]).

-include("ejabberd.hrl").

-define(DEFAULT_POOL_SIZE, 10).
-define(DEFAULT_ODBC_START_INTERVAL, 30). % 30 seconds

% time to wait for the supervisor to start its child before returning
% a timeout error to the request
-define(CONNECT_TIMEOUT, 500). % milliseconds


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

init([Host]) ->
    PoolSize = case ejabberd_config:get_local_option({odbc_pool_size, Host}) of
		   I when is_integer(I) ->
		       I;
	    undefined ->
		       ?DEFAULT_POOL_SIZE;
		   Other ->
		       ?ERROR_MSG("Wrong odbc_pool_size definition '~p' "
				  "for host ~p, default to ~p~n",
				  [Other, Host, ?DEFAULT_POOL_SIZE]),
		       ?DEFAULT_POOL_SIZE
	       end,
    StartInterval = case ejabberd_config:get_local_option({odbc_start_interval,
							   Host}) of
			Interval when is_integer(Interval) ->
			    Interval;
			undefined ->
			    ?DEFAULT_ODBC_START_INTERVAL;
			_Other2 ->
			    ?ERROR_MSG("Wrong odbc_start_interval "
				       "definition '~p' for host ~p, "
				       "defaulting to ~p~n",
				       [_Other2, Host,
					?DEFAULT_ODBC_START_INTERVAL]),
			    ?DEFAULT_ODBC_START_INTERVAL
		    end,
    {ok, {{one_for_one, PoolSize+1, StartInterval},
	  lists:map(
	    fun(I) ->
		    {I,
		     {ejabberd_odbc, start_link, [Host, StartInterval*1000]},
		     transient,
		     brutal_kill,
		     worker,
		     [?MODULE]}
	    end, lists:seq(1, PoolSize))}}.

get_pids(Host) ->
    Proc = gen_mod:get_module_proc(Host, ?MODULE),

    % throw an exception if supervisor is not ready (i.e. if it cannot
    % start its children, if the database is down for example)
    sys:get_status(Proc, ?CONNECT_TIMEOUT),

    [Child ||
	{_Id, Child, _Type, _Modules} <- supervisor:which_children(Proc),
	Child /= undefined].

get_random_pid(Host) ->
    Pids = get_pids(Host),
    lists:nth(erlang:phash(now(), length(Pids)), Pids).