PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/ucengine/src/core/ucengine_app.erl

http://github.com/AF83/ucengine
Erlang | 97 lines | 65 code | 14 blank | 18 comment | 0 complexity | e40fa2a8385ed0883d6afa995c176fe8 MD5 | raw file
  1. %%
  2. %% U.C.Engine - Unified Collaboration Engine
  3. %% Copyright (C) 2011 af83
  4. %%
  5. %% This program is free software: you can redistribute it and/or modify
  6. %% it under the terms of the GNU Affero General Public License as published by
  7. %% the Free Software Foundation, either version 3 of the License, or
  8. %% (at your option) any later version.
  9. %%
  10. %% This program is distributed in the hope that it will be useful,
  11. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. %% GNU Affero General Public License for more details.
  14. %%
  15. %% You should have received a copy of the GNU Affero General Public License
  16. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. %%
  18. -module(ucengine_app).
  19. -behaviour(application).
  20. -export([start/0]).
  21. %% application callback
  22. -export([start/2, stop/1]).
  23. -include("uce.hrl").
  24. start() ->
  25. application:start(ucengine).
  26. start(_, _) ->
  27. start_apps([sasl, crypto, metrics, gproc, ibrowse]),
  28. mnesia:create_schema([node()|nodes()]),
  29. application:start(mnesia, permanent),
  30. error_logger:tty(false),
  31. Arguments = init:get_arguments(),
  32. [[ConfigurationPath]] = utils:get(Arguments, [c], [["etc/uce.cfg"]]),
  33. ok = config:init(ConfigurationPath),
  34. case uce_sup:start_link() of
  35. {ok, Pid} ->
  36. setup(),
  37. {ok, Pid};
  38. Error ->
  39. {error, Error}
  40. end.
  41. start_apps([]) ->
  42. ok;
  43. start_apps([App|Apps]) ->
  44. case application:start(App) of
  45. ok ->
  46. ok;
  47. {error, {already_started, App}} ->
  48. ok;
  49. Error ->
  50. io:format("error ~p~n", [Error])
  51. end,
  52. start_apps(Apps).
  53. setup() ->
  54. save_pid(),
  55. setup_search(),
  56. setup_routes(),
  57. ok.
  58. stop(State) ->
  59. remove_pid(),
  60. State.
  61. setup_search() ->
  62. case config:get(search) of
  63. solr ->
  64. ChildSpec = {uce_solr_commiter,
  65. {uce_solr_commiter, start_link, []},
  66. permanent, brutal_kill, worker, [uce_solr_commiter]},
  67. {ok, _Pid} = uce_sup:start_child(ChildSpec);
  68. _ ->
  69. []
  70. end.
  71. setup_routes() ->
  72. routes:init().
  73. save_pid() ->
  74. Pid = os:getpid(),
  75. PidFile = config:get(pidfile),
  76. {ok, PidFileId} = file:open(PidFile, [read, write]),
  77. io:fwrite(PidFileId, "~s", [Pid]),
  78. file:close(PidFileId),
  79. ok.
  80. remove_pid() ->
  81. PidFile = config:get(pidfile),
  82. file:delete(PidFile),
  83. ok.