lanonna/pam/bin/mq.ml

47 lines
1.8 KiB
OCaml
Raw Normal View History

2024-03-16 09:34:29 +01:00
open Amqp_client_lwt
let handle_consumer_exn _exn =
Lwt.return_unit (* TODO *)
let no_routing_key = `Queue ""
type rabbit_actor = {queue: Queue.t; channel: Channel.no_confirm Channel.t; connection: Connection.t}
2024-04-17 18:18:19 +02:00
let init (mq_url, mq_user, mq_password) consume_fn =
2024-03-16 09:34:29 +01:00
(* Init rabbitmq client *)
2024-03-25 14:17:34 +01:00
let%lwt connection = Amqp.Connection.connect ~credentials:(mq_user, mq_password) ~id:"pam" mq_url in
let%lwt channel = Amqp.Connection.open_channel ~id:"pam" Amqp.Channel.no_confirm connection in
2024-03-16 09:34:29 +01:00
let%lwt queue = Amqp.Queue.declare channel "pam" in
2024-04-17 18:18:19 +02:00
let _ = Pamlog.info "Initialized rabbitmq client" in
2024-03-16 09:34:29 +01:00
(* Establish consumer *)
let%lwt (_ampq_consumer, reader) = Amqp.Queue.consume ~no_ack:true ~id:"" channel queue in
2024-04-17 18:18:19 +02:00
let _ = Thread.spawn ~exn_handler:handle_consumer_exn (Thread.Pipe.iter_without_pushback reader ~f:consume_fn) in
2024-03-16 09:34:29 +01:00
Lwt.return {queue=queue; channel=channel; connection=connection}
let shutdown {queue=_queue; channel; connection} =
let ignore_exn = fun _ -> Lwt.return_unit in
let%lwt _ = Lwt.catch (fun () -> Amqp.Channel.close channel) ignore_exn in
let%lwt _ = Lwt.catch (fun () -> Amqp.Connection.close connection) ignore_exn in
Lwt.return_unit
2024-05-01 12:03:26 +02:00
let mq_publish {queue=_; channel=channel; connection=_} msg =
let _ = Pamlog.info [%string "GOT MSG FOR MQ:: %{msg}"] in
let%lwt _ =
Message.make msg
|> Exchange.publish channel Exchange.default ~routing_key:"lanonna"
in Lwt.return_unit
2024-05-02 12:26:01 +02:00
let error_msg_to_json_format room_id error =
let open Yojson.Basic in
let content = [%string {|Pam è entrata in errore: %{error}| |}] in
let d = `Assoc [
("content", `String content);
("source_message_id", `Null);
("room_id", `String room_id);
("as_reply", `Bool false);
("as_markdown", `Bool true)
] in
to_string d