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-04-17 18:18:19 +02:00
|
|
|
let mq_publish {queue=_; channel=_; connection=_} msg =
|
|
|
|
let _ = Pamlog.info [%string "GOT MSG FOR MQ:: %{msg}"]
|
|
|
|
(* let%lwt _ = *)
|
|
|
|
(* Message.make msg *)
|
|
|
|
(* |> Exchange.publish channel Exchange.default ~routing_key:"lanonna" *)
|
2024-03-16 09:34:29 +01:00
|
|
|
in Lwt.return_unit
|
|
|
|
|
|
|
|
let rec mq_publish_all mq = function
|
|
|
|
| [] -> Lwt.return_unit
|
|
|
|
| r::rest -> let%lwt _ = mq_publish mq r in mq_publish_all mq rest
|