diff --git a/apps/emqx_bridge/src/emqx_bridge_v2.erl b/apps/emqx_bridge/src/emqx_bridge_v2.erl index 79e8fc8f8..0b2e9277a 100644 --- a/apps/emqx_bridge/src/emqx_bridge_v2.erl +++ b/apps/emqx_bridge/src/emqx_bridge_v2.erl @@ -456,7 +456,7 @@ install_bridge_v2_helper( ConnectorId = emqx_connector_resource:resource_id( connector_type(BridgeV2Type), ConnectorName ), - emqx_resource_manager:add_channel( + _ = emqx_resource_manager:add_channel( ConnectorId, BridgeV2Id, augment_channel_config( @@ -786,7 +786,11 @@ create_dry_run_helper(ConfRootKey, BridgeV2Type, ConnectorRawConf, BridgeV2RawCo BridgeName, BridgeV2Conf ), - case emqx_resource_manager:add_channel(ConnectorId, ChannelTestId, AugmentedConf) of + %% We'll perform it ourselves to get the resulting status afterwards. + Opts = #{perform_health_check => false}, + case + emqx_resource_manager:add_channel(ConnectorId, ChannelTestId, AugmentedConf, Opts) + of {error, Reason} -> {error, Reason}; ok -> diff --git a/apps/emqx_resource/src/emqx_resource_manager.erl b/apps/emqx_resource/src/emqx_resource_manager.erl index 816c38301..c042054e3 100644 --- a/apps/emqx_resource/src/emqx_resource_manager.erl +++ b/apps/emqx_resource/src/emqx_resource_manager.erl @@ -14,6 +14,9 @@ %% limitations under the License. %%-------------------------------------------------------------------- -module(emqx_resource_manager). + +-feature(maybe_expr, enable). + -behaviour(gen_statem). -include("emqx_resource.hrl"). @@ -34,6 +37,7 @@ health_check/1, channel_health_check/2, add_channel/3, + add_channel/4, remove_channel/2, get_channels/1 ]). @@ -133,6 +137,12 @@ ST =:= ?status_connecting; ST =:= ?status_connected; ST =:= ?status_disconnected ). +-type add_channel_opts() :: #{ + %% Whether to immediately perform a health check after adding the channel. + %% Default: `true' + perform_health_check => boolean() +}. + %%------------------------------------------------------------------------------ %% API %%------------------------------------------------------------------------------ @@ -378,10 +388,30 @@ channel_health_check(ResId, ChannelId) -> _ = health_check(ResId), safe_call(ResId, {channel_health_check, ChannelId}, ?T_OPERATION). +-spec add_channel( + connector_resource_id(), + action_resource_id() | source_resource_id(), + _Config +) -> + ok | {error, term()}. add_channel(ResId, ChannelId, Config) -> + add_channel(ResId, ChannelId, Config, _Opts = #{}). + +-spec add_channel( + connector_resource_id(), + action_resource_id() | source_resource_id(), + _Config, + add_channel_opts() +) -> + ok | {error, term()}. +add_channel(ResId, ChannelId, Config, Opts) -> Result = safe_call(ResId, {add_channel, ChannelId, Config}, ?T_OPERATION), - %% Wait for health_check to finish - _ = channel_health_check(ResId, ChannelId), + maybe + true ?= maps:get(perform_health_check, Opts, true), + %% Wait for health_check to finish + _ = channel_health_check(ResId, ChannelId), + ok + end, Result. remove_channel(ResId, ChannelId) ->