feat(emqx_resource_instance): add wait_connected Opts

This commit is contained in:
EMQ-YangM 2022-02-23 18:37:34 +08:00
parent 5056e9fa55
commit bf57bf717c
1 changed files with 12 additions and 1 deletions

View File

@ -178,6 +178,16 @@ do_recreate(InstId, ResourceType, NewConfig, Opts) ->
{error, not_found} {error, not_found}
end. end.
wait_for_resource_ready(InstId, 0) ->
force_lookup(InstId);
wait_for_resource_ready(InstId, Retry) ->
case force_lookup(InstId) of
#{resource_data := #{status := connected}} = Data -> Data;
_ ->
timer:sleep(100),
wait_for_resource_ready(InstId, Retry-1)
end.
do_create(InstId, Group, ResourceType, Config, Opts) -> do_create(InstId, Group, ResourceType, Config, Opts) ->
case lookup(InstId) of case lookup(InstId) of
{ok,_, _} -> {ok,_, _} ->
@ -187,7 +197,8 @@ do_create(InstId, Group, ResourceType, Config, Opts) ->
ok -> ok ->
ok = emqx_plugin_libs_metrics:create_metrics(resource_metrics, InstId, ok = emqx_plugin_libs_metrics:create_metrics(resource_metrics, InstId,
[matched, success, failed, exception], [matched]), [matched, success, failed, exception], [matched]),
{ok, force_lookup(InstId)}; WaitTime = maps:get(wait_connected, Opts, 0),
{ok, wait_for_resource_ready(InstId, WaitTime div 100)};
Error -> Error ->
Error Error
end end