fix(resource): re-create the helth checker if already exists

This commit is contained in:
Shawn 2022-01-02 20:32:24 +08:00
parent e1ab331a30
commit a64b29ff76
3 changed files with 37 additions and 34 deletions

View File

@ -15,14 +15,47 @@
%%--------------------------------------------------------------------
-module(emqx_resource_health_check).
-export([start_link/2]).
-export([ start_link/2
, create_checker/2
, delete_checker/1
]).
-export([health_check/2]).
-define(SUP, emqx_resource_health_check_sup).
-define(ID(NAME), {resource_health_check, NAME}).
child_spec(Name, Sleep) ->
#{id => ?ID(Name),
start => {?MODULE, start_link, [Name, Sleep]},
restart => transient,
shutdown => 5000, type => worker, modules => [?MODULE]}.
start_link(Name, Sleep) ->
Pid = proc_lib:spawn_link(?MODULE, health_check, [Name, Sleep]),
{ok, Pid}.
create_checker(Name, Sleep) ->
case supervisor:start_child(?SUP, child_spec(Name, Sleep)) of
{ok, _} -> ok;
{error, already_present} -> ok;
{error, {already_started, _}} ->
ok = delete_checker(Name),
create_checker(Name, Sleep);
Error -> Error
end.
delete_checker(Name) ->
case supervisor:terminate_child(?SUP, {health_check, Name}) of
ok ->
case supervisor:delete_child(?SUP, {health_check, Name}) of
{error, not_found} -> ok;
Error -> Error
end;
{error, not_found} -> ok;
Error -> Error
end.
health_check(Name, SleepTime) ->
timer:sleep(SleepTime),
case emqx_resource:health_check(Name) of

View File

@ -19,18 +19,7 @@
-export([start_link/0]).
-export([init/1,
create_checker/2,
delete_checker/1]).
-define(HEALTH_CHECK_MOD, emqx_resource_health_check).
-define(ID(NAME), {resource_health_check, NAME}).
child_spec(Name, Sleep) ->
#{id => ?ID(Name),
start => {?HEALTH_CHECK_MOD, start_link, [Name, Sleep]},
restart => transient,
shutdown => 5000, type => worker, modules => [?HEALTH_CHECK_MOD]}.
-export([init/1]).
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
@ -38,22 +27,3 @@ start_link() ->
init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 10, period => 10},
{ok, {SupFlags, []}}.
create_checker(Name, Sleep) ->
case supervisor:start_child(?MODULE, child_spec(Name, Sleep)) of
{ok, _} -> ok;
{error, already_present} -> ok;
{error, {already_started, _}} -> ok;
Error -> Error
end.
delete_checker(Name) ->
case supervisor:terminate_child(?MODULE, {health_check, Name}) of
ok ->
case supervisor:delete_child(?MODULE, {health_check, Name}) of
{error, not_found} -> ok;
Error -> Error
end;
{error, not_found} -> ok;
Error -> Error
end.

View File

@ -238,7 +238,7 @@ start_and_check(InstId, ResourceType, Config, Opts, Data) ->
ets:insert(emqx_resource_instance, {InstId, Data2}),
case maps:get(async_create, Opts, false) of
false -> do_health_check(Data2);
true -> emqx_resource_health_check_sup:create_checker(InstId,
true -> emqx_resource_health_check:create_checker(InstId,
maps:get(health_check_interval, Opts, 15000))
end;
{error, Reason} ->
@ -252,7 +252,7 @@ do_stop(#{state := undefined}) ->
ok;
do_stop(#{id := InstId, mod := Mod, state := ResourceState} = Data) ->
_ = emqx_resource:call_stop(InstId, Mod, ResourceState),
ok = emqx_resource_health_check_sup:delete_checker(InstId),
ok = emqx_resource_health_check:delete_checker(InstId),
ets:insert(emqx_resource_instance, {InstId, Data#{status => stopped}}),
ok.