我正在尝试使用电子开发桌面应用程序,并且我想在进程终止(即Ctrl + C)之前运行一个更新查询(用于更新字段logout_time)。在我当前的实现中,该应用程序捕获了进程的终止,但未按预期运行更新。
我有一个清理代码,当用户执行Ctrl + C时会执行
routes / index.js
let Hardware = require('../models/Hardware');
var cleanup = require('../cleanup').Cleanup(myCleanup);
function myCleanup() {
console.log('App specific cleanup code...');
var now = new Date();
// console.log(now)
Hardware.findOneAndUpdate({uuid: "4C4C4544-0056-5810-8054-C4C04F595A31"}, {logout_time : now },
function(err, hardware){
console.log('trigger on close')
pusher.trigger('my-hardware', 'my-cast', {
hardware:hardware
});
})
}
cleanup.js
exports.Cleanup = function Cleanup(callback) {
// attach user callback to the process event emitter
// if no callback, it will still exit gracefully on Ctrl-C
callback = callback || noOp;
process.on('cleanup',callback);
// do app specific cleaning before exiting
process.on('exit', function () {
process.emit('cleanup');
});
// catch ctrl+c event and exit normally
process.on('SIGINT', function () {
console.log('Ctrl-C...');
process.exit(2);
});
//catch uncaught exceptions, trace, then exit normally
process.on('uncaughtException', function(e) {
console.log('Uncaught Exception...');
console.log(e.stack);
process.exit(99);
});
};
感谢任何替代方案/解决方案,谢谢!
预期结果: 当我用Ctrl + C终止服务器时,它应在此记录上运行FindOneAndUpdate并用当前时间更新logout_time字段。