nswbmw / koa-mongo

MongoDB middleware for koa, support connection pool.
145 stars 31 forks source link

question : when do I close the database in the example #3

Closed ghost closed 9 years ago

ghost commented 9 years ago

Hello,

I saw the example on the front-page. But I wonder where and when I close the database ?

Roelof

yorkie commented 9 years ago

I guess the real closing action would be self-managed by the module generic-pool, but we release the connection in the end of every context at https://github.com/MangroveTech/koa-mongo/blob/master/index.js#L56

ghost commented 9 years ago

Thanks,

Then I wonder why this script :


var koa = require('koa');
var mongo = require('koa-mongo');

var app = koa();

app.use(mongo());
app.use(function* (next) {
  var query = {};
    var projection = {'State':1, 'Temperature':1};

    var cursor = db.collection('data').find(query, projection);

    // Sort by state and then by temperature (decreasing)
    cursor.sort([['State',1], ['Temperature',-1]]);

    var state = ''; // initialize to dummy value
    var operator = {'$set':{month_high:true}};

    cursor.each(function(err, doc) {
        if (err) throw err;

        if (doc === null) {
            return db.close();
        } else if (doc.State !== state) {
            // first record for each state is the high temp one
            state = doc.State;

            db.collection('data').update({'_id':doc._id}, operator, function(err, updated) {
                if (err) throw err;
            });
        }
    });
});
app.listen(3000);

never gets ended.

yorkie commented 9 years ago

Could you please print the mongo log? and by the way, you should not call .close() by yourself

ghost commented 9 years ago

I think you mean this log:


2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten] MongoDB starting : pid=708 port=27017 dbpath=/home/codio/.parts/var/mongodb 64-bit host=orbit-couple                                                                               
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten]                                                                                                                                                                                    
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.                                                                                                               
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'                                                                                                                                         
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten]                                                                                                                                                                                    
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.                                                                                                                
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'                                                                                                                                         
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten]                                                                                                                                                                                    
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten] db version v3.0.0                                                                                                                                                                  
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten] git version: a841fd6394365954886924a35076691b4d149168                                                                                                                              
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten] build info: Linux build15.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49                                                  
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten] allocator: tcmalloc                                                                                                                                                                
2015-06-11T07:35:31.326+0000 I CONTROL  [initandlisten] options: { config: "/home/codio/.parts/etc/mongodb.conf", net: { bindIp: "127.0.0.1" }, processManagement: { fork: true, pidFilePath: "/home/codio/.parts/var/mongodb/mongod.pid" }
, storage: { dbPath: "/home/codio/.parts/var/mongodb", mmapv1: { smallFiles: true } }, systemLog: { destination: "file", logAppend: true, path: "/home/codio/.parts/var/log/mongodb/mongod.log" } }                                        
2015-06-11T07:35:31.345+0000 I JOURNAL  [initandlisten] journal dir=/home/codio/.parts/var/mongodb/journal                                                                                                                                 
2015-06-11T07:35:31.345+0000 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed                                                                                                                             
2015-06-11T07:35:31.488+0000 I JOURNAL  [durability] Durability thread started                                                                                                                                                             
2015-06-11T07:35:31.489+0000 I JOURNAL  [journal writer] Journal writer thread started                                                                                                                                                     
2015-06-11T07:35:31.491+0000 I INDEX    [initandlisten] allocating new ns file /home/codio/.parts/var/mongodb/local.ns, filling with zeroes...                                                                                             
2015-06-11T07:35:31.561+0000 I STORAGE  [FileAllocator] allocating new datafile /home/codio/.parts/var/mongodb/local.0, filling with zeroes...                                                                                             
2015-06-11T07:35:31.561+0000 I STORAGE  [FileAllocator] creating directory /home/codio/.parts/var/mongodb/_tmp                                                                                                                             
2015-06-11T07:35:31.563+0000 I STORAGE  [FileAllocator] done allocating datafile /home/codio/.parts/var/mongodb/local.0, size: 16MB,  took 0 secs                                                                                          
2015-06-11T07:35:31.566+0000 I NETWORK  [initandlisten] waiting for connections on port 27017                                                                                                                                              
2015-06-11T07:47:32.302+0000 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:53702 #1 (1 connection now open)                                                                                                                
2015-06-11T07:47:32.355+0000 I INDEX    [conn1] allocating new ns file /home/codio/.parts/var/mongodb/weather.ns, filling with zeroes...                                                                                                   
2015-06-11T07:47:32.425+0000 I STORAGE  [FileAllocator] allocating new datafile /home/codio/.parts/var/mongodb/weather.0, filling with zeroes...                                                                                           
2015-06-11T07:47:32.426+0000 I STORAGE  [FileAllocator] done allocating datafile /home/codio/.parts/var/mongodb/weather.0, size: 16MB,  took 0.001 secs                                                                                    
2015-06-11T07:47:32.447+0000 I NETWORK  [conn1] end connection 127.0.0.1:53702 (0 connections now open)                                                                                                                                    
2015-06-11T07:50:47.112+0000 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:53987 #2 (1 connection now open)                                                                                                                
2015-06-11T07:50:47.123+0000 I NETWORK  [conn2] end connection 127.0.0.1:53987 (0 connections now open)                                                                                                                                    
2015-06-11T07:50:47.143+0000 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:53988 #3 (1 connection now open)                                                                                                                
2015-06-11T07:51:17.178+0000 I NETWORK  [conn3] end connection 127.0.0.1:53988 (0 connections now open)                                                                                                                                    
2015-06-11T07:51:17.181+0000 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:54040 #4 (1 connection now open)                                                                                                                
2015-06-11T07:51:17.182+0000 I NETWORK  [conn4] end connection 127.0.0.1:54040 (0 connections now open)                                                                                                                                    
2015-06-11T07:51:17.184+0000 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:54041 #5 (1 connection now open)                                                                                                                
2015-06-11T07:51:22.706+0000 I NETWORK  [conn5] end connection 127.0.0.1:54041 (0 connections now open)   

~~`
yorkie commented 9 years ago

Yeah, @roelof1967 we could get the last line of the log at MongoDB:

[conn5] end connection 127.0.0.1:54041 (0 connections now open)  

So the connection is closed, what did you mean by "never ended"?

ghost commented 9 years ago

I mean with never ended that I see a blinking cursor and nothing more. I was expecting to see the prompt again.

yorkie commented 9 years ago

Oh, that doesn't mean you didn't close your mongo connections, because you was starting up a server with koa, so your node instance was waiting for requests, that's why you would see cursor on your terminal, that is correct behavior as you described, so I'm going to close the issue, thanks for your feedback :)