lanonna/pam/bin/main.ml

76 lines
2.2 KiB
OCaml
Raw Normal View History

2024-03-16 09:34:29 +01:00
open Riot
open Datatypes
open Batteries
2024-03-25 14:17:34 +01:00
open Utils
2024-03-16 09:34:29 +01:00
2024-03-25 14:17:34 +01:00
let _MQ_CLIENT = "mq_client"
let _HTTP_CLIENT = "http_client"
let http_client base64password =
let http_actor = Httpclient.init base64password in
2024-03-16 09:34:29 +01:00
let _ = print_endline "Initialized http client" in
let rec loop () =
let _ =
match Riot.receive () with
2024-03-25 14:17:34 +01:00
| ListIssues ->
2024-03-16 09:34:29 +01:00
let reminders = Httpclient.make_get_request http_actor in
2024-03-25 14:17:34 +01:00
let msg = result_unpack reminders
2024-03-16 09:34:29 +01:00
in
2024-03-25 14:17:34 +01:00
Riot.send_by_name ~name:_MQ_CLIENT msg
| m -> unhandled m
2024-03-16 09:34:29 +01:00
in
loop ()
in loop ()
2024-03-25 14:17:34 +01:00
let mq_client (mq_url, mq_user, mq_password) consumer =
let pprint rem =
[%string "%{r.title}"]
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
2024-03-16 09:34:29 +01:00
let rec loop () =
let%lwt _ = Lwt_unix.sleep 1.0 in
let _ =
try%lwt
2024-03-25 14:17:34 +01:00
match Riot.receive ~after:one_second () with
2024-03-16 09:34:29 +01:00
| ForgejoError err ->
let _ = print_endline [%string "Got error from Forgejo: %{err}"] in
2024-03-25 14:17:34 +01:00
(* Lwt.return (Mq.mq_publish mq err ) *)
Mq.mq_publish mq err
2024-03-16 09:34:29 +01:00
| ForgejoIssues reminders ->
let _ = [%string "Got reminders: %{Batteries.dump reminders}"] |> print_endline in
2024-03-25 14:17:34 +01:00
let rems = List.map pprint reminders in
Mq.mq_publish_all mq rems
| m -> unhandled m
with | Riot.Receive_timeout -> Lwt.return_unit
2024-03-16 09:34:29 +01:00
in
loop ()
in
loop ()
2024-03-25 14:17:34 +01:00
let main (mq_url, mq_user, mq_password, base64_creds) =
let http_client_pid = spawn (fun () -> http_client base64_creds) in
let mq_client_pid = spawn (fun () -> Lwt_main.run (mq_client (mq_url, mq_user, mq_password) http_client_pid)) in
let _ = Riot.register _HTTP_CLIENT http_client_pid in
let _ = Riot.register _MQ_CLIENT mq_client_pid
2024-03-16 09:34:29 +01:00
in
2024-03-25 14:17:34 +01:00
let timeout = 6.0 in
let rec loop_ () =
let _ = send http_client_pid ListIssues in
sleep timeout |> loop_
2024-03-16 09:34:29 +01:00
in
2024-03-25 14:17:34 +01:00
sleep timeout |> loop_
2024-03-16 09:34:29 +01:00
let () =
2024-03-25 14:17:34 +01:00
let (mq_url, mq_user, mq_password, base64_creds) = Config.configuration () in
Riot.run (fun () -> main (mq_url, mq_user, mq_password, base64_creds))