4

I write a tool for migrating data using Bluebird promise, so that user can trigger this tool directly via node command. Example:

node migrate.js

The problem is, this node process will not exist after complete. Here is the content of main migrate.js, the exec() function return a 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);
    });

At the moment, I force it exit by invoke process.exit(0);

The content of migrate.js, some code I can't expose so I remove them out

'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;
Kiet Thanh Vo
  • 399
  • 5
  • 16
  • Does the `new migrate()` call create a child process that needs to be shutdown? – jfriend00 May 22 '15 at 04:03
  • I check the the process list with *ps -u* and see only one node process is running: kiettv 20832 4.9 1.0 1007264 79596 pts/9 Sl+ 11:08 0:01 node migrate.js – Kiet Thanh Vo May 22 '15 at 04:10
  • What does `new migrate()` do? Can you add a reference to that library? – jfriend00 May 22 '15 at 04:14
  • I update the post with the content of **migrate.js** – Kiet Thanh Vo May 22 '15 at 04:31
  • 1
    Somewhere, node.js apparently thinks it has a socket that is still open or a server waiting for incoming requests still running or a timer still to fire. See http://stackoverflow.com/questions/7698834/how-does-a-node-js-process-know-when-to-stop for more info. – jfriend00 May 22 '15 at 05:48
  • you were right, I check the open handle by process._getActiveHandles() and see some socket was opened. It was found out that those socket was open by Knex library, invoke destroy on Knex solve the problem. – Kiet Thanh Vo May 22 '15 at 07:35

1 Answers1

5

Making my comment into an answer since this led you to the solution.

Somewhere, node.js apparently thinks it has a socket that is still open or a server waiting for incoming requests still running or a timer still to fire.

See How does a node.js process know when to stop? for a lot more detail.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979