diff --git a/apps/emqx_connector/test/emqx_connector_mongo_SUITE.erl b/apps/emqx_connector/test/emqx_connector_mongo_SUITE.erl index 294424638..d76b8420a 100644 --- a/apps/emqx_connector/test/emqx_connector_mongo_SUITE.erl +++ b/apps/emqx_connector/test/emqx_connector_mongo_SUITE.erl @@ -83,7 +83,7 @@ perform_lifecycle_check(PoolName, InitialConfig) -> status := InitialStatus }} = emqx_resource:get_instance(PoolName), - ?assertEqual(ok, emqx_resource:health_check(PoolName)), + ?assertEqual({ok, connected}, emqx_resource:health_check(PoolName)), % % Perform query as further check that the resource is working as expected ?assertMatch([], emqx_resource:query(PoolName, test_query_find())), ?assertMatch(undefined, emqx_resource:query(PoolName, test_query_find_one())), @@ -96,7 +96,7 @@ perform_lifecycle_check(PoolName, InitialConfig) -> }} = emqx_resource:get_instance(PoolName), ?assertEqual(StoppedStatus, disconnected), - ?assertEqual({error, stopped}, emqx_resource:health_check(PoolName)), + ?assertEqual({error, resource_is_stopped}, emqx_resource:health_check(PoolName)), % Resource healthcheck shortcuts things by checking ets. Go deeper by checking pool itself. ?assertEqual({error, not_found}, ecpool:stop_sup_pool(ReturnedPoolName)), % Can call stop/1 again on an already stopped instance @@ -107,7 +107,7 @@ perform_lifecycle_check(PoolName, InitialConfig) -> timer:sleep(500), {ok, ?CONNECTOR_RESOURCE_GROUP, #{status := InitialStatus}} = emqx_resource:get_instance(PoolName), - ?assertEqual(ok, emqx_resource:health_check(PoolName)), + ?assertEqual({ok, connected}, emqx_resource:health_check(PoolName)), ?assertMatch([], emqx_resource:query(PoolName, test_query_find())), ?assertMatch(undefined, emqx_resource:query(PoolName, test_query_find_one())), % Stop and remove the resource in one go. diff --git a/apps/emqx_connector/test/emqx_connector_mysql_SUITE.erl b/apps/emqx_connector/test/emqx_connector_mysql_SUITE.erl index 0631c7ccd..d7f5cec63 100644 --- a/apps/emqx_connector/test/emqx_connector_mysql_SUITE.erl +++ b/apps/emqx_connector/test/emqx_connector_mysql_SUITE.erl @@ -82,7 +82,7 @@ perform_lifecycle_check(PoolName, InitialConfig) -> status := InitialStatus }} = emqx_resource:get_instance(PoolName), - ?assertEqual(ok, emqx_resource:health_check(PoolName)), + ?assertEqual({ok, connected}, emqx_resource:health_check(PoolName)), % % Perform query as further check that the resource is working as expected ?assertMatch({ok, _, [[1]]}, emqx_resource:query(PoolName, test_query_no_params())), ?assertMatch({ok, _, [[1]]}, emqx_resource:query(PoolName, test_query_with_params())), @@ -102,7 +102,7 @@ perform_lifecycle_check(PoolName, InitialConfig) -> }} = emqx_resource:get_instance(PoolName), ?assertEqual(StoppedStatus, disconnected), - ?assertEqual({error, stopped}, emqx_resource:health_check(PoolName)), + ?assertEqual({error, resource_is_stopped}, emqx_resource:health_check(PoolName)), % Resource healthcheck shortcuts things by checking ets. Go deeper by checking pool itself. ?assertEqual({error, not_found}, ecpool:stop_sup_pool(ReturnedPoolName)), % Can call stop/1 again on an already stopped instance @@ -113,7 +113,7 @@ perform_lifecycle_check(PoolName, InitialConfig) -> timer:sleep(500), {ok, ?CONNECTOR_RESOURCE_GROUP, #{status := InitialStatus}} = emqx_resource:get_instance(PoolName), - ?assertEqual(ok, emqx_resource:health_check(PoolName)), + ?assertEqual({ok, connected}, emqx_resource:health_check(PoolName)), ?assertMatch({ok, _, [[1]]}, emqx_resource:query(PoolName, test_query_no_params())), ?assertMatch({ok, _, [[1]]}, emqx_resource:query(PoolName, test_query_with_params())), ?assertMatch( diff --git a/apps/emqx_connector/test/emqx_connector_pgsql_SUITE.erl b/apps/emqx_connector/test/emqx_connector_pgsql_SUITE.erl index ed7bf7f4f..d99d8ab6c 100644 --- a/apps/emqx_connector/test/emqx_connector_pgsql_SUITE.erl +++ b/apps/emqx_connector/test/emqx_connector_pgsql_SUITE.erl @@ -83,7 +83,7 @@ perform_lifecycle_check(PoolName, InitialConfig) -> status := InitialStatus }} = emqx_resource:get_instance(PoolName), - ?assertEqual(ok, emqx_resource:health_check(PoolName)), + ?assertEqual({ok, connected}, emqx_resource:health_check(PoolName)), % % Perform query as further check that the resource is working as expected ?assertMatch({ok, _, [{1}]}, emqx_resource:query(PoolName, test_query_no_params())), ?assertMatch({ok, _, [{1}]}, emqx_resource:query(PoolName, test_query_with_params())), @@ -96,7 +96,7 @@ perform_lifecycle_check(PoolName, InitialConfig) -> }} = emqx_resource:get_instance(PoolName), ?assertEqual(StoppedStatus, disconnected), - ?assertEqual({error, stopped}, emqx_resource:health_check(PoolName)), + ?assertEqual({error, resource_is_stopped}, emqx_resource:health_check(PoolName)), % Resource healthcheck shortcuts things by checking ets. Go deeper by checking pool itself. ?assertEqual({error, not_found}, ecpool:stop_sup_pool(ReturnedPoolName)), % Can call stop/1 again on an already stopped instance @@ -107,7 +107,7 @@ perform_lifecycle_check(PoolName, InitialConfig) -> timer:sleep(500), {ok, ?CONNECTOR_RESOURCE_GROUP, #{status := InitialStatus}} = emqx_resource:get_instance(PoolName), - ?assertEqual(ok, emqx_resource:health_check(PoolName)), + ?assertEqual({ok, connected}, emqx_resource:health_check(PoolName)), ?assertMatch({ok, _, [{1}]}, emqx_resource:query(PoolName, test_query_no_params())), ?assertMatch({ok, _, [{1}]}, emqx_resource:query(PoolName, test_query_with_params())), % Stop and remove the resource in one go. diff --git a/apps/emqx_connector/test/emqx_connector_redis_SUITE.erl b/apps/emqx_connector/test/emqx_connector_redis_SUITE.erl index 9f0312eb3..8c54b7224 100644 --- a/apps/emqx_connector/test/emqx_connector_redis_SUITE.erl +++ b/apps/emqx_connector/test/emqx_connector_redis_SUITE.erl @@ -97,7 +97,7 @@ perform_lifecycle_check(PoolName, InitialConfig, RedisCommand) -> status := InitialStatus }} = emqx_resource:get_instance(PoolName), - ?assertEqual(ok, emqx_resource:health_check(PoolName)), + ?assertEqual({ok, connected}, emqx_resource:health_check(PoolName)), % Perform query as further check that the resource is working as expected ?assertEqual({ok, <<"PONG">>}, emqx_resource:query(PoolName, {cmd, RedisCommand})), ?assertEqual(ok, emqx_resource:stop(PoolName)), @@ -109,7 +109,7 @@ perform_lifecycle_check(PoolName, InitialConfig, RedisCommand) -> }} = emqx_resource:get_instance(PoolName), ?assertEqual(StoppedStatus, disconnected), - ?assertEqual({error, stopped}, emqx_resource:health_check(PoolName)), + ?assertEqual({error, resource_is_stopped}, emqx_resource:health_check(PoolName)), % Resource healthcheck shortcuts things by checking ets. Go deeper by checking pool itself. ?assertEqual({error, not_found}, ecpool:stop_sup_pool(ReturnedPoolName)), % Can call stop/1 again on an already stopped instance @@ -120,7 +120,7 @@ perform_lifecycle_check(PoolName, InitialConfig, RedisCommand) -> timer:sleep(500), {ok, ?CONNECTOR_RESOURCE_GROUP, #{status := InitialStatus}} = emqx_resource:get_instance(PoolName), - ?assertEqual(ok, emqx_resource:health_check(PoolName)), + ?assertEqual({ok, connected}, emqx_resource:health_check(PoolName)), ?assertEqual({ok, <<"PONG">>}, emqx_resource:query(PoolName, {cmd, RedisCommand})), % Stop and remove the resource in one go. ?assertEqual(ok, emqx_resource:remove_local(PoolName)), diff --git a/apps/emqx_resource/src/emqx_resource.erl b/apps/emqx_resource/src/emqx_resource.erl index 29309afcc..f5b03e76b 100644 --- a/apps/emqx_resource/src/emqx_resource.erl +++ b/apps/emqx_resource/src/emqx_resource.erl @@ -276,7 +276,7 @@ restart(InstId, Opts) -> stop(InstId) -> emqx_resource_manager:stop(InstId). --spec health_check(instance_id()) -> resource_status(). +-spec health_check(instance_id()) -> {ok, resource_status()} | {error, term()}. health_check(InstId) -> emqx_resource_manager:health_check(InstId). diff --git a/apps/emqx_resource/src/emqx_resource_manager.erl b/apps/emqx_resource/src/emqx_resource_manager.erl index 405b47097..d48c910fa 100644 --- a/apps/emqx_resource/src/emqx_resource_manager.erl +++ b/apps/emqx_resource/src/emqx_resource_manager.erl @@ -190,7 +190,7 @@ list_group(Group) -> List = ets:match(?ETS_TABLE, {'$1', Group, '_'}), lists:flatten(List). --spec health_check(instance_id()) -> resource_status(). +-spec health_check(instance_id()) -> {ok, resource_status()} | {error, term()}. health_check(InstId) -> safe_call(InstId, health_check). @@ -351,7 +351,7 @@ proc_name(Id) -> handle_health_check_request(From, Data) -> with_health_check(Data, fun(Status, UpdatedData) -> - Actions = [{reply, From, Status}], + Actions = [{reply, From, {ok, Status}}], {next_state, Status, UpdatedData, Actions} end). diff --git a/apps/emqx_resource/test/emqx_resource_SUITE.erl b/apps/emqx_resource/test/emqx_resource_SUITE.erl index 77053054f..77d0e57c3 100644 --- a/apps/emqx_resource/test/emqx_resource_SUITE.erl +++ b/apps/emqx_resource/test/emqx_resource_SUITE.erl @@ -182,7 +182,7 @@ t_healthy(_) -> timer:sleep(300), emqx_resource:set_resource_status_connecting(?ID), - ok = emqx_resource:health_check(?ID), + {ok, connected} = emqx_resource:health_check(?ID), ?assertMatch( [#{status := connected}], @@ -191,7 +191,7 @@ t_healthy(_) -> erlang:exit(Pid, shutdown), - ?assertEqual({error, connecting}, emqx_resource:health_check(?ID)), + ?assertEqual({ok, connecting}, emqx_resource:health_check(?ID)), ?assertMatch( [], @@ -368,7 +368,7 @@ t_auto_retry(_) -> #{name => test_resource, create_error => true}, #{auto_retry_interval => 100} ), - ?assertEqual(error, Res). + ?assertEqual(ok, Res). %%------------------------------------------------------------------------------ %% Helpers