Skip to content
Permalink
Browse files

Fetch resource status

  • Loading branch information...
terry-xiaoyu committed Jun 4, 2019
1 parent ccbffd7 commit dd9a8d4801f650c1ac888f7420f5497f7d0d6c73
Showing with 35 additions and 1 deletion.
  1. +6 −0 include/rule_engine.hrl
  2. +29 −1 src/emqx_rule_engine.erl
@@ -31,6 +31,10 @@

-type(hook() :: atom() | 'any').

-type(resource_status() :: #{ alive := boolean()
, atom() => binary() | atom() | list(binary()|atom())
}).

-define(descr, #{en => <<>>, zh => <<>>}).

-record(action,
@@ -67,6 +71,7 @@
{ id :: resource_id()
, type :: resource_type_name()
, config :: #{} %% the configs got from API for initializing resource
, created_at :: erlang:timestamp()
, description :: binary()
}).

@@ -75,6 +80,7 @@
, provider :: atom()
, params_spec :: #{atom() => term()} %% params specs
, on_create :: mf()
, on_status :: mf()
, on_destroy :: mf()
, title = ?descr :: descr()
, description = ?descr :: descr()
@@ -105,7 +105,8 @@ new_action({App, Mod, #{name := Name,
params := ParamsSpec} = Params}) ->
ok = emqx_rule_validator:validate_spec(ParamsSpec),
#action{name = Name, for = Hook, app = App, types = Types,
module = Mod, on_create = Create, on_destroy = maps:get(destroy, Params, undefined),
module = Mod, on_create = Create,
on_destroy = maps:get(destroy, Params, undefined),
params_spec = ParamsSpec,
title = maps:get(title, Params, ?descr),
description = maps:get(description, Params, ?descr)}.
@@ -117,6 +118,7 @@ new_resource_type({App, Mod, #{name := Name,
#resource_type{name = Name, provider = App,
params_spec = ParamsSpec,
on_create = {Mod, Create},
on_status = {Mod, maps:get(status, Params, undefined)},
on_destroy = {Mod, maps:get(destroy, Params, undefined)},
title = maps:get(title, Params, ?descr),
description = maps:get(description, Params, ?descr)}.
@@ -212,6 +214,18 @@ test_resource(#{type := Type, config := Config}) ->
{error, {resource_type_not_found, Type}}
end.

-spec(get_resource_status(resource_id()) -> {ok, resource_status()} | {error, Reason :: term()}).
get_resource_status(ResId) ->
case emqx_rule_registry:find_resource(ResId) of
{ok, #resource{type = ResType}} ->
{ok, #resource_type{on_status = {Mod, Status}}}
= emqx_rule_registry:find_resource_type(ResType),
Status = fetch_resource_status(Mod, Status, ResId),
{ok, Status};
not_found ->
{error, {resource_not_found, ResId}}
end.

-spec(delete_resource(resource_id()) -> ok | {error, Reason :: term()}).
delete_resource(ResId) ->
case emqx_rule_registry:find_resource(ResId) of
@@ -366,6 +380,20 @@ clear_action(Module, Destroy, ActionInstId) ->
ok
end.

fetch_resource_status(Module, Status, ResId) ->
case emqx_rule_registry:find_resource_params(ResId) of
{ok, #resource_params{params = Params}} ->
Status =
try Module:Status(ResId, Params)
catch _Error:Reason:STrace ->
?LOG(error, "get resource status for ~p failed: ~0p", [ResId, {Reason, STrace}]),
#{is_alive => false}
end,
Status;
not_found ->
#{is_alive => false}
end.

refresh_actions_of_a_resource(ResId) ->
[lists:foreach(
fun(#action_instance{args = Args = #{<<"$resource">> := ResId0},

0 comments on commit dd9a8d4

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