Loopback script console won't exit

I am writing a Loopback script that should be called by cron.

To get an object app

, I do

var app = require('./server/server');

# Script logic
console.log('done');

      

However, the script doesn't exit as soon as it finishes executing. How do I get him to exit?

Link: http://docs.strongloop.com/display/public/LB/Working+with+LoopBack+objects

+3


source to share


2 answers


Finally, the cause of this problem was found out.

The problem is with the database connection (in my case mongodb via loopback-connector-mongodb ) is still connected.



To disconnect the database connection and subsequently exit the console script

var app = require('./server/server');
app.dataSources.DATASOURCENAME.disconnect();

      

+4


source


I read in some places that the problem is with the http server preventing the script from closing.

I ended up with a module that doesn't even start the http server, I named it loopback-init.js

and I usually import it from migrations and scripts (the important part is the custom callback passed to boot()

):

'use strict';

const Promise = require('bluebird');
const loopback = require('loopback');
const boot = require('loopback-boot');
const logger = require('logger');
const app = loopback();

boot(app, __dirname + '/../server', err => {
    if (err) throw err;

    logger.debug('Loopback initialized.');
    app.start = function() {
        app.close = function(cb) {
            app.removeAllListeners('started');
            app.removeAllListeners('loaded');
            if (cb) cb();
        };
    };
});

const autoMigrate = Promise.promisify(
    app.dataSources.db.automigrate,
    {context: app.dataSources.db}
);

app.autoMigrate = autoMigrate;
module.exports = app;

      



and my db-migrate scripts look like this:

'use strict';

var dbm;
var type;
var seed;

/**
  * We receive the dbmigrate dependency from dbmigrate initially.
  * This enables us to not have to rely on NODE_PATH.
  */
exports.setup = function(options, seedLink) {
  dbm = options.dbmigrate;
  type = dbm.dataType;
  seed = seedLink;
};

exports.up = function(db) {
    const lb = require('loopback-init');
    return lb.autoMigrate('Item')
        .then(lb.close, lb.close);
};

exports.down = function(db) {
    return db.dropTable('item');
};

exports._meta = {
  "version": 1
};

      

0


source







All Articles