Skip to content

Instantly share code, notes, and snippets.

@whitetigle
Created July 15, 2019 16:56
Show Gist options
  • Save whitetigle/a2f112c6a19958e6011d1e4b39675c2a to your computer and use it in GitHub Desktop.
Save whitetigle/a2f112c6a19958e6011d1e4b39675c2a to your computer and use it in GitHub Desktop.
sample cluster that spawns new workers on crash
module Cluster
open Fable.Core
open Fable.Core.DynamicExtensions
open Node.Base
open Node.Http
open Node.Cluster
let cluster = Node.Api.cluster
let pid = Node.Api.``process``.pid
let mutable count = 0
if cluster.isMaster then
JS.console.log (sprintf "Master %.0f is running" pid)
let spawnWorker _ =
let worker = cluster.fork()
let id = worker.``process``.pid
JS.console.log(sprintf "SPAWNED %.0f started" id)
// creating workers
let numCPUS = Node.Api.os.cpus() |> Seq.length
[1..numCPUS] |> Seq.iter spawnWorker
let onExit (deadWorker:Worker) code signal =
let id = deadWorker.``process``.pid
JS.console.log(sprintf "DEAD: %.0f " id)
spawnWorker()
cluster.on("exit", onExit) |> ignore
else
// our http servers
let http = Node.Api.http
let listener (req:IncomingMessage) (res:ServerResponse) =
if count < 3 then
res.writeHead 200
res.``end`` "Fable on a Node.js Cluster!!"
count <- count + 1
else
count <- 0
raise(System.Exception"Bye bye!")
http.createServer(listener).listen 8000 |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment