监控blockscout服务脚本
const https = require("https")
const { exec } = require('child_process');

// 下面两个变量根据情况修改
const cwd = "/home/ubuntu/analysis" // docker-compose.yml文件所在目录
const url = "https://analysis.nelo.world/tokens?type=JSON" // 要请求的blockscout服务地址

Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1,                 //月份   
    "d+": this.getDate(),                    //日   
    "h+": this.getHours(),                   //小时   
    "m+": this.getMinutes(),                 //分   
    "s+": this.getSeconds(),                 //秒   
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
    "S": this.getMilliseconds()             //毫秒   
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

let loading = false
let count = 0

const reload = () => {
  console.log("reload time: ", new Date().format("yyyy-MM-dd hh:mm:ss"), ", reload count: ", count++)
  exec("sudo docker-compose down &&  sudo docker-compose up -d blockscout", { cwd }, (error, stdout, stderr) => {
    if (error) {
      console.error(`reload exec error: ${error}`);
    } else {
      console.log(`reload stdout: ${stdout}`);
      console.error(`reload stderr: ${stderr}`);
    }
    setTimeout(() => { loading = false }, 60000)
  })
}

const watch = () => {
  if (!loading) {
    loading = true
    https.get(url, { timeout: 6000 }, (res) => {
      if (res.statusCode !== 200) {
        console.log("http status code:", res.statusCode)
        reload()
      } else {
        loading = false
      }
    }).on('error', (e) => {
      console.log(`http error: ${e.message}`);
      reload()
    });
  }
}

watch()
setInterval(watch, 10000)

完事之后使用pm2启动,启动命令:pm2 start watch.js --name watch-blockscout

暂无评论

发送评论 编辑评论


				
上一篇
下一篇