package archi

  1. Overview
  2. Docs
A library for managing the lifecycle of stateful components in OCaml

Install

Dune Dependency

Authors

Maintainers

Sources

archi-0.1.0.tbz
sha256=b485cd300d87c5d0c7448c70f2ad85b508bb5d0e377900b3d6fb70c71c5ac5db
sha512=2c3239c570750b02f078382f7ca1a87a2de55c817c17611abc0e58c32012a390174aa2b4fb3e89f646b8053ed443e46d98b176477bc2ba94152f38cf5253e8e2

Description

Archi is an OCaml library for managing the lifecycle of stateful components and their dependencies.

Published: 08 May 2020

README

archi

Archi is an OCaml library for managing the lifecycle of stateful components and their dependencies.

Installation

$ opam install archi # choose your preferred runtime: archi-lwt archi-async

Usage & Examples

TODO, read the mli file for now.

Examples

The design of this library is heavily inspired by the component library for Clojure. The terminology is similar, and so is the API.

open Lwt.Infix
open Archi_lwt

module Database = struct
  type db = { handle : int }

  let start () =
    Format.eprintf "Started DB.@.";
    Lwt_result.return { handle = 42 }

  let stop _db = Lwt_io.eprintlf "Stopped DB.%!"

  let component = Component.make ~start ~stop
end

module WebServer = struct
  type t = Lwt_io.server

  let start () _db : (t, string) Lwt_result.t =
    let listen_address = Unix.(ADDR_INET (inet_addr_loopback, 3000)) in
    Lwt_io.establish_server_with_client_address listen_address (fun _ _ ->
        Lwt_io.printlf "Client connected.%!")
    >>= fun server ->
    Format.eprintf "Started server.@.";
    Lwt_result.return server

  let stop server =
    Lwt_io.shutdown_server server >>= fun () ->
    Lwt_io.eprintlf "Stopped server.%!"

  let component =
    Component.using ~start ~stop ~dependencies:[ Database.component ]
end

let system =
  System.make [ "db", Database.component; "server", WebServer.component ]

let main () =
  System.start () system >>= fun system ->
  match system with
  | Ok system ->
    let forever, waiter = Lwt.wait () in
    Sys.(
      set_signal
        sigint
        (Signal_handle
           (fun _ ->
             Format.eprintf "SIGINT received, tearing down.@.";
             Lwt.async (fun () ->
                 System.stop system >|= fun _stopped_system ->
                 Lwt.wakeup_later waiter ()))));
    forever
  | Error error ->
    Format.eprintf "ERROR: %s@." error;
    exit 1

let () = Lwt_main.run (main ())

License & Copyright

Copyright (c) 2020 António Nuno Monteiro

Archi is distributed under the 3-Clause BSD License, see LICENSE.

Dependencies (3)

  1. hmap
  2. dune >= "1.0"
  3. ocaml >= "4.06"

Dev Dependencies (1)

  1. alcotest with-test

Used by (2)

  1. archi-async < "0.2.0"
  2. archi-lwt < "0.2.0"

Conflicts

None

OCaml

Innovation. Community. Security.