Skip to content
Permalink
Browse files

API for get_resource_status

  • Loading branch information...
terry-xiaoyu committed Jun 4, 2019
1 parent dd9a8d4 commit d162246c0b630e059c21f7b36e50154f3d7832e3
Showing with 21 additions and 4 deletions.
  1. +5 −4 src/emqx_rule_engine.erl
  2. +16 −0 src/emqx_rule_engine_api.erl
@@ -28,6 +28,7 @@
, create_resource/1
, test_resource/1
, start_resource/1
, get_resource_status/1
, delete_resource/1
]).

@@ -218,9 +219,9 @@ test_resource(#{type := Type, config := Config}) ->
get_resource_status(ResId) ->
case emqx_rule_registry:find_resource(ResId) of
{ok, #resource{type = ResType}} ->
{ok, #resource_type{on_status = {Mod, Status}}}
{ok, #resource_type{on_status = {Mod, OnStatus}}}
= emqx_rule_registry:find_resource_type(ResType),
Status = fetch_resource_status(Mod, Status, ResId),
Status = fetch_resource_status(Mod, OnStatus, ResId),
{ok, Status};
not_found ->
{error, {resource_not_found, ResId}}
@@ -380,11 +381,11 @@ clear_action(Module, Destroy, ActionInstId) ->
ok
end.

fetch_resource_status(Module, Status, ResId) ->
fetch_resource_status(Module, OnStatus, ResId) ->
case emqx_rule_registry:find_resource_params(ResId) of
{ok, #resource_params{params = Params}} ->
Status =
try Module:Status(ResId, Params)
try Module:OnStatus(ResId, Params)
catch _Error:Reason:STrace ->
?LOG(error, "get resource status for ~p failed: ~0p", [ResId, {Reason, STrace}]),
#{is_alive => false}
@@ -83,6 +83,13 @@
descr => "Show a resource"
}).

-rest_api(#{name => get_resource_status,
method => 'GET',
path => "/resource_status/:bin:id",
func => get_resource_status,
descr => "Get status of a resource"
}).

-rest_api(#{name => start_resource,
method => 'POST',
path => "/resources/:bin:id",
@@ -131,6 +138,7 @@
-export([ create_resource/2
, list_resources/2
, show_resource/2
, get_resource_status/2
, start_resource/2
, delete_resource/2
]).
@@ -266,6 +274,14 @@ list_resources_by_type(#{type := Type}, _Params) ->
show_resource(#{id := Id}, _Params) ->
reply_with(fun emqx_rule_registry:find_resource/1, Id).

get_resource_status(#{id := Id}, _Params) ->
case emqx_rule_engine:get_resource_status(Id) of
{ok, Status} ->
return({ok, Status});
{error, {resource_not_found, ResId}} ->
return({error, 400, ?ERR_NO_RESOURCE(ResId)})
end.

start_resource(#{id := Id}, _Params) ->
try emqx_rule_engine:start_resource(Id) of
ok ->

0 comments on commit d162246

Please sign in to comment.
You can’t perform that action at this time.