From a64b29ff76f9278d053b1264cf6bc48fd91407b5 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Sun, 2 Jan 2022 20:32:24 +0800 Subject: [PATCH] fix(resource): re-create the helth checker if already exists --- .../src/emqx_resource_health_check.erl | 35 ++++++++++++++++++- .../src/emqx_resource_health_check_sup.erl | 32 +---------------- .../src/emqx_resource_instance.erl | 4 +-- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/apps/emqx_resource/src/emqx_resource_health_check.erl b/apps/emqx_resource/src/emqx_resource_health_check.erl index dc0795fb3..dfda683e1 100644 --- a/apps/emqx_resource/src/emqx_resource_health_check.erl +++ b/apps/emqx_resource/src/emqx_resource_health_check.erl @@ -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 diff --git a/apps/emqx_resource/src/emqx_resource_health_check_sup.erl b/apps/emqx_resource/src/emqx_resource_health_check_sup.erl index 6a2b07e94..e17186114 100644 --- a/apps/emqx_resource/src/emqx_resource_health_check_sup.erl +++ b/apps/emqx_resource/src/emqx_resource_health_check_sup.erl @@ -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. diff --git a/apps/emqx_resource/src/emqx_resource_instance.erl b/apps/emqx_resource/src/emqx_resource_instance.erl index 4ac72ca4d..a847e85f5 100644 --- a/apps/emqx_resource/src/emqx_resource_instance.erl +++ b/apps/emqx_resource/src/emqx_resource_instance.erl @@ -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.