Skip to content

Commit bab27f2

Browse files
committed
feat: add readme
1 parent e31a54d commit bab27f2

File tree

6 files changed

+107
-99
lines changed

6 files changed

+107
-99
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ on:
1010
workflow_dispatch:
1111

1212
jobs:
13-
# test:
14-
# name: Run tests
15-
# runs-on: ubuntu-24.04
13+
test:
14+
name: Run tests
15+
runs-on: ubuntu-24.04
1616

17-
# steps:
18-
# - uses: actions/checkout@v4
17+
steps:
18+
- uses: actions/checkout@v4
1919

20-
# - name: Setup Erlang
21-
# uses: erlef/setup-beam@v1
22-
# with:
23-
# otp-version: "26"
24-
# rebar3-version: "3"
20+
- name: Setup Erlang
21+
uses: erlef/setup-beam@v1
22+
with:
23+
otp-version: "26"
24+
rebar3-version: "3"
2525

26-
# - name: Start Dependencies
27-
# run: make up
26+
- name: Start Dependencies
27+
run: make up
2828

29-
# - name: Run Tests
30-
# run: make ct
29+
- name: Run Tests
30+
run: make ct
3131

32-
# - name: Cleanup
33-
# if: always()
34-
# run: make down
32+
- name: Cleanup
33+
if: always()
34+
run: make down
3535

3636
release:
3737
name: Publish release
38-
# needs: test
38+
needs: test
3939
if: startsWith(github.ref, 'refs/tags/')
4040
runs-on: ubuntu-24.04
4141

.github/workflows/run_tests.yml

Lines changed: 0 additions & 66 deletions
This file was deleted.

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,85 @@
11
# EMQX Offline Message Plugin
22

3+
## Usage
4+
5+
Download the plugin:
6+
7+
```bash
8+
wget https://github.com/savonarola/emqx-offline-message-plugin/releases/download/v0.0.4/emqx_offline_message_plugin-1.0.0.tar.gz
9+
```
10+
11+
Install the plugin:
12+
13+
```bash
14+
curl -u key:secret -X POST http://localhost:18083/api/v5/plugins/install \
15+
-H "Content-Type: multipart/form-data" \
16+
-F "plugin=@emqx_offline_message_plugin-1.0.0.tar.gz"
17+
```
18+
19+
Check the plugin is installed:
20+
21+
```bash
22+
curl -u key:secret http://localhost:18083/api/v5/plugins | jq
23+
```
24+
25+
Add connector:
26+
27+
```bash
28+
curl -u key:secret -X POST http://localhost:18083/api/v5/connectors \
29+
-H "Content-Type: application/json" \
30+
-d '{"type":"mysql","name":"omp","server":"mysql","database":"emqx","pool_size":8,"username":"emqx","password":"public","resource_opts":{"start_timeout":"5s","health_check_interval":"15s"}}'
31+
32+
curl -s -u key:secret http://localhost:18083/api/v5/connectors | jq
33+
```
34+
35+
Add publish action:
36+
37+
```bash
38+
curl -u key:secret -X POST http://localhost:18083/api/v5/actions \
39+
-H "Content-Type: application/json" \
40+
-d '{"type":"mysql","parameters":{"undefined_vars_as_null":false,"sql":"insert into mqtt_msg(msgid, sender, topic, qos, retain, payload, arrived) values(${id}, ${clientid}, ${topic}, ${qos}, ${retain}, ${payload}, FROM_UNIXTIME(${timestamp}/1000))"},"name":"omp_publish_action","enable":true,"connector":"omp"}'
41+
42+
curl -s -u key:secret http://localhost:18083/api/v5/actions | jq
43+
```
44+
45+
Add publish rule:
46+
47+
```bash
48+
curl -u key:secret -X POST http://localhost:18083/api/v5/rules \
49+
-H "Content-Type: application/json" \
50+
-d '{"sql":"SELECT id, clientid, topic, qos, payload, timestamp, int(coalesce(flags.retain, 0)) as retain FROM \"t/#\" WHERE qos = 1 or qos = 2","name":"omp_publish_rule","description":"Offline Message Plugin Publish Action","actions":["mysql:omp_publish_action"]}'
51+
52+
curl -s -u key:secret http://localhost:18083/api/v5/rules | jq
53+
```
54+
55+
Add subscribe/ack rule.
56+
This the only configuration change that is unavailable from the Dashboard.
57+
58+
```bash
59+
curl -u key:secret -X POST http://localhost:18083/api/v5/rules \
60+
-H "Content-Type: application/json" \
61+
-d '{"sql":"SELECT * FROM \"$events/session_subscribed\", \"$events/message_acked\" WHERE topic =~ '"'"'t/#'"'"'","name":"omp_subscribe_ack_rule","description":"Offline Message Plugin Subscribe/Ack Action","actions":[{"function":"emqx_omp:action","args":{"opts":{},"connector_name":"mysql:omp"}}]}'
62+
63+
curl -s -u key:secret http://localhost:18083/api/v5/rules | jq
64+
```
65+
66+
Verify:
67+
68+
```bash
69+
mosquitto_pub -d -q 1 -t 't/2' -m 'hello-from-offline1'
70+
mosquitto_pub -d -q 1 -t 't/2' -m 'hello-from-offline2'
71+
mosquitto_pub -d -q 1 -t 't/2' -m 'hello-from-offline3'
72+
73+
mosquitto_sub -d -q 1 -t 't/2' -i $(pwgen 20 -1)
74+
```
75+
76+
No messages should be received:
77+
78+
```bash
79+
mosquitto_sub -d -q 1 -t 't/2' -i $(pwgen 20 -1)
80+
```
81+
82+
383
## Release
484

585
An EMQX plugin release is a tar file including including a subdirectory of this plugin's name and it's version, that contains:

src/emqx_omp_mysql.erl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
-spec on_message_acked(Envs :: map(), ConnectorName :: binary(), Opts :: map()) -> ok.
2828
on_message_acked(#{id := MsgId} = Envs, ConnectorName, Opts) ->
2929
ResourceId = resource_id(ConnectorName),
30-
3130
case emqx_resource:simple_sync_query(ResourceId, {sql, ?DELETE_SQL, [MsgId]}) of
3231
ok ->
3332
emqx_metrics_worker:inc(emqx_omp_metrics_worker, message_acked, success),

test/emqx_omp_SUITE.erl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ init_per_suite(Config) ->
4848
[{plugin_id, PluginId}, {plugin_filename, Filename} | Config].
4949

5050
end_per_suite(_Config) ->
51-
% cleanup
5251
ok = emqx_omp_test_api_helpers:delete_all_plugins(),
5352
ok = emqx_omp_test_helpers:stop(),
5453
ok.
@@ -90,20 +89,12 @@ end_per_testcase(_Case, _Config) ->
9089
%%--------------------------------------------------------------------
9190

9291
t_ok(_Config) ->
93-
%% create actions and rules
94-
% PublishAction = publish_mysql_action("omp_publish_action", "omp"),
95-
% ok = emqx_omp_test_api_helpers:create_action(PublishAction),
96-
% PublishRule = publish_mysql_rule("omp_publish_rule", "t/#", "mysql:omp_publish_action"),
97-
% ok = emqx_omp_test_api_helpers:create_rule(PublishRule),
98-
% SubscribeAckRule = subscribe_ack_rule("omp_subscribe_ack_rule", "t/#", "mysql:omp", #{}),
99-
% ok = emqx_omp_test_api_helpers:create_rule(SubscribeAckRule),
100-
10192
%% publish message
10293
Payload = emqx_guid:to_hexstr(emqx_guid:gen()),
10394
ClientPub = emqtt_connect(),
10495
_ = emqtt:publish(ClientPub, <<"t/1">>, Payload, 1),
10596
ok = emqtt:stop(ClientPub),
106-
ct:sleep(1000),
97+
ct:sleep(500),
10798

10899
%% A new subscriber should receive the message
109100
ClientSub0 = emqtt_connect(),
@@ -115,6 +106,7 @@ t_ok(_Config) ->
115106
ct:fail("Message not received")
116107
end,
117108
ok = emqtt:stop(ClientSub0),
109+
ct:sleep(500),
118110

119111
%% Another subscriber should NOT receive the message:
120112
%% it should be deleted.

test/emqx_omp_test_helpers.erl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ api_get(Path) ->
2626
handle_result(Result).
2727

2828
api_get_raw(Path) ->
29+
ct:pal("GET ~s~n~n", [api_url(Path)]),
2930
Result = hackney:request(get, api_url(Path), []),
3031
handle_result_raw(Result).
3132

@@ -34,6 +35,8 @@ api_post(Path, Body) ->
3435
handle_result(Result).
3536

3637
api_post_raw(Path, Headers0, Body) ->
38+
HeadersS = [io_lib:format("~s: ~s~n", [Key, Value]) || {Key, Value} <- Headers0],
39+
ct:pal("POST ~s~n~s~n~n...", [api_url(Path), iolist_to_binary(HeadersS)]),
3740
Headers = [auth_header() | Headers0],
3841
Result = hackney:request(post, api_url(Path), Headers, Body),
3942
handle_result_raw(Result).
@@ -51,16 +54,16 @@ api_delete(Path) ->
5154
handle_result(Result).
5255

5356
make_request({get, Path}) ->
54-
ct:print("GET ~s~n~n", [api_url(Path)]),
57+
ct:pal("GET ~s~n~n", [api_url(Path)]),
5558
hackney:request(get, api_url(Path), headers());
5659
make_request({post, Path, Body}) ->
57-
ct:print("POST ~s~n~n~s~n~n", [api_url(Path), encode_json(Body)]),
60+
ct:pal("POST ~s~n~n~s~n~n", [api_url(Path), encode_json(Body)]),
5861
hackney:request(post, api_url(Path), headers(), encode_json(Body));
5962
make_request({put, Path, Body}) ->
60-
ct:print("PUT ~s~n~n~s~n~n", [api_url(Path), encode_json(Body)]),
63+
ct:pal("PUT ~s~n~n~s~n~n", [api_url(Path), encode_json(Body)]),
6164
hackney:request(put, api_url(Path), headers(), encode_json(Body));
6265
make_request({delete, Path}) ->
63-
ct:print("DELETE ~s~n~n", [api_url(Path)]),
66+
ct:pal("DELETE ~s~n~n", [api_url(Path)]),
6467
hackney:request(delete, api_url(Path), headers()).
6568

6669
handle_result({ok, Code, _Headers, ClientRef}) when Code >= 200 andalso Code < 300 ->

0 commit comments

Comments
 (0)