node.js - promise 阻止 Node 进程退出

标签 node.js promise bluebird

我用Bluebird promise写了一个迁移数据的工具,用户可以直接通过node命令触发这个工具。示例:

node migrate.js

问题是,这个 Node 进程完成后将不存在。这是 migrate.js 的主要内容,exec() 函数返回一个 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?了解更多详情。

关于node.js - promise 阻止 Node 进程退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30388082/

相关文章:

javascript - AngularJS $q promise 不会按预期工作

node.js - 尽管成功 echo $variable,NodeJs/etc/environment 仍看不到文件更改

javascript - 从涉及 Promise 的函数返回非 Promise 值

node.js - 在 Node.js 中何时使用 next() 并返回 next()

javascript - 如何向全局范围提供 "this"?

javascript - 不使用 <any> 的 typescript 和解决 promise

javascript - 在node.js中迭代大量异步调用/结果(使用ES6/async/bluebird/generators)?

node.js - 如何在mongodb聚合中将动态值传递给$in

promise - Promise 对象内外的分号

javascript - 当一个 promise 依赖于另一个 promise 时,Bluebird 的 Promise.all() 方法