lanonna/pam/bin/main.ml

78 lines
2 KiB
OCaml
Raw Normal View History

2024-03-16 09:34:29 +01:00
open Riot
open Datatypes
open Batteries
open Util
let http mq_pid =
let http_actor = Httpclient.init () in
let _ = print_endline "Initialized http client" in
let rec loop () =
let _ =
match Riot.receive () with
| WebReq ->
let _ = print_endline "Got webreq" in
let reminders = Httpclient.make_get_request http_actor in
let msg =
reminders
|> Result.map (fun f -> Batteries.dump f |> print_endline; f)
|> result_unpack
in
Riot.send mq_pid msg
| _ -> failwith "Unknown msg"
in
loop ()
in loop ()
let rabbit consumer =
let%lwt mq = Mq.init consumer in
let rec loop () =
let%lwt _ = Lwt_unix.sleep 1.0 in
let _ =
try%lwt
match Riot.receive ~after:1000L () with (* TODO: Somehow the 1000L doesn't work. Report it.*)
| ForgejoError err ->
let _ = print_endline [%string "Got error from Forgejo: %{err}"] in
Lwt.return (Mq.mq_publish mq err )
| ForgejoIssues reminders ->
let _ = [%string "Got reminders: %{Batteries.dump reminders}"] |> print_endline in
let rems = List.map of_reminder reminders in
Lwt.return (Mq.mq_publish_all mq rems)
| _ ->
failwith "Unhandled msg"
with | Riot.Receive_timeout -> failwith "dunno"
in
loop ()
in
loop ()
let loop http_pid =
let rec loop_ () =
let _ = send http_pid WebReq in
let _ = sleep 60.0 in
loop_ ()
in
loop_()
let main () =
let _own = self () in
let http = spawn (fun () -> http _own) in (*TODO: nope*)
let _mq = spawn (fun () -> Lwt_main.run (rabbit http)) in
sleep 2.0
;
send _mq (Datatypes.ForgejoIssues []);
let _ = spawn (fun () -> loop http) in
let _ = print_endline "Now looping"
in
let rec forever () = (* TODO: is there something better? Like monitoring processes ?*)
sleep 22.2; forever ()
in forever ()
let () =
Config.configuration () |> print_endline;
Riot.run main