emqx_machine.erl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_machine).
  17. -export([ start/0
  18. , graceful_shutdown/0
  19. , is_ready/0
  20. ]).
  21. -include_lib("emqx/include/logger.hrl").
  22. %% @doc EMQ X boot entrypoint.
  23. start() ->
  24. case os:type() of
  25. {win32, nt} -> ok;
  26. _nix ->
  27. os:set_signal(sighup, ignore),
  28. os:set_signal(sigterm, handle) %% default is handle
  29. end,
  30. ok = set_backtrace_depth(),
  31. start_sysmon(),
  32. ekka:start(),
  33. ok = print_otp_version_warning().
  34. graceful_shutdown() ->
  35. emqx_machine_terminator:graceful_wait().
  36. set_backtrace_depth() ->
  37. {ok, Depth} = application:get_env(emqx_machine, backtrace_depth),
  38. _ = erlang:system_flag(backtrace_depth, Depth),
  39. ok.
  40. %% @doc Return true if boot is complete.
  41. is_ready() ->
  42. emqx_machine_terminator:is_running().
  43. -if(?OTP_RELEASE > 22).
  44. print_otp_version_warning() -> ok.
  45. -else.
  46. print_otp_version_warning() ->
  47. ?ULOG("WARNING: Running on Erlang/OTP version ~p. Recommended: 23~n",
  48. [?OTP_RELEASE]).
  49. -endif. % OTP_RELEASE > 22
  50. start_sysmon() ->
  51. case application:get_env(system_monitor, db_hostname) of
  52. undefined ->
  53. %% If there is no sink for the events, there is no reason
  54. %% to run system_monitor_top, ignore it:
  55. ok;
  56. _ ->
  57. application:set_env(system_monitor, callback_mod, system_monitor_pg),
  58. _ = application:ensure_all_started(system_monitor, temporary),
  59. ok
  60. end.