承诺阻止节点进程退出
Promise prevent node process from exiting
我用Bluebird Promise写了一个迁移数据的工具,用户可以直接通过node命令触发这个工具。示例:
node migrate.js
问题是,这个节点进程完成后将不存在。这里是mainmigrate.js的内容,exec()函数return一个promise.
var migrate = require('../elasticsearch/migrate');
var setting = require('../../config/setting');
var cmd = new migrate(setting.NewConfig.search, true);
cmd.exec()
.then(function () {
console.info('Operation completed');
})
.catch(function (err) {
console.error(err);
});
目前,我通过调用 process.exit(0);
强制退出
migrate.js的内容,有些代码我不能公开,所以我把它们去掉了
'use strict';
var Promise = require('bluebird');
var request = Promise.promisifyAll(require('request'));
var _ = require('lodash');
var index = require('./mapping.json');
var Schema = require('../../app/database/mysql/model');
var common = require('../../utils/common');
var client = require('../../utils/search');
var logger = require('../../utils/logger');
function Migrate(opts, enable) {
this.buildInLogger = enable == undefined;
this.opts = opts || {};
// Sensitive code
// ....
this.options = {
url: this.opts.protocol + '://' + this.opts.host + '/' + this.opts.index,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}
}
Migrate.prototype.initElasticsearch = function () {
var self = this;
var options = _.clone(this.options);
return request.delAsync(options)
.then(function () {
options = _.clone(self.options);
options.json = index;
return request.putAsync(options);
})
.then(function () {
if (self.buildInLogger) {
logger.info('Init new index successfully');
}
else {
console.log('Init new index successfully');
}
})
.catch(function (err) {
if (self.buildInLogger) {
logger.error(err);
}
else {
console.error(err);
}
});
};
Migrate.prototype.exec = function () {
var self = this;
return this.initElasticsearch()
.then(function(){
// Sensitive code which also return a promise
// ....
})
.catch(function (err) {
if (self.buildInLogger) {
logger.error(err);
}
else {
console.error(err);
}
})
};
module.exports = Migrate;
将我的评论作为答案,因为这会引导您找到解决方案。
在某个地方,node.js 显然认为它有一个仍处于打开状态的套接字或仍在等待传入请求的服务器 运行 或仍在触发的计时器。
有关更多详细信息,请参阅 How does a node.js process know when to stop?。
我用Bluebird Promise写了一个迁移数据的工具,用户可以直接通过node命令触发这个工具。示例:
node migrate.js
问题是,这个节点进程完成后将不存在。这里是mainmigrate.js的内容,exec()函数return一个promise.
var migrate = require('../elasticsearch/migrate');
var setting = require('../../config/setting');
var cmd = new migrate(setting.NewConfig.search, true);
cmd.exec()
.then(function () {
console.info('Operation completed');
})
.catch(function (err) {
console.error(err);
});
目前,我通过调用 process.exit(0);
强制退出migrate.js的内容,有些代码我不能公开,所以我把它们去掉了
'use strict';
var Promise = require('bluebird');
var request = Promise.promisifyAll(require('request'));
var _ = require('lodash');
var index = require('./mapping.json');
var Schema = require('../../app/database/mysql/model');
var common = require('../../utils/common');
var client = require('../../utils/search');
var logger = require('../../utils/logger');
function Migrate(opts, enable) {
this.buildInLogger = enable == undefined;
this.opts = opts || {};
// Sensitive code
// ....
this.options = {
url: this.opts.protocol + '://' + this.opts.host + '/' + this.opts.index,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}
}
Migrate.prototype.initElasticsearch = function () {
var self = this;
var options = _.clone(this.options);
return request.delAsync(options)
.then(function () {
options = _.clone(self.options);
options.json = index;
return request.putAsync(options);
})
.then(function () {
if (self.buildInLogger) {
logger.info('Init new index successfully');
}
else {
console.log('Init new index successfully');
}
})
.catch(function (err) {
if (self.buildInLogger) {
logger.error(err);
}
else {
console.error(err);
}
});
};
Migrate.prototype.exec = function () {
var self = this;
return this.initElasticsearch()
.then(function(){
// Sensitive code which also return a promise
// ....
})
.catch(function (err) {
if (self.buildInLogger) {
logger.error(err);
}
else {
console.error(err);
}
})
};
module.exports = Migrate;
将我的评论作为答案,因为这会引导您找到解决方案。
在某个地方,node.js 显然认为它有一个仍处于打开状态的套接字或仍在等待传入请求的服务器 运行 或仍在触发的计时器。
有关更多详细信息,请参阅 How does a node.js process know when to stop?。