MomsFriendlyDevCo / Doop

The MEVN micro-framework used by MomsFriendlyDevCo
4 stars 5 forks source link

Problems with gulp db and promises #7

Closed mryellow closed 4 years ago

mryellow commented 4 years ago

We never progress past .then(()=> app.emit('dbInit')) in db/db.gulp.js so postSchemas is never emitted.

app.emit returns a promise however the chain is never resolved, consequently app.emit('dbInit') remains unresolved and fails to progress`.

Monoxide diff: https://github.com/hash-bang/Monoxide/compare/caa263108a02c640b17243e30c095e817cb664de...master

It appears that the difference is on listeners used to simply attach the listener but now they're also waiting for the handler to resolve it's promise.

mryellow commented 4 years ago

For instance db/db.doop resolves app.emit('dbInit') when these app.on() handlers are detached from the promise chain.

    .then(()=> {
        app.on('postSchemas', ()=> {
            app.log('Loaded', app.log.colors.cyan(Object.keys(monoxide.models).length), 'DB schemas:', Object.keys(monoxide.models).map(m => app.log.colors.cyan(m)).join(', '))
            Object.assign(app.db, monoxide.models);
            global.db = app.db;
        })
    })
    .then(()=> {
        app.on('exit', ()=> monoxide.disconnect());
    })
mryellow commented 4 years ago
diff --git a/db/db.doop b/db/db.doop
index 6173240..05086b8 100644
--- a/db/db.doop
+++ b/db/db.doop
@@ -6,10 +6,17 @@ exports = ()=> Promise.resolve()
        .then(()=> app.log('Connecting to', app.log.colors.cyan(app.config.mongo.uri)))
        .then(()=> promisify(monoxide.use)(app.config.mongo.plugins)) // Setup plugins - NOTE that since we've not set up the promises plugin yet this needs to be promisified
        .then(()=> monoxide.connect(app.config.mongo.uri, app.config.mongo.options))
-       .then(()=> app.on('postSchemas', ()=> {
-               app.log('Loaded', app.log.colors.cyan(Object.keys(monoxide.models).length), 'DB schemas:', Object.keys(monoxide.models).map(m => app.log.colors.cyan(m)).join(', '))
-               Object.assign(app.db, monoxide.models);
-               global.db = app.db;
-       }))
-       .then(()=> app.on('exit', ()=> monoxide.disconnect()))
+       .then(()=> app.log('Attaching listener: postSchemas'))
+       .then(()=> {
+               app.on('postSchemas', ()=> {
+                       app.log('Loaded', app.log.colors.cyan(Object.keys(monoxide.models).length), 'DB schemas:', Object.keys(monoxide.models).map(m => app.log.colors.cyan(m)).join(', '))
+                       Object.assign(app.db, monoxide.models);
+                       global.db = app.db;
+               })
+       })
+       .then(()=> app.log('Attaching listener: exit'))
+       .then(()=> {
+               app.on('exit', ()=> monoxide.disconnect());
+       })
+       .then(()=> app.log('db.doop completed.'));
 </server>
diff --git a/db/db.gulp.js b/db/db.gulp.js
index c0e91da..cbf1fa5 100644
--- a/db/db.gulp.js
+++ b/db/db.gulp.js
@@ -10,10 +10,12 @@ gulp.task.once('load:app.db', 'load:app', ()=>
                })
                .then(()=> hasLoaded = true)
                .then(()=> app.emit('dbInit'))
-               .then(()=> gulp.on('finish', ()=> // Clean up the database connection when we finish
-                       monoxide.disconnect()
-                               .then(()=> gulp.log('DB Disconnected'))
-               ))
+               .then(()=> {
+                       gulp.on('finish', ()=> // Clean up the database connection when we finish
+                               monoxide.disconnect()
+                                       .then(()=> gulp.log('DB Disconnected'))
+                       );
+               })
                .then(()=> app.emit('dbMiddleware'))
                .then(()=> app.emit('preSchemas'))
                .then(()=> app.emit('schemas'))
(END)
mryellow commented 4 years ago

I note that eventer is the change.

https://github.com/MomsFriendlyDevCo/eventer/commit/cd08f88eb562255a2cf319bc40cfa6bfb7c86773

hash-bang commented 4 years ago

Disabled promise return by default in latest eventer.