Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process not exiting? #405

Closed
npow opened this issue Jul 1, 2014 · 11 comments
Closed

Process not exiting? #405

npow opened this issue Jul 1, 2014 · 11 comments
Labels

Comments

@npow
Copy link

npow commented Jul 1, 2014

Hi,

I'm trying to use bookshelf.js to populate my database via a nodejs script (executed in a cronjob). However I noticed that the process doesn't exit even after the records are successfully written to the database. Is there something I'm supposed to do to make it exit?

I've tried the following simple code and it doesn't exit either:

var knex = require('knex')({
  client: 'postgresql',
  connection: {
    host     : '127.0.0.1',
    user     : 'npow',
    password : '',
    database : 'miserly',
    charset  : 'utf8'
  }
});

var bookshelf = require('bookshelf')(knex);
console.log('here');
@johanneslumpe
Copy link
Contributor

It doesn't exit because Knex keeps the connection open. You will have to destroy/close it manually through the Knex connection pool. Don't have a code sample right now but I will add one when I'm at work. Or you can search trough the issues, because this has come up before :)

Sent from my iPhone

On Jul 1, 2014, at 7:17 AM, npow notifications@github.com wrote:

Hi,

I'm trying to use bookshelf.js to populate my database via a nodejs script (executed in a cronjob). However I noticed that the process doesn't exit even after the records are successfully written to the database. Is there something I'm supposed to do to make it exit?

I've tried the following simple code and it doesn't exit either:

var knex = require('knex')({
client: 'postgresql',
connection: {
host : '127.0.0.1',
user : 'npow',
password : '',
database : 'miserly',
charset : 'utf8'
}
});

var bookshelf = require('bookshelf')(knex);
console.log('here');

Reply to this email directly or view it on GitHub.

@tgriesser
Copy link
Member

Yeah, @carldanley just brought this up to me tonight as well, I guess this might be a good FAQ add.

But yeah, calling knex.destroy(cb) should do it. Or process.exit which isn't as graceful but gets the job done.

The way to think about it is that opening pooled database connections is kind of like opening a new http server with .createServer... it won't exit until you tell it to, so you should just tell it to explicitly when your script is finished.

@npow
Copy link
Author

npow commented Jul 1, 2014

Thanks for the info! Now my problem is figuring out when to call destroy(). I have the following code which is rather ugly. Is there a better way to do it?

var num = 5;
var num_saved = 0;
for (var i = 0; i < num; ++i) {
  Store.forge({ ... }).save().then(function () {
    num_saved++;
    if (num_saved === num) {
      knex.destroy(function () {});
    }
  });
}

@johanneslumpe
Copy link
Contributor

How about something along these lines:

var num = 5;
var saves = [];

for (var i = 0; i < num; ++i) {
    saves.push(Store.forge({ ... }).save());
}

Promise.all(saves)
.then(function () {
    return knex.destroy();
})
.then(function () {
    console.log('all done');
});

@npow
Copy link
Author

npow commented Jul 1, 2014

Thanks, that would do the trick!

@npow npow closed this as completed Jul 1, 2014
@johanneslumpe
Copy link
Contributor

You're welcome :)

@matanox
Copy link

matanox commented Aug 18, 2014

+1

could be mentioned in the api docs that the connection pool makes the process eternal unless destroyed

@bendrucker
Copy link
Member

@matanster It is mentioned there http://knexjs.org/#Installation-pooling

@matanox
Copy link

matanox commented Aug 18, 2014

@bendrucker I think it kind of implies the opposite by saying "if you ever need to...". May have missed a word somewhere... sorry for the hassle.

@bendrucker
Copy link
Member

Missed the second half of your comment. I think the note about how to destroy the pool is sufficient. I think it's reasonable to expect that users understand that open sockets cause processes to stay open. Plenty of people won't even need to think about this if they're using a CLI to run tests that automatically exits the process (like Mocha's CLI). The only case where the average person might need to destroy the pool is for tests.

@kjs3
Copy link

kjs3 commented Jan 21, 2016

This didn't seem to cause other people here any issue but I wasn't able to get my js task to exit by merely requireing knex and calling .destroy(). I had to get the bookshelf instance used in my model files and access knex through that. So the following is for anyone else who was stumped by this.

process.exit() worked perfectly well but not closing things out the "right" way was bugging me.

// knexfile.js

module.exports = {
  test: {
    // stuff
  },

  development: {
    // stuff
  },

  production: {
    // stuff
  }
}
// bookshelf.js

var dbConfig = require('./knexfile')[process.env.NODE_ENV]
var knex = require('knex')(dbConfig)

module.exports = require('bookshelf')(knex)
// some js task file
// i.e. not a server and I want this to exit

var bookshelf = require('./bookshelf')
var User = require('./models/user.js') // <- this file also requires my bookshelf module to create a model

var cannedUsers = [
  {name: 'foo', email: 'foo@example.com'},
  {name: 'bar', email: 'bar@example.com'},
  {name: 'baz', email: 'baz@example.com'}
]

var userPromisesArray = cannedUsers.map(user => {
  return new User(user).save()
    .then((user) => {
      console.log('User created: ', user.get('name'))
    })
})

Promise.all(userPromisesArray)
.then(() => {
  return User.count()
})
.then(count => {
  console.log(count + ' users created')
  return bookshelf.knex.destroy()
})
.then(()=>{
  console.log('Bye!')
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants