怎样在小程序里实现标题的更改
588
2022-10-06
两个node服务共同修改一个计数文件,互相监控服务是否停止
node服务A:
const fs = require('fs')let timerlet startValue//监控const toolsMonitor = async (req, res) => { const monitorFilePath = '/temp/log/monitor.json' clearInterval(timer) fs.readFile(monitorFilePath, 'utf-8', function (err, data) { if (err) { console.log(err) } else { let dataObj = eval('(' + data + ')') startValue = dataObj.count } }) let isAlert = false timer = setInterval(() => { fs.readFile(monitorFilePath, 'utf-8', function (err, data) { if (err) { console.log(err) } else { let dataObj = eval('(' + data + ')') dataObj.count = dataObj.count + 1 console.log(dataObj.count, startValue) if (dataObj.count - startValue > 5 && isAlert === false) { console.log('报警') isAlert = true } fs.writeFile( monitorFilePath, JSON.stringify(dataObj, null, 2), { encoding: 'utf8' }, (err) => { if (err) { console.log(err) } } ) } }) }, 1000) res.send({ code: 200, data: {}, message: '监控成功' })}
node服务B:
const fs = require('fs')let timerlet startValue//监控const toolsMonitor = async (req, res) => { const monitorFilePath = '/temp/log/monitor.json' clearInterval(timer) fs.readFile(monitorFilePath, 'utf-8', function (err, data) { if (err) { console.log(err) } else { let dataObj = eval('(' + data + ')') startValue = dataObj.count } }) let isAlert = false timer = setInterval(() => { fs.readFile(monitorFilePath, 'utf-8', function (err, data) { if (err) { console.log(err) } else { let dataObj = eval('(' + data + ')') dataObj.count = dataObj.count - 1 console.log(dataObj.count, startValue) if (dataObj.count - startValue > 5 && isAlert === false) { console.log('报警') isAlert = true } fs.writeFile( monitorFilePath, JSON.stringify(dataObj, null, 2), { encoding: 'utf8' }, (err) => { if (err) { console.log(err) } } ) } }) }, 1000) res.send({ code: 200, data: {}, message: '监控成功' })}
monitor.json:
{ "count": 0}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~