Advertisement
Guest User

heartbeat-by-request

a guest
Feb 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const axios = require('axios');
  2. const fork = require("child_process").fork;
  3. var server, heartbeat;
  4. const time = 20000;
  5.  
  6. function startServer() {
  7.   console.log(new Date() + " :: starting server");
  8.   server = fork("./server.js");
  9.  
  10.   server.on("close", code => {
  11.     console.log(new Date() + " :: restart server");
  12.     startServer();
  13.   });
  14.  
  15.   setTimeout(checkHeartbeat, time);
  16. }
  17.  
  18. function checkHeartbeat() {
  19.   const port = process.env.PORT || 3000;
  20.   axios.get(`http://localhost:${port}`,{
  21.     validateStatus: function (status) {
  22.       return status <= 500;
  23.     }
  24.   })
  25.   .then(function (response) {
  26.     setTimeout(checkHeartbeat, time);
  27.   })
  28.   .catch(function (error) {
  29.     console.log(new Date() + " :: server looks stuck, killing now");
  30.     server.kill();
  31.   })
  32. }
  33.  
  34. startServer();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement