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

setup service method not called in async plugin #67

Closed
ksmth opened this issue May 31, 2014 · 7 comments · Fixed by #78 or #107
Closed

setup service method not called in async plugin #67

ksmth opened this issue May 31, 2014 · 7 comments · Fixed by #78 or #107
Milestone

Comments

@ksmth
Copy link

ksmth commented May 31, 2014

Even though the setup method isn't called, the services work properly.

// app.js
require('feathers')()
    .configure(function () {
        var app = this;
        app.use('/setup', {
            get : function (id, params, callback) {
                callback(null, {});
            },
            setup : function (app) {
                console.log('setup');
            }
        });

        setTimeout(function () {
            app.use('/nosetup', {
                get : function (id, params, callback) {
                    callback(null, {});
                },
                setup : function (app) {
                    console.log('no setup :(');
                }
            });
        }, 100);
    })
    .listen(8080);
$ node app.js
setup
@ksmth ksmth changed the title setup service method not called in async plugin setup service method not called in async plugin May 31, 2014
@daffl daffl added this to the 1.0.0 milestone May 31, 2014
@daffl
Copy link
Member

daffl commented May 31, 2014

setup is being called on .listen for every registered service here. The problem with setting up services right away when calling .use() is that they won't be able to use any other service that has been registered later on. Not sure right now what the best way would be to check if the application has been set up already or not.

@ksmth
Copy link
Author

ksmth commented May 31, 2014

One option would be to wait for configure to finish by making it async, i.e.:

app.configure(function (done) { 
    setTimeout(done, 500);
})

Or if backwards compatibility is a concern:

app.configure(function (async) { 
    // if async isn't called, configure must be sync
    var done = async();
    setTimeout(done, 500);
})

In my blueprints plugin I'm calling the setup method manually for now.

@daffl
Copy link
Member

daffl commented May 31, 2014

I wouldn't use .configure to set up the application. At least in the example case the problem can be easily solved by changing the code to:

var app = require('feathers')();

app.use('/setup', {
    get : function (id, params, callback) {
        callback(null, {});
    },
    setup : function (app) {
        console.log('setup');
    }
});

setTimeout(function () {
    app.use('/nosetup', {
        get : function (id, params, callback) {
            callback(null, {});
        },
        setup : function (app) {
            console.log('no setup :(');
        }
    });

    app.listen(8080);
}, 100);

Because we can't know if the provider (REST, SocketIO, Primus - and potentially more in the future) might hook into app.setup (see here) and needs all services registered right from the beginning Feathers should probably throw an error if you try to app.use() a service after app.setup() has been called already. An alternative might be adding individual setup functionality for a provider.

@ksmth
Copy link
Author

ksmth commented Jun 1, 2014

Hmm, ok. That was the approach I took when developing a blueprints plugin. What'd be an alternative? The goal for my plugin was to generate services based on model definitions and hook them up to the application using the plugin architecture. You can have a look at the source code to check implementation details.

Since I have opened this issue I changed from calling setup manually to providing a callback, so you can wait for the initialization to be finished.

@ksmth
Copy link
Author

ksmth commented Jun 1, 2014

The more I'm thinking about it ... it should also be possible to mount another express application. Theoretically, I could implement the blueprints as a standalone feathers app and mount that, but I'm uncertain whether that's really necessary - maybe you can share your thoughts.

@daffl daffl removed this from the 1.0.0 milestone Jun 4, 2014
@daffl daffl added this to the 1.0.0 milestone Jun 11, 2014
@daffl
Copy link
Member

daffl commented Jun 11, 2014

The question is why you need to register services after starting the application. One option would be to override app.listen and only call _super once everything has been set up.

I will probably add an error for version 1.0 if you are trying to register a service on an already running application so that nobody runs into weird issues that could happen if you do.

@daffl daffl closed this as completed in #78 Jun 11, 2014
@daffl daffl reopened this Jun 14, 2014
@daffl daffl mentioned this issue Jun 14, 2014
@daffl daffl removed this from the 1.0.0 milestone Aug 10, 2014
@daffl daffl added this to the 1.1.0 milestone Feb 11, 2015
daffl added a commit that referenced this issue Aug 21, 2018
daffl added a commit that referenced this issue Aug 22, 2018
daffl pushed a commit that referenced this issue Aug 24, 2018
daffl pushed a commit that referenced this issue Aug 25, 2018
Removing the dry-run arg so the update script actually updates
daffl pushed a commit that referenced this issue Aug 28, 2018
* chore(package): update sinon to version 6.0.0

* Update Travis install script
daffl pushed a commit that referenced this issue Aug 29, 2018
Removing assigning token to params.query for sockets. Closes #65.
daffl pushed a commit that referenced this issue Aug 29, 2018
* chore(package): update sinon to version 6.0.0

* Update Travis install script
daffl pushed a commit that referenced this issue Aug 29, 2018
Removing assigning token to params.query for sockets. Closes #65.
@lock
Copy link

lock bot commented Feb 8, 2019

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants