2 %% Copyright (C) 2006 Damien Katz
4 %% This program is free software; you can redistribute it and/or
5 %% modify it under the terms of the GNU General Public License
6 %% as published by the Free Software Foundation; either version 2
7 %% of the License, or (at your option) any later version.
9 %% This program is distributed in the hope that it will be useful,
10 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
11 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 %% GNU General Public License for more details.
14 %% You should have received a copy of the GNU General Public License
15 %% along with this program; if not, write to the Free Software
16 %% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 -module(couch_server_sup
).
19 -behaviour(supervisor
).
21 -define(DEFAULT_INI
, "couch.ini").
23 -export([start_link
/1,stop
/0]).
25 %% supervisor callbacks
28 start_link(IniFilename
) ->
29 case whereis(couch_server_sup
) of
31 start_server(IniFilename
);
33 {error
, already_started
}
36 start_server(IniFilename
) ->
40 % no ini file specified, check the command line args
41 case init:get_argument(couchini
) of
43 file:read_file(Filename
);
48 % if IniFilename is specified, it is used
49 file:read_file(IniFilename
)
51 {ok
, Ini
} = couch_util:parse_ini(binary_to_list(IniBin
)),
52 ConsoleStartupMsg
= couch_util:ini_get_value(Ini
, "Couch", "ConsoleStartupMsg", "CouchDb server starting"),
53 ConsoleStartupCompleteMsg
= couch_util:ini_get_value(Ini
, "Couch", "ConsoleStartupCompleteMsg", "CouchDb server started"),
54 LogLevel
= list_to_atom(couch_util:ini_get_value(Ini
, "Couch", "LogLevel", "error")),
55 DbRootDir
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "DbRootDir", ".")),
56 HttpConfigFile
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "HttpConfigFile", "couch_httpd.conf")),
57 LogFile
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "LogFile", "couchdb.log")),
58 UtilDriverDir
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "UtilDriverDir", ".")),
59 UpdateNotifierExes
= [couch_util:abs_pathname(UpdateNotifierExe
) || {_
,UpdateNotifierExe
} <- couch_util:ini_get_values_starting(Ini
, "Couch", "DbUpdateNotificationProcess")],
61 UpdateNotifierExes
= [couch_util:abs_pathname(QueryExe
) || {{"Couch", "DbUpdateNotificationProcess"}, QueryExe
} <- Ini
],
63 QueryServers
= [{Lang
, couch_util:abs_pathname(QueryExe
)} || {{"Couch Query Servers", Lang
}, QueryExe
} <- Ini
],
65 FtSearchQueryServer0
= couch_util:ini_get_value(Ini
, "Couch", "FullTextSearchQueryServer", ""),
68 case FtSearchQueryServer0
of
70 _
-> couch_util:abs_pathname(FtSearchQueryServer0
)
74 io:format("couch ~s (LogLevel=~s)~n", [couch_server:get_version(), LogLevel
]),
76 io:format("~s~n", [ConsoleStartupMsg
]),
78 StartResult
= supervisor:start_link({local
, couch_server_sup
}, couch_server_sup
, {LogFile
, LogLevel
, DbRootDir
, QueryServers
, HttpConfigFile
, UtilDriverDir
, UpdateNotifierExes
, FtSearchQueryServer
}),
80 couch_log:debug("Startup Info:~n\tDbRootDir=~p~n" ++
81 "\tHttpConfigFile=~p~n" ++
83 "\tUtilDriverDir=~p~n" ++
84 "\tDbUpdateNotificationProcesses=~p~n" ++
85 "\tFullTextSearchQueryServer=~p~n" ++
93 [lists:flatten(io_lib:format("\t~s=~p~n", [Lang
, QueryExe
])) || {Lang
, QueryExe
} <- QueryServers
]]),
97 % only output when startup was successful
98 io:format("~s~n", [ConsoleStartupCompleteMsg
]);
104 catch exit(whereis(couch_server_sup
), normal
),
108 case file:read_file(?DEFAULT_INI
) of
112 {ok
, <<>>} % default ini missing, just return a blank
115 init({LogFile
, LogLevel
, DbServerRoot
, QueryServers
, HttpConfigFile
, UtilDriverDir
, UpdateNotifierExes
, FtSearchQueryServer
}) ->
116 {ok
, {{one_for_one
, 10, 3600},
118 {couch_log
, start_link
, [LogFile
, LogLevel
]},
124 {couch_server
, sup_start_link
, [DbServerRoot
]},
130 {couch_util
, start_link
, [UtilDriverDir
]},
135 {couch_query_servers
,
136 {couch_query_servers
, start_link
, [QueryServers
]},
140 [couch_query_servers
]},
142 {httpd
, start_link
, [HttpConfigFile
]},
147 {couch_db_update_event
,
148 {gen_event
, start_link
, [{local
, couch_db_update
}]},
154 lists:map(fun(UpdateNotifierExe
) ->
156 {couch_db_update_notifier
, start_link
, [UpdateNotifierExe
]},
160 [couch_db_update_notifier
]}
161 end, UpdateNotifierExes
)
163 case FtSearchQueryServer
of
168 {couch_ft_query
, start_link
, [FtSearchQueryServer
]},