xref: /5.5.2/couchdb/src/couchdb/couch_app.erl (revision ba0c3c12)
1% Licensed under the Apache License, Version 2.0 (the "License"); you may not
2% use this file except in compliance with the License. You may obtain a copy of
3% the License at
4%
5%   http://www.apache.org/licenses/LICENSE-2.0
6%
7% Unless required by applicable law or agreed to in writing, software
8% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10% License for the specific language governing permissions and limitations under
11% the License.
12
13-module(couch_app).
14
15-behaviour(application).
16
17-include("couch_db.hrl").
18
19-export([start/2, stop/1]).
20
21start(_Type, DefaultIniFiles) ->
22    IniFiles = get_ini_files(DefaultIniFiles),
23    Apps = [
24        crypto, asn1, public_key, sasl, inets, oauth, ssl, lhttpc,
25        xmerl, compiler, syntax_tools, mochiweb, os_mon
26    ] ++ couch_apps(),
27
28    case start_apps(Apps) of
29    ok ->
30        couch_server_sup:start_link(IniFiles);
31    {error, Reason} ->
32        {error, Reason}
33    end.
34
35stop(_) ->
36    stop_apps(couch_apps()).
37
38couch_apps() ->
39    Apps0 = [couch_set_view, couch_index_merger, mapreduce],
40    case os:type() of
41    {win32, _} ->
42        Apps0;
43    _ ->
44        [couch_view_parser | Apps0]
45    end.
46
47get_ini_files(Default) ->
48    case init:get_argument(couch_ini) of
49    error ->
50        Default;
51    {ok, [[]]} ->
52        Default;
53    {ok, [Values]} ->
54        Values
55    end.
56
57start_apps([]) ->
58    ok;
59start_apps([App|Rest]) ->
60    case application:start(App) of
61    ok ->
62       start_apps(Rest);
63    {error, {already_started, App}} ->
64       start_apps(Rest);
65    {error, _Reason} when App =:= public_key; App =:= couch_index_merger ->
66       % ignore on R12B5
67       % don't crash the node if couch_query_logger fails to start
68       start_apps(Rest);
69    {error, Reason} ->
70       error_logger:error_msg("Could not start app ~p: ~p~n", [App, Reason]),
71       {error, {app_would_not_start, App}}
72    end.
73
74stop_apps(Apps) ->
75    do_stop_apps(lists:reverse(Apps)).
76
77do_stop_apps([]) ->
78    ok;
79do_stop_apps([App|Rest]) ->
80    case application:stop(App) of
81    ok ->
82       stop_apps(Rest);
83    {error, {not_running, App}} ->
84       stop_apps(Rest);
85    {error, Reason} ->
86       error_logger:error_msg("Could not stop app ~p: ~p~n", [App, Reason])
87    end.
88