Add Lucene Indexer Control
[couchdbimport.git] / CouchProjects / CouchDb / couch_server_sup.erl
blobb0aa63470ba2f624b0696a7d04d7d0a1b7af8c14
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(couch) of
42 {ok, Args} ->
43 case lists:keysearch("ini", 1, Args) of
44 {value, {"ini", Filename}} ->
45 file:read_file(Filename);
46 _Else ->
47 get_default_ini()
48 end;
49 _Else ->
50 get_default_ini()
51 end;
52 IniFilename ->
53 % if IniFilename is specified, it is used
54 file:read_file(IniFilename)
55 end,
56 {ok, Ini} = couch_util:parse_ini(binary_to_list(IniBin)),
57 ConsoleStartupMsg = list_to_atom(couch_util:ini_get_value(Ini, "Couch", "ConsoleStartupMsg", "CouchDb server starting")),
58 ConsoleStartupCompleteMsg = list_to_atom(couch_util:ini_get_value(Ini, "Couch", "ConsoleStartupCompleteMsg", "CouchDb server started")),
59 LogLevel = list_to_atom(couch_util:ini_get_value(Ini, "Couch", "LogLevel", "error")),
60 DbRootDir = couch_util:abs_pathname(couch_util:ini_get_value(Ini, "Couch", "DbRootDir", ".")),
61 HttpConfigFile = couch_util:abs_pathname(couch_util:ini_get_value(Ini, "Couch", "HttpConfigFile", "couch_httpd.conf")),
62 FabricServer = couch_util:abs_pathname(couch_util:ini_get_value(Ini, "Couch", "FabricServer", "FabricServer")),
63 LuceneServer = couch_util:ini_get_value(Ini, "Couch", "LuceneServer", ""),
64 LogFile = couch_util:abs_pathname(couch_util:ini_get_value(Ini, "Couch", "LogFile", "couchdb.log")),
65 UtilDriverDir = couch_util:abs_pathname(couch_util:ini_get_value(Ini, "Couch", "UtilDriverDir", ".")),
68 io:format("couch ~s (LogLevel=~s)~n", [couch_server:get_version(), LogLevel]),
70 io:format("~s~n", [ConsoleStartupMsg]),
72 % start the couch logger
73 ok = couch_log:start(LogFile, LogLevel),
75 couch_log:debug("Startup Info:~n\tDbRootDir=~p~n\tHttpConfigFile=~p~n\tFabricServer=~p~n\tLogFile=~p~n\tUtilDriverDir=~p",
76 [DbRootDir, HttpConfigFile, FabricServer, LogFile, UtilDriverDir]),
78 StartResult = supervisor:start_link({local, couch_server_sup}, couch_server_sup, {DbRootDir, FabricServer, HttpConfigFile, UtilDriverDir, LuceneServer}),
80 case StartResult of
81 {ok,_} ->
82 % only output when startup was successful
83 io:format("~s~n", [ConsoleStartupCompleteMsg]);
84 _ -> ok
85 end,
86 StartResult.
88 stop() ->
89 catch exit(whereis(couch_server_sup), normal),
90 couch_log:stop().
92 get_default_ini() ->
93 case file:read_file(?DEFAULT_INI) of
94 {ok, Ini} ->
95 {ok, Ini};
96 {error, enoent} ->
97 {ok, <<>>} % default ini missing, just return a blank
98 end.
100 init({DbServerRoot, FabricServer, HttpConfigFile, UtilDriverDir, LuceneServer}) ->
101 case string:equal(LuceneServer, "") of
102 true ->
103 {ok, {{one_for_one, 10, 3600},
104 [{couch_server,
105 {couch_server, sup_start_link, [DbServerRoot]},
106 permanent,
107 brutal_kill,
108 worker,
109 [couch_server]},
110 {couch_util,
111 {couch_util, start_link, [UtilDriverDir]},
112 permanent,
113 brutal_kill,
114 worker,
115 [couch_util]},
116 {couch_fabric,
117 {couch_fabric, start_link, [FabricServer]},
118 permanent,
119 brutal_kill,
120 worker,
121 [couch_fabric]},
122 {httpd,
123 {httpd, start_link, [HttpConfigFile]},
124 permanent,
125 1000,
126 supervisor,
127 [httpd]}
128 ]}};
130 _ ->
131 {ok, {{one_for_one, 10, 3600},
132 [{couch_server,
133 {couch_server, sup_start_link, [DbServerRoot]},
134 permanent,
135 brutal_kill,
136 worker,
137 [couch_server]},
138 {couch_util,
139 {couch_util, start_link, [UtilDriverDir]},
140 permanent,
141 brutal_kill,
142 worker,
143 [couch_util]},
144 {couch_fabric,
145 {couch_fabric, start_link, [FabricServer]},
146 permanent,
147 brutal_kill,
148 worker,
149 [couch_fabric]},
150 {couch_lucene,
151 {couch_lucene, start_link, [LuceneServer]},
152 permanent,
153 brutal_kill,
154 worker,
155 [couch_lucene]},
156 {httpd,
157 {httpd, start_link, [HttpConfigFile]},
158 permanent,
159 1000,
160 supervisor,
161 [httpd]}
163 end.