emqx_test_janitor.erl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2022-2024 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_test_janitor).
  17. -behaviour(gen_server).
  18. %% `gen_server' API
  19. -export([
  20. init/1,
  21. handle_call/3,
  22. handle_cast/2,
  23. handle_info/2,
  24. terminate/2
  25. ]).
  26. %% API
  27. -export([
  28. start_link/0,
  29. stop/1,
  30. stop/2,
  31. push_on_exit_callback/2
  32. ]).
  33. %%----------------------------------------------------------------------------------
  34. %% API
  35. %%----------------------------------------------------------------------------------
  36. start_link() ->
  37. gen_server:start_link(?MODULE, self(), []).
  38. stop(Server) ->
  39. stop(Server, 15_000).
  40. stop(Server, Timeout) ->
  41. gen_server:call(Server, terminate, Timeout).
  42. push_on_exit_callback(Server, Callback) when is_function(Callback, 0) ->
  43. gen_server:call(Server, {push, Callback}).
  44. %%----------------------------------------------------------------------------------
  45. %% `gen_server' API
  46. %%----------------------------------------------------------------------------------
  47. init(Parent) ->
  48. process_flag(trap_exit, true),
  49. {ok, #{callbacks => [], owner => Parent}}.
  50. terminate(_Reason, #{callbacks := Callbacks}) ->
  51. _ = do_terminate(Callbacks),
  52. ok.
  53. handle_call({push, Callback}, _From, State = #{callbacks := Callbacks}) ->
  54. {reply, ok, State#{callbacks := [Callback | Callbacks]}};
  55. handle_call(terminate, _From, State = #{callbacks := Callbacks}) ->
  56. FailedCallbacks = do_terminate(Callbacks),
  57. {stop, normal, ok, State#{callbacks := FailedCallbacks}};
  58. handle_call(_Req, _From, State) ->
  59. {reply, error, State}.
  60. handle_cast(_Req, State) ->
  61. {noreply, State}.
  62. handle_info({'EXIT', Parent, _Reason}, State = #{owner := Parent}) ->
  63. {stop, normal, State};
  64. handle_info(_Msg, State) ->
  65. {noreply, State}.
  66. %%----------------------------------------------------------------------------------
  67. %% Internal fns
  68. %%----------------------------------------------------------------------------------
  69. do_terminate(Callbacks) ->
  70. lists:foldl(
  71. fun(Fun, Failed) ->
  72. try
  73. Fun(),
  74. Failed
  75. catch
  76. K:E:S ->
  77. ct:pal("error executing callback ~p: ~p", [Fun, {K, E}]),
  78. ct:pal("stacktrace: ~p", [S]),
  79. [Fun | Failed]
  80. end
  81. end,
  82. [],
  83. Callbacks
  84. ).