Packages
diint_utilites_common_app
1.4.2
1.4.23
1.4.22
1.4.21
1.4.20
1.4.19
1.4.18
1.4.17
1.4.16
1.4.15
1.4.14
1.4.12
1.4.11
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.9
1.3.8
1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.101
1.2.11
1.2.10
1.2.9
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.13
1.0.12
1.0.11
1.0.10
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
библиотеки для работы с ребитом и протоколы
Current section
Files
Jump to
Current section
Files
src/log/rabbitMQ_logger_handler.erl
-module(rabbitMQ_logger_handler).
-include("_build/default/lib/amqp_client/include/amqp_client.hrl").
-include("include/types_rabbit.hrl").
-export([adding_handler/1, removing_handler/1, log/2]).
-export([init/1, handle_call/3, handle_cast/2, terminate/2]).
adding_handler(Config) ->
MyConfig = maps:get(config,Config),
{ok, Pid} = gen_server:start(?MODULE, MyConfig, []),
{ok, Config#{config => MyConfig#{pid => Pid}}}.
removing_handler(#{config := #{pid := Pid}}) ->
gen_server:stop(Pid).
log(LogEvent,#{config := #{pid := Pid}} = Config) ->
gen_server:cast(Pid, {log, LogEvent, Config}).
init(#{rabbitConfig := RabbitConfig, exchange := ExchangeName, queueName := QueueName}) ->
RmqConnectionConfig = rabbit_utilites:convert_array_to_rabbit_connection_config(RabbitConfig),
case connect_to_rabbit(RmqConnectionConfig) of
{ok, Connection, Channel} ->
#'exchange.declare_ok'{} = amqp_channel:call(Channel, #'exchange.declare'{exchange = ExchangeName}),
{ok, #{connection => Connection, channel => Channel, exchange => ExchangeName, queueName => QueueName}};
_ -> {error, "NoConnect to Log Rabbit"}
end.
handle_call(_, _, State) ->
{reply, {error, bad_request}, State}.
handle_cast({log, LogEvent, Config}, #{connection := _, channel := _, exchange := _, queueName := _} = State) ->
do_log(State, LogEvent, Config),
{noreply, State}.
terminate(_Reason, #{fd := Fd}) ->
_ = file:close(Fd),
ok.
do_log(#{connection := Connection, channel := Channel, exchange := ExchangeName, queueName := QueueName}, LogEvent, #{formatter := {FModule, FConfig}}) ->
String = FModule:format(LogEvent, FConfig),
Message = unicode:characters_to_binary(String, unicode, utf8),
Props = #'P_basic'{},
amqp_channel:cast(Channel, #'basic.publish'{exchange = ExchangeName}, #amqp_msg{payload = Message, props = Props}).
%% так надо, потом уберу
connect_to_rabbit(RabbitConfig) ->
case connect(RabbitConfig) of
{ok, Connection, NewChannel} -> {ok, Connection, NewChannel};
% #'exchange.declare_ok'{} = amqp_channel:call(NewChannel, #'exchange.declare'{exchange = Exchange}),
% #'queue.declare_ok'{} = amqp_channel:call(NewChannel, #'queue.declare'{queue = Queue, auto_delete = false, arguments = RabbitConfig#rabbit_connection_config.arguments}),
% #'queue.bind_ok'{} = amqp_channel:call(NewChannel, #'queue.bind'{queue = Queue, exchange = Exchange}),
Other -> Other
end.
connect(#rabbit_connection_config{ host = Host, username = Username, password = Password, virtual_host=VirtHost}) ->
case amqp_connection:start(#amqp_params_network{host = Host, password = Password, username = Username, heartbeat = 10, frame_max = 8388608, virtual_host = VirtHost}) of
{ok, Connection} ->
case amqp_connection:open_channel(Connection) of
{ok, Channel} ->
{ok, Connection, Channel};
ErrorChannel ->
{error, ErrorChannel}
end;
ErrorConnection ->
{error, ErrorConnection}
end.