1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2016 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%%============================================================================== |
16 |
|
|
17 |
|
-module(mod_event_pusher). |
18 |
|
-author('konrad.zemek@erlang-solutions.com'). |
19 |
|
|
20 |
|
-behaviour(gen_mod). |
21 |
|
-behaviour(mongoose_module_metrics). |
22 |
|
|
23 |
|
-include("mod_event_pusher_events.hrl"). |
24 |
|
-include("mongoose_config_spec.hrl"). |
25 |
|
|
26 |
|
-type backend() :: http | push | rabbit | sns. |
27 |
|
-type event() :: #user_status_event{} | #chat_event{} | #unack_msg_event{}. |
28 |
|
-export_type([event/0]). |
29 |
|
|
30 |
|
-export([deps/2, start/2, stop/1, config_spec/0, push_event/2]). |
31 |
|
|
32 |
|
-export([config_metrics/1]). |
33 |
|
|
34 |
|
-ignore_xref([behaviour_info/1]). |
35 |
|
|
36 |
|
%%-------------------------------------------------------------------- |
37 |
|
%% Callbacks |
38 |
|
%%-------------------------------------------------------------------- |
39 |
|
|
40 |
|
-callback push_event(mongoose_acc:t(), event()) -> mongoose_acc:t(). |
41 |
|
|
42 |
|
%%-------------------------------------------------------------------- |
43 |
|
%% API |
44 |
|
%%-------------------------------------------------------------------- |
45 |
|
|
46 |
|
%% @doc Pushes the event to each backend registered with the event_pusher. |
47 |
|
-spec push_event(mongoose_acc:t(), event()) -> mongoose_acc:t(). |
48 |
|
push_event(Acc, Event) -> |
49 |
434 |
HostType = mongoose_acc:host_type(Acc), |
50 |
434 |
Backends = maps:keys(gen_mod:get_loaded_module_opts(HostType, ?MODULE)), |
51 |
434 |
lists:foldl(fun(B, Acc0) -> (backend_module(B)):push_event(Acc0, Event) end, Acc, Backends). |
52 |
|
|
53 |
|
%%-------------------------------------------------------------------- |
54 |
|
%% gen_mod API |
55 |
|
%%-------------------------------------------------------------------- |
56 |
|
|
57 |
|
-spec deps(mongooseim:host_type(), gen_mod:module_opts()) -> gen_mod_deps:deps(). |
58 |
|
deps(_HostType, Opts) -> |
59 |
63 |
[{backend_module(Backend), BackendOpts, hard} || {Backend, BackendOpts} <- maps:to_list(Opts)]. |
60 |
|
|
61 |
|
-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> any(). |
62 |
|
start(HostType, _Opts) -> |
63 |
13 |
mod_event_pusher_hook_translator:add_hooks(HostType). |
64 |
|
|
65 |
|
-spec stop(mongooseim:host_type()) -> any(). |
66 |
|
stop(HostType) -> |
67 |
12 |
mod_event_pusher_hook_translator:delete_hooks(HostType). |
68 |
|
|
69 |
|
-spec config_spec() -> mongoose_config_spec:config_section(). |
70 |
|
config_spec() -> |
71 |
24 |
BackendItems = [{atom_to_binary(B, utf8), |
72 |
24 |
(backend_module(B)):config_spec()} || B <- all_backends()], |
73 |
24 |
#section{items = maps:from_list(BackendItems)}. |
74 |
|
|
75 |
|
-spec config_metrics(mongooseim:host_type()) -> [{gen_mod:opt_key(), gen_mod:opt_value()}]. |
76 |
|
config_metrics(HostType) -> |
77 |
:-( |
case gen_mod:get_module_opts(HostType, ?MODULE) of |
78 |
|
Empty when Empty =:= #{}; |
79 |
|
Empty =:= [] -> % TODO remove when get_module_opts does not return [] anymore |
80 |
:-( |
[{none, none}]; |
81 |
|
Opts -> |
82 |
:-( |
[{backend, Backend} || Backend <- maps:keys(Opts)] |
83 |
|
end. |
84 |
|
|
85 |
|
%%-------------------------------------------------------------------- |
86 |
|
%% Helpers |
87 |
|
%%-------------------------------------------------------------------- |
88 |
|
|
89 |
|
-spec backend_module(backend()) -> module(). |
90 |
150 |
backend_module(http) -> mod_event_pusher_http; |
91 |
340 |
backend_module(push) -> mod_event_pusher_push; |
92 |
24 |
backend_module(rabbit) -> mod_event_pusher_rabbit; |
93 |
79 |
backend_module(sns) -> mod_event_pusher_sns. |
94 |
|
|
95 |
|
all_backends() -> |
96 |
24 |
[http, push, rabbit, sns]. |