Sometimes we need to inspect a lot of systems at once and its annoying to have to wait a long time for a result. What to do?

Jane Street created a library for asynchronous programming called Async which you can read about on the Ocaml Core site. We can use that to speed things up a bit. Say that we need to ping a bunch of systems to make sure that they’re accessible before performing some other checks. We can do something like:

open Core.Std
open Async.Std

module Ashell = struct
  module Shell = Core_extended.Shell
  module Process = Shell.Process

let k_shell_command k f fmt =
    ksprintf (fun command -> k f ( command)) fmt

let sh_test ?true_v =
    Process.test_k (k_shell_command (fun f cmd -> (fun () -> f cmd))) ?true_v

let ping_host = Ashell.sh_test "/bin/ping -c 1 %s >/dev/null 2>&1 || /bin/false"

let ping_all host_list =
   ~f:(fun h -> ping_host h

    | fun p -> printf "%s%sn" h (if p then "" else " (ping failed)"))

      fun () -> shutdown 0

let () =
  ping_all ["node1"; "node2"; "node3"; "node4"];
  never_returns (Scheduler.go())

When Jane Street’s Async_extended library is released you’ll be able to ditch the Ashell module above and use Shell.test in the program.

If it looks daunting then read the introduction linked above and you’ll be able to write faster code for systems administration tasks quickly and safely typed! Awesome!