lanonna/pam/bin/main.ml
2024-04-02 14:38:00 +02:00

73 lines
2.2 KiB
OCaml

open Riot
open Datatypes
open Batteries
open Utils
let _MQ_CLIENT = "mq_client"
let _HTTP_CLIENT = "http_client"
let http_client (repos: Config.repo_data list) =
let http_actor = Httpclient.init repos in
let _ = print_endline "Initialized http client" in
let rec loop () =
let _ =
match Riot.receive () with
| ListIssues ->
Httpclient.make_get_request http_actor
|> List.map (Result.fold ~ok:identity ~error:identity)
|> List.iter (Riot.send_by_name ~name:_MQ_CLIENT)
| m -> unhandled m
in
loop ()
in loop ()
let mq_client (mq_url, mq_user, mq_password) consumer =
let pprint rem =
[%string "%{rem.title}|%{rem.matrix_target}"]
in
let call_consumer pid { Amqp_client_lwt.Message.message = (_content, body); _ } =
Riot.send pid (Datatypes.ReceivedFromMq ("GOT RABBITs: "^body)) (*TODO: log*)
in
let%lwt mq = Mq.init (mq_url, mq_user, mq_password) consumer call_consumer in
let rec loop () =
let%lwt _ = Lwt_unix.sleep 1.0 in
let _ =
try%lwt
match Riot.receive ~after:one_second () with
| ForgejoError err ->
let _ = print_endline [%string "Got error from Forgejo: %{err}"] in
(* Lwt.return (Mq.mq_publish mq err ) *)
Mq.mq_publish mq err
| ForgejoIssues reminders ->
let _ = [%string "Got reminders: %{Batteries.dump reminders}"] |> print_endline in
let rems = List.map pprint reminders in
Mq.mq_publish_all mq rems
| m -> unhandled m
with | Riot.Receive_timeout -> Lwt.return_unit
in
loop ()
in
loop ()
let main (config: Config.config) =
let http_client_pid = spawn (fun () -> http_client config.repos) in
let mq_client_pid = spawn (fun () -> Lwt_main.run (mq_client (config.mq_url, config.mq_user, config.mq_password) http_client_pid)) in
let _ = Riot.register _HTTP_CLIENT http_client_pid in
let _ = Riot.register _MQ_CLIENT mq_client_pid
in
let timeout = 6.0 in
let rec loop_ () =
let _ = send http_client_pid ListIssues in
sleep timeout |> loop_
in
sleep timeout |> loop_
let () =
let config = Config.configuration () |> Result.fold ~error:exit2 ~ok:identity in
Riot.run (fun () -> main config)