liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

service.nix (1173B)


      1 {
      2   liminix,
      3   lib,
      4   lim,
      5   s6,
      6 }:
      7 {
      8   service,
      9   interval,
     10   threshold,
     11   healthCheck,
     12 }:
     13 let
     14   inherit (liminix.services) oneshot longrun;
     15   inherit (builtins) toString;
     16   inherit (service) name;
     17   checker =
     18     let
     19       name' = "check-${name}";
     20     in
     21     longrun {
     22       name = name';
     23       run = ''
     24         fails=0
     25         echo waiting for /run/service/${name}
     26         ${s6}/bin/s6-svwait -U /run/service/${name} || exit
     27         while sleep ${toString interval} ; do
     28           ${healthCheck}
     29           if test $? -gt 0; then
     30             fails=$(expr $fails + 1)
     31           else
     32             fails=0
     33           fi
     34           echo fails $fails/${toString threshold} for ${name}
     35           if test "$fails" -gt "${toString threshold}" ; then
     36             echo time to die
     37             ${s6}/bin/s6-svc -r /run/service/${name}
     38             echo bounced
     39             fails=0
     40             echo waiting for /run/service/${name}
     41             ${s6}/bin/s6-svwait -U /run/service/${name}
     42           fi
     43         done
     44       '';
     45     };
     46 in
     47 service.overrideAttrs (o: {
     48   buildInputs = (lim.orEmpty o.buildInputs) ++ [ checker ];
     49   dependencies = (lim.orEmpty o.dependencies) ++ [ checker ];
     50 })