emqx_modules_app.erl 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_modules_app).
  17. -behaviour(application).
  18. -export([
  19. start/2,
  20. stop/1
  21. ]).
  22. start(_Type, _Args) ->
  23. {ok, Sup} = emqx_modules_sup:start_link(),
  24. maybe_enable_modules(),
  25. {ok, Sup}.
  26. stop(_State) ->
  27. maybe_disable_modules(),
  28. ok.
  29. maybe_enable_modules() ->
  30. DelayedEnabled = emqx_conf:get([delayed, enable], true),
  31. RewriteEnabled = length(emqx_conf:get([rewrite], [])) > 0,
  32. RetainerEnabled = emqx_conf:get([retainer, enable], false),
  33. AutoSubscribeEnabled = length(emqx_conf:get([auto_subscribe, topics], [])) > 0,
  34. application:set_env(
  35. emqx_modules,
  36. advanced_mqtt_features_in_use,
  37. #{
  38. delayed => DelayedEnabled,
  39. topic_rewrite => RewriteEnabled,
  40. retained => RetainerEnabled,
  41. auto_subscribe => AutoSubscribeEnabled
  42. }
  43. ),
  44. DelayedEnabled andalso emqx_delayed:enable(),
  45. emqx_conf:get([telemetry, enable], true) andalso emqx_telemetry:enable(),
  46. emqx_conf:get([observer_cli, enable], true) andalso emqx_observer_cli:enable(),
  47. emqx_conf_cli:load(),
  48. ok = emqx_rewrite:enable(),
  49. emqx_topic_metrics:enable(),
  50. emqx_modules_conf:load().
  51. maybe_disable_modules() ->
  52. emqx_conf:get([delayed, enable], true) andalso emqx_delayed:disable(),
  53. emqx_conf:get([telemetry, enable], true) andalso emqx_telemetry:disable(),
  54. emqx_conf:get([observer_cli, enable], true) andalso emqx_observer_cli:disable(),
  55. emqx_rewrite:disable(),
  56. emqx_conf_cli:unload(),
  57. emqx_topic_metrics:disable(),
  58. emqx_modules_conf:unload().