avatar

后端/轮询实现

// 轮询通过本地文件写入与读取记录标记值
async function readLoopFile() {
let filePath = path.resolve(__dirname, ‘../../../mail-project-upload/polling.txt’);
try {
let res = await isFileExisted(filePath);
if (res) {
let data = fs.readFileSync(filePath, ‘utf-8’);
let lastChar = parseInt(data.charAt(data.length - 1));
if (lastChar < 4) {
lastChar = lastChar + 1;
} else {
lastChar = 1;
}
fs.appendFileSync(filePath, ‘\n’ + lastChar);
return lastChar;
} else {
console.log(‘文件不存在’);
return 1;
}
} catch (error) {
console.log(‘error’, error);
fs.writeFileSync(filePath, 1);
return 1;
}
}
async function isFileExisted(filePath) {
return new Promise(function (resolve, reject) {
fs.exists(filePath, (exists) => {
if (exists) {
resolve(‘文件已存在’);
} else {
reject(‘文件不存在’);
}
});
});
}

文章作者: 小黑
文章链接: http://ynxh.xyz/2023/01/17/后端/轮询实现/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 小黑的小站
打赏
  • 微信
    微信
  • 支付寶
    支付寶
2