command line options
[couchdbimport.git] / src / CouchDb / couch_server_sup.erl
blobdec3acd2a410f8f2213503288c68534d757c0542
1 %% CouchDb
2 %% Copyright (C) 2006 Damien Katz
3 %%
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.
8 %%
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.
13 %%
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
26 -export([init/1]).
28 start_link(IniFilename) ->
29 case whereis(couch_server_sup) of
30 undefined ->
31 start_server(IniFilename);
32 _Else ->
33 {error, already_started}
34 end.
36 start_server(IniFilename) ->
37 {ok, IniBin} =
38 case IniFilename of
39 "" ->
40 % no ini file specified, check the command line args
41 case init:get_argument(ini) of
42 {ok, [Filename]} ->
43 file:read_file(Filename);
44 _Else ->
45 get_default_ini()
46 end;
47 IniFilename ->
48 % if IniFilename is specified, it is used
49 file:read_file(IniFilename)
50 end,
51 {ok, Ini} = couch_util:parse_ini(binary_to_list(IniBin)),
52 ConsoleStartupMsg = list_to_atom(couch_util:ini_get_value(Ini, "Couch", "ConsoleStartupMsg", "CouchDb server starting")),
53 ConsoleStartupCompleteMsg = list_to_atom(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", "text/" ++ Lang}, QueryExe} <- Ini],
65 FtSearchQueryServer0 = couch_util:ini_get_value(Ini, "Couch", "FullTextSearchQueryServer", ""),
67 FtSearchQueryServer =
68 case FtSearchQueryServer0 of
69 "" -> "";
70 _ -> couch_util:abs_pathname(FtSearchQueryServer0)
71 end,
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\tHttpConfigFile=~p~n\tLogFile=~p~n\tUtilDriverDir=~p~n\tDbUpdateNotificationProcesses=~p~n\tFullTextSearchQueryServer=~p~n" ++
81 [lists:flatten(io_lib:format("\ttext/~s=~p~n", [Lang, QueryExe])) || {Lang, QueryExe} <- QueryServers],
82 [DbRootDir, HttpConfigFile, LogFile, UtilDriverDir, UpdateNotifierExes, FtSearchQueryServer]),
84 case StartResult of
85 {ok,_} ->
86 % only output when startup was successful
87 io:format("~s~n", [ConsoleStartupCompleteMsg]);
88 _ -> ok
89 end,
90 StartResult.
92 stop() ->
93 catch exit(whereis(couch_server_sup), normal),
94 couch_log:stop().
96 get_default_ini() ->
97 case file:read_file(?DEFAULT_INI) of
98 {ok, Ini} ->
99 {ok, Ini};
100 {error, enoent} ->
101 {ok, <<>>} % default ini missing, just return a blank
102 end.
104 init({LogFile, LogLevel, DbServerRoot, QueryServers, HttpConfigFile, UtilDriverDir, UpdateNotifierExes, FtSearchQueryServer}) ->
105 {ok, {{one_for_one, 10, 3600},
106 [{couch_log,
107 {couch_log, start_link, [LogFile, LogLevel]},
108 permanent,
109 brutal_kill,
110 worker,
111 [couch_server]},
112 {couch_server,
113 {couch_server, sup_start_link, [DbServerRoot]},
114 permanent,
115 brutal_kill,
116 worker,
117 [couch_server]},
118 {couch_util,
119 {couch_util, start_link, [UtilDriverDir]},
120 permanent,
121 brutal_kill,
122 worker,
123 [couch_util]},
124 {couch_query_servers,
125 {couch_query_servers, start_link, [QueryServers]},
126 permanent,
127 brutal_kill,
128 worker,
129 [couch_query_servers]},
130 {httpd,
131 {httpd, start_link, [HttpConfigFile]},
132 permanent,
133 1000,
134 supervisor,
135 [httpd]},
136 {couch_db_update_event,
137 {gen_event, start_link, [{local, couch_db_update}]},
138 permanent,
139 1000,
140 supervisor,
141 dynamic}
142 ] ++
143 lists:map(fun(UpdateNotifierExe) ->
144 {UpdateNotifierExe,
145 {couch_db_update_notifier, start_link, [UpdateNotifierExe]},
146 permanent,
147 1000,
148 supervisor,
149 [couch_db_update_notifier]}
150 end, UpdateNotifierExes)
152 case FtSearchQueryServer of
153 "" ->
155 _ ->
156 [{couch_ft_query,
157 {couch_ft_query, start_link, [FtSearchQueryServer]},
158 permanent,
159 1000,
160 supervisor,
161 [httpd]}]