Skip to content

Commit

Permalink
Merge pull request #105 from zmstone/refactor-pin-emqx-5.0.0
Browse files Browse the repository at this point in the history
refactor: pin emqx 5.0.0
  • Loading branch information
zmstone authored Jul 4, 2022
2 parents 99fa9ea + 6b3d3b0 commit 32ae0ef
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 43 deletions.
4 changes: 2 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{deps,
[ %% not all plugins have to depend on emqx
%% but if they do, use git_subdir in the default profile
{emqx, {git_subdir, "https://github.com/emqx/emqx.git", {branch, "master"}, "apps/emqx"}}
{emqx, {git_subdir, "https://github.com/emqx/emqx.git", {tag, "v5.0.0"}, "apps/emqx"}}
%% this is my plugin's dependency
, {map_sets, "1.1.0"}
]}.
Expand All @@ -12,7 +12,7 @@

{erl_opts, [debug_info]}.

{relx, [ {release, {emqx_plugin_template, "5.0.0-rc.3"}, %% this is the release version, different from app vsn in .app file
{relx, [ {release, {emqx_plugin_template, "5.0.0"}, %% this is the release version, different from app vsn in .app file
[ emqx_plugin_template
, map_sets
]}
Expand Down
92 changes: 51 additions & 41 deletions src/emqx_plugin_template.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,26 @@

%% Called when the plugin application start
load(Env) ->
emqx_hooks:add('client.connect', {?MODULE, on_client_connect, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('client.connack', {?MODULE, on_client_connack, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('client.connected', {?MODULE, on_client_connected, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('client.disconnected', {?MODULE, on_client_disconnected, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('client.authenticate', {?MODULE, on_client_authenticate, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('client.authorize', {?MODULE, on_client_authorize, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('client.subscribe', {?MODULE, on_client_subscribe, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('client.unsubscribe', {?MODULE, on_client_unsubscribe, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('session.created', {?MODULE, on_session_created, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('session.subscribed', {?MODULE, on_session_subscribed, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('session.unsubscribed',{?MODULE, on_session_unsubscribed, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('session.resumed', {?MODULE, on_session_resumed, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('session.discarded', {?MODULE, on_session_discarded, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('session.takeovered', {?MODULE, on_session_takeovered, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('session.terminated', {?MODULE, on_session_terminated, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('message.publish', {?MODULE, on_message_publish, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('message.delivered', {?MODULE, on_message_delivered, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('message.acked', {?MODULE, on_message_acked, [Env]}, ?HP_HIGHEST),
emqx_hooks:add('message.dropped', {?MODULE, on_message_dropped, [Env]}, ?HP_HIGHEST).
hook('client.connect', {?MODULE, on_client_connect, [Env]}),
hook('client.connack', {?MODULE, on_client_connack, [Env]}),
hook('client.connected', {?MODULE, on_client_connected, [Env]}),
hook('client.disconnected', {?MODULE, on_client_disconnected, [Env]}),
hook('client.authenticate', {?MODULE, on_client_authenticate, [Env]}),
hook('client.authorize', {?MODULE, on_client_authorize, [Env]}),
hook('client.check_acl', {?MODULE, on_client_check_acl, [Env]}),
hook('client.subscribe', {?MODULE, on_client_subscribe, [Env]}),
hook('client.unsubscribe', {?MODULE, on_client_unsubscribe, [Env]}),
hook('session.created', {?MODULE, on_session_created, [Env]}),
hook('session.subscribed', {?MODULE, on_session_subscribed, [Env]}),
hook('session.unsubscribed',{?MODULE, on_session_unsubscribed, [Env]}),
hook('session.resumed', {?MODULE, on_session_resumed, [Env]}),
hook('session.discarded', {?MODULE, on_session_discarded, [Env]}),
hook('session.takeovered', {?MODULE, on_session_takeovered, [Env]}),
hook('session.terminated', {?MODULE, on_session_terminated, [Env]}),
hook('message.publish', {?MODULE, on_message_publish, [Env]}),
hook('message.delivered', {?MODULE, on_message_delivered, [Env]}),
hook('message.acked', {?MODULE, on_message_acked, [Env]}),
hook('message.dropped', {?MODULE, on_message_dropped, [Env]}).

%%--------------------------------------------------------------------
%% Client LifeCircle Hooks
Expand Down Expand Up @@ -164,36 +165,45 @@ on_message_publish(Message, _Env) ->
on_message_dropped(#message{topic = <<"$SYS/", _/binary>>}, _By, _Reason, _Env) ->
ok;
on_message_dropped(Message, _By = #{node := Node}, Reason, _Env) ->
io:format("Message dropped by node ~s due to ~s: ~p~n",
io:format("Message dropped by node ~p due to ~p:~n~p~n",
[Node, Reason, emqx_message:to_map(Message)]).

on_message_delivered(_ClientInfo = #{clientid := ClientId}, Message, _Env) ->
io:format("Message delivered to client(~s): ~p~n",
io:format("Message delivered to client(~s):~n~p~n",
[ClientId, emqx_message:to_map(Message)]),
{ok, Message}.

on_message_acked(_ClientInfo = #{clientid := ClientId}, Message, _Env) ->
io:format("Message acked by client(~s): ~p~n",
io:format("Message acked by client(~s):~n~p~n",
[ClientId, emqx_message:to_map(Message)]).

%% Called when the plugin application stop
unload() ->
emqx_hooks:del('client.connect', {?MODULE, on_client_connect}),
emqx_hooks:del('client.connack', {?MODULE, on_client_connack}),
emqx_hooks:del('client.connected', {?MODULE, on_client_connected}),
emqx_hooks:del('client.disconnected', {?MODULE, on_client_disconnected}),
emqx_hooks:del('client.authenticate', {?MODULE, on_client_authenticate}),
emqx_hooks:del('client.authorize', {?MODULE, on_client_authorize}),
emqx_hooks:del('client.subscribe', {?MODULE, on_client_subscribe}),
emqx_hooks:del('client.unsubscribe', {?MODULE, on_client_unsubscribe}),
emqx_hooks:del('session.created', {?MODULE, on_session_created}),
emqx_hooks:del('session.subscribed', {?MODULE, on_session_subscribed}),
emqx_hooks:del('session.unsubscribed',{?MODULE, on_session_unsubscribed}),
emqx_hooks:del('session.resumed', {?MODULE, on_session_resumed}),
emqx_hooks:del('session.discarded', {?MODULE, on_session_discarded}),
emqx_hooks:del('session.takeovered', {?MODULE, on_session_takeovered}),
emqx_hooks:del('session.terminated', {?MODULE, on_session_terminated}),
emqx_hooks:del('message.publish', {?MODULE, on_message_publish}),
emqx_hooks:del('message.delivered', {?MODULE, on_message_delivered}),
emqx_hooks:del('message.acked', {?MODULE, on_message_acked}),
emqx_hooks:del('message.dropped', {?MODULE, on_message_dropped}).
unhook('client.connect', {?MODULE, on_client_connect}),
unhook('client.connack', {?MODULE, on_client_connack}),
unhook('client.connected', {?MODULE, on_client_connected}),
unhook('client.disconnected', {?MODULE, on_client_disconnected}),
unhook('client.authenticate', {?MODULE, on_client_authenticate}),
unhook('client.authorize', {?MODULE, on_client_authorize}),
unhook('client.check_acl', {?MODULE, on_client_check_acl}),
unhook('client.subscribe', {?MODULE, on_client_subscribe}),
unhook('client.unsubscribe', {?MODULE, on_client_unsubscribe}),
unhook('session.created', {?MODULE, on_session_created}),
unhook('session.subscribed', {?MODULE, on_session_subscribed}),
unhook('session.unsubscribed',{?MODULE, on_session_unsubscribed}),
unhook('session.resumed', {?MODULE, on_session_resumed}),
unhook('session.discarded', {?MODULE, on_session_discarded}),
unhook('session.takeovered', {?MODULE, on_session_takeovered}),
unhook('session.terminated', {?MODULE, on_session_terminated}),
unhook('message.publish', {?MODULE, on_message_publish}),
unhook('message.delivered', {?MODULE, on_message_delivered}),
unhook('message.acked', {?MODULE, on_message_acked}),
unhook('message.dropped', {?MODULE, on_message_dropped}).

hook(HookPoint, MFA) ->
%% use highest hook priority so this module's callbacks
%% are evaluated before the default hooks in EMQX
emqx_hooks:add(HookPoint, MFA, _Property = ?HP_HIGHEST).

unhook(HookPoint, MFA) ->
emqx_hooks:del(HookPoint, MFA).

0 comments on commit 32ae0ef

Please sign in to comment.