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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.
-module(couch_server_sup).
-behaviour(supervisor).
-define(DEFAULT_INI, "couch.ini").
-export([start_link/1,stop/0]).
-include("couch_db.hrl").
%% supervisor callbacks
-export([init/1]).
start_link(IniFilename) ->
case whereis(couch_server_sup) of
undefined ->
start_server(IniFilename);
_Else ->
{error, already_started}
end.
start_server("") ->
% no ini file specified, check the command line args
IniFile =
case init:get_argument(couchini) of
{ok, [CmdLineIniFilename]} ->
CmdLineIniFilename;
_Else ->
?DEFAULT_INI
end,
start_server(IniFile);
start_server(InputIniFilename) ->
case init:get_argument(pidfile) of
{ok, [PidFile]} ->
case file:write_file(PidFile, os:getpid()) of
ok -> ok;
Error -> io:format("Failed to write PID file ~s, error: ~p", [PidFile, Error])
end;
_ -> ok
end,
{ok, Cwd} = file:get_cwd(),
IniFilename = couch_util:abs_pathname(InputIniFilename),
IniBin =
case file:read_file(IniFilename) of
{ok, IniBin0} ->
IniBin0;
{error, enoent} ->
Msg = io_lib:format("Couldn't find server configuration file ~s.", [InputIniFilename]),
io:format("~s~n", [Msg]),
throw({startup_error, Msg})
end,
{ok, Ini} = couch_util:parse_ini(binary_to_list(IniBin)),
ConsoleStartupMsg = proplists:get_value({"Couch", "ConsoleStartupMsg"}, Ini, "Apache CouchDB is starting."),
LogLevel = list_to_atom(proplists:get_value({"Couch", "LogLevel"}, Ini, "error")),
DbRootDir = proplists:get_value({"Couch", "DbRootDir"}, Ini, "."),
BindAddress = proplists:get_value({"Couch", "BindAddress"}, Ini, any),
Port = proplists:get_value({"Couch", "Port"}, Ini, 5984),
DocumentRoot = proplists:get_value({"Couch", "DocumentRoot"}, Ini, "share/www"),
LogFile = proplists:get_value({"Couch", "LogFile"}, Ini, "couchdb.log"),
UtilDriverDir = proplists:get_value({"Couch", "UtilDriverDir"}, Ini, ""),
UpdateNotifierExes = proplists:get_all_values({"Couch", "DbUpdateNotificationProcess"}, Ini),
FtSearchQueryServer = proplists:get_value({"Couch", "FullTextSearchQueryServer"}, Ini, ""),
RemoteRestart = list_to_atom(proplists:get_value({"Couch", "AllowRemoteRestart"}, Ini, "undefined")),
ServerOptions = [{remote_restart, RemoteRestart}],
QueryServers = [{Lang, QueryExe} || {{"Couch Query Servers", Lang}, QueryExe} <- Ini],
ChildProcesses =
[{couch_log,
{couch_log, start_link, [LogFile, LogLevel]},
permanent,
brutal_kill,
worker,
[couch_server]},
{couch_db_update_event,
{gen_event, start_link, [{local, couch_db_update}]},
permanent,
1000,
supervisor,
dynamic},
{couch_server,
{couch_server, sup_start_link, [DbRootDir, ServerOptions]},
permanent,
brutal_kill,
worker,
[couch_server]},
{couch_query_servers,
{couch_query_servers, start_link, [QueryServers]},
permanent,
brutal_kill,
worker,
[couch_query_servers]},
{couch_view,
{couch_view, start_link, [DbRootDir]},
permanent,
brutal_kill,
worker,
[couch_view]},
{couch_httpd,
{couch_httpd, start_link, [BindAddress, Port, DocumentRoot]},
permanent,
1000,
supervisor,
[couch_httpd]}
] ++
lists:map(fun(UpdateNotifierExe) ->
{UpdateNotifierExe,
{couch_db_update_notifier, start_link, [UpdateNotifierExe]},
permanent,
1000,
supervisor,
[couch_db_update_notifier]}
end, UpdateNotifierExes)
++
case FtSearchQueryServer of
"" ->
[];
_ ->
[{couch_ft_query,
{couch_ft_query, start_link, [FtSearchQueryServer]},
permanent,
1000,
supervisor,
[couch_ft_query]}]
end,
io:format("couch ~s (LogLevel=~s)~n", [couch_server:get_version(), LogLevel]),
io:format("~s~n", [ConsoleStartupMsg]),
couch_util:start_driver(UtilDriverDir),
% ensure these applications are running
application:start(inets),
application:start(crypto),
process_flag(trap_exit, true),
StartResult = (catch supervisor:start_link(
{local, couch_server_sup}, couch_server_sup, ChildProcesses)),
ConfigInfo = io_lib:format("Config Info ~s:~n\tCurrentWorkingDir=~s~n" ++
"\tDbRootDir=~s~n" ++
"\tBindAddress=~p~n" ++
"\tPort=~p~n" ++
"\tDocumentRoot=~s~n" ++
"\tLogFile=~s~n" ++
"\tUtilDriverDir=~s~n" ++
"\tDbUpdateNotificationProcesses=~s~n" ++
"\tFullTextSearchQueryServer=~s~n" ++
"~s",
[IniFilename,
Cwd,
DbRootDir,
BindAddress,
Port,
DocumentRoot,
LogFile,
UtilDriverDir,
UpdateNotifierExes,
FtSearchQueryServer,
[lists:flatten(io_lib:format("\t~s=~s~n", [Lang, QueryExe])) || {Lang, QueryExe} <- QueryServers]]),
?LOG_DEBUG("~s", [ConfigInfo]),
case StartResult of
{ok,_} ->
% only output when startup was successful
io:format("Apache CouchDB has started. Time to relax.~n");
_ ->
% Since we failed startup, unconditionally dump configuration data to console
io:format("~s", [ConfigInfo]),
ok
end,
process_flag(trap_exit, false),
StartResult.
stop() ->
catch exit(whereis(couch_server_sup), normal),
couch_log:stop().
init(ChildProcesses) ->
{ok, {{one_for_one, 10, 3600}, ChildProcesses}}.
|