Hopac.Plus


Supervision example

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
#r "Hopac.Core"
#r "Hopac"
#r "Hopac.Platform"
#r "Hopac.Plus"

open Hopac
open Hopac.Infixes
open Hopac.Plus.Supervision
open Hopac.Plus.Extensions

let goodDaemon name will : Job<unit> =
  let rec init () =
    printfn "%s: Starting" name
    Will.latest will
    |> OptionJob.orDefault 0
    |> Job.bind loop
  and loop state =
    printfn "%s: loop %i" name state
    timeOutMillis 10
      |> Alt.afterJob (fun () ->
        let newState = state + 1
        let nextLoop = if newState < 100 then loop newState else Alt.unit
        Will.update will newState
        >>=. nextLoop
      )
  init ()

let badDaemon name (rand : System.Random) will : Job<unit> =
  let rec init () =
    printfn "%s: Starting" name
    Will.latest will
    |> OptionJob.orDefault 0
    |> Job.bind loop
  and loop state =
    printfn "%s: loop %i" name state
    timeOutMillis 10
      |> Alt.afterJob (fun () ->
        let newState = state + 1
        let nextLoop =
          if newState < 100 then
            if rand.Next(0,5) = 0 then
              throw state
            else
              loop newState
          else
            Job.unit
        Will.update will newState
        >>=. nextLoop
      ) |> asJob
  and throw state =
    Job.delay <| fun () ->
      printfn "%s: Failed" name
      if state < 75 then
        job { raise (exn "Sadness") }
      else
        job { raise (exn "Super Sad") }
  init ()

let rand = System.Random()

let startJ xJ = Promise.start xJ |> run >>- printfn "%A" |> start

// Examples of starting supervised jobs

let retryThrice = Policy.retry 3u
let backoffTo8 = Policy.exponentialBackoff 1000u 2u 8000u 8u

startJ <| Job.superviseWithWill Policy.restart (goodDaemon "happy")
startJ <| Job.superviseWithWill Policy.terminate (badDaemon "sad" rand)
startJ <| Job.superviseWithWill retryThrice (badDaemon "retry" rand)
startJ <| Job.superviseWithWill backoffTo8 (badDaemon "retry" rand)
namespace Hopac
Multiple items
module Hopac

from Hopac

--------------------
namespace Hopac
module Infixes

from Hopac
namespace Hopac.Plus
namespace Hopac.Plus.Supervision
namespace Hopac.Plus.Extensions
val goodDaemon : name:string -> will:Will<int> -> Job<unit>

Full name: Supervision.goodDaemon
val name : string
val will : Will<int>
type Job<'T> =

Full name: Hopac.Job<_>
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
val init : (unit -> Job<unit>)
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Multiple items
module Will

from Hopac.Plus.Supervision

--------------------
type Will<'a>

Full name: Hopac.Plus.Supervision.Will<_>
val latest : Will<'a> -> Alt<'a option>

Full name: Hopac.Plus.Supervision.Will.latest
module OptionJob

from Hopac.Plus.Extensions
val orDefault : x:'x -> xOJ:Job<'x option> -> Job<'x>

Full name: Hopac.Plus.Extensions.OptionJob.orDefault
val bind : ('x -> #Job<'y>) -> Job<'x> -> Job<'y>

Full name: Hopac.Job.bind
val loop : (int -> Alt<unit>)
val state : int
val timeOutMillis : int -> Alt<unit>

Full name: Hopac.Hopac.timeOutMillis
type Alt<'T> =
  inherit Job<'T>

Full name: Hopac.Alt<_>
val afterJob : ('x -> #Job<'y>) -> Alt<'x> -> Alt<'y>

Full name: Hopac.Alt.afterJob
val newState : int
val nextLoop : Alt<unit>
Multiple items
val unit : Alt<unit>

Full name: Hopac.Plus.Extensions.Alt.unit

--------------------
val unit : unit -> Alt<unit>

Full name: Hopac.Alt.unit
val update : Will<'a> -> 'a -> Alt<unit>

Full name: Hopac.Plus.Supervision.Will.update
val badDaemon : name:string -> rand:System.Random -> will:Will<int> -> Job<unit>

Full name: Supervision.badDaemon
val rand : System.Random
namespace System
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
System.Random() : unit
System.Random(Seed: int) : unit
val loop : (int -> Job<unit>)
val nextLoop : Job<unit>
System.Random.Next() : int
System.Random.Next(maxValue: int) : int
System.Random.Next(minValue: int, maxValue: int) : int
val throw : (int -> Job<unit>)
Multiple items
val unit : Job<unit>

Full name: Hopac.Plus.Extensions.Job.unit

--------------------
val unit : unit -> Job<unit>

Full name: Hopac.Job.unit
val asJob : Job<'x> -> Job<'x>

Full name: Hopac.Hopac.asJob
val delay : (unit -> #Job<'y>) -> Job<'y>

Full name: Hopac.Job.delay
val job : JobBuilder

Full name: Hopac.Hopac.job
val raise : exn:System.Exception -> 'T

Full name: Microsoft.FSharp.Core.Operators.raise
type exn = System.Exception

Full name: Microsoft.FSharp.Core.exn
val rand : System.Random

Full name: Supervision.rand
val startJ : xJ:Job<'a> -> unit

Full name: Supervision.startJ
val xJ : Job<'a>
Multiple items
type Promise<'T> =
  inherit Alt<'T>
  new : unit -> Promise<'T> + 3 overloads
  member Full : bool

Full name: Hopac.Promise<_>

--------------------
Promise() : unit
Promise(tJ: Job<'T>) : unit
Promise(value: 'T) : unit
Promise(e: exn) : unit
val start : Job<'x> -> Job<Promise<'x>>

Full name: Hopac.Promise.start
val run : Job<'x> -> 'x

Full name: Hopac.Hopac.run
val start : Job<unit> -> unit

Full name: Hopac.Hopac.start
val retryThrice : Policy

Full name: Supervision.retryThrice
Multiple items
module Policy

from Hopac.Plus.Supervision

--------------------
type Policy =
  | Always of FailureAction
  | DetermineWith of (exn -> FailureAction)
  | DetermineWithJob of (exn -> Job<FailureAction>)

Full name: Hopac.Plus.Supervision.Policy
val retry : maxRetries:uint32 -> Policy

Full name: Hopac.Plus.Supervision.Policy.retry
val backoffTo8 : Policy

Full name: Supervision.backoffTo8
val exponentialBackoff : initialDelay:uint32 -> multiplier:uint32 -> maxDelay:uint32 -> maxRetries:uint32 -> Policy

Full name: Hopac.Plus.Supervision.Policy.exponentialBackoff
val superviseWithWill : p:Policy -> w2xJ:(Will<'a> -> #Job<'x>) -> SupervisedJob<'x>

Full name: Hopac.Plus.Supervision.Job.superviseWithWill
val restart : Policy

Full name: Hopac.Plus.Supervision.Policy.restart
val terminate : Policy

Full name: Hopac.Plus.Supervision.Policy.terminate
Fork me on GitHub