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

If we run boot process in a context, getCurrentContext fails some times when connector is still connecting to mongodb #2358

Closed
pkgulati opened this issue May 20, 2016 · 6 comments
Assignees
Labels

Comments

@pkgulati
Copy link

We have tried both approach calling boot in runInContext as well as calling some individual boot scripts in runInContext.
Why we are using getCurrentContext in boot?
Actually we have some logic in 'before save" mixins which works on context, we are basically trying to load some data in boot script for different context.

@gunjpan gunjpan self-assigned this May 26, 2016
@gunjpan
Copy link
Contributor

gunjpan commented May 26, 2016

@pkgulati : Hi, I don't understand what you're trying to do. Could you please provide a sample code/repo so that I can understand well what you're trying to achieve and can help you better.

@pkgulati
Copy link
Author

// in server.js 
var ns = loopback.createContext('loopback');

loopback.runInContext(function(ns, domain) {

console.log('setting foo=bar before boot');
ns.set('foo', 'bar');
console.log('check after set ', loopback.getCurrentContext().get('foo'));

// boot scripts mount components like REST API
boot(app, __dirname, function() {
    console.log('boot completed');
});

 });

// timer.js boot script 
var path = require('path');
var loopback = require('loopback');
var async = require('async');

module.exports = function (app, next) {
console.log('in boot script timer.js');
var ctx = loopback.getCurrentContext();
if (ctx) {
    var a = ctx.get('foo');
    console.log('value of foo ', a);
} else {
    console.log('loopback context is null ');
}

setTimeout(function () {
    console.log('timer occured');
    var ctx = loopback.getCurrentContext();
    if (ctx) {
        console.log('loopback context is there');
        var a = ctx.get('foo');
        console.log('value of foo ', a);
    } else {
        console.log('loopback context is null ');
    }
    next();
}, 5000);
}

@pkgulati
Copy link
Author

Other possibility is we write a simple mocha test script , so basically getCurrentContext works for http request but for mocha or boot scripts it does not work

  myapp.boot(app, options, function fnboot () {
       app.start();
       app.emit('started');
 });

var ns = loopback.createContext('loopback');
loopback.runInContext(function (ns, domain) {

var ctx = {
    ctx: {
        foo: 'bar'
    }
};
ns.set('callContext', ctx);
console.log('context 1 ', loopback.getCurrentContext().get('callContext'));
describe('boot script', function (ns) {
    console.log('context 2  ', loopback.getCurrentContext().get('callContext'));
    this.timeout(20000);
    it('wait for boot script', function (done) {
        console.log('context 3  ', loopback.getCurrentContext().get('callContext'));
        app.on("started", function fncb () {
            done();
        });
    });
});

});

`

@pkgulati
Copy link
Author

pkgulati commented Jun 3, 2016

In above example I expect foo = bar when printing context 3 line, however it does not work that way

@gunjpan
Copy link
Contributor

gunjpan commented Jun 7, 2016

@pkgulati : #2004 is somewhat related?!, IMO

@gunjpan
Copy link
Contributor

gunjpan commented Aug 9, 2016

Hi @pkgulati, closing this as we're planning to deprecate getCurrentContext() with a warning message as it has not been reliable. See the discussion in #1676. Should you have any concerns about how this deprecation may affect you, please post them in the issue there.

Thank you.

@gunjpan gunjpan closed this as completed Aug 9, 2016
@gunjpan gunjpan removed the blocked label Aug 9, 2016
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

2 participants