-
Notifications
You must be signed in to change notification settings - Fork 569
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
Comments
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
|
Yeah, @carldanley just brought this up to me tonight as well, I guess this might be a good FAQ add. But yeah, calling The way to think about it is that opening pooled database connections is kind of like opening a new http server with |
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?
|
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');
}); |
Thanks, that would do the trick! |
You're welcome :) |
+1 could be mentioned in the api docs that the connection pool makes the process eternal unless destroyed |
@matanster It is mentioned there http://knexjs.org/#Installation-pooling |
@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. |
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. |
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
// 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!')
}) |
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:
The text was updated successfully, but these errors were encountered: