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

Add async init hook #560

Closed
danfinlay opened this issue Mar 3, 2014 · 11 comments
Closed

Add async init hook #560

danfinlay opened this issue Mar 3, 2014 · 11 comments

Comments

@danfinlay
Copy link

Currently if I need to perform an async operation before a geddy instance starts, I'd have to either wrap the geddy.start() function in a callback (doesn't work for clustered instances), or try something else hacky.

Here was mde's thought:

12:10 PM <mde> Actually, the simplest thing might be to provide some lifecycle methods on top of the events.
12:10 PM <mde> geddy.beforeStart for example, in the worker.
12:11 PM <mde> We could allow people to set those in their init.js, which should be loaded before the server actually starts.
12:11 PM <danfinlay> Yeah that would be ideal
12:11 PM <mde> It would be an async function, so you can do I/O.
12:11 PM <mde> That would work for you?
12:11 PM <danfinlay> Yeah, as long as it provided a callback or next() function
@ben-ng
Copy link

ben-ng commented Mar 4, 2014

we already have async support in the init.js script. is that not early enough?

@mde
Copy link
Contributor

mde commented Mar 4, 2014

The lifecycle events don't allow you any control over the startup sequence. People need a way to run some code before the server actually starts up.

@mde
Copy link
Contributor

mde commented Mar 4, 2014

Added in f93e789, pushed to NPM in v0.12.6.

@mde mde closed this as completed Mar 4, 2014
@danfinlay
Copy link
Author

Sorry, I'm a bit confused on how to use this.

Correct me if I'm wrong. In init.js I could:

var init = function(cb){
  geddy.beforeStart = function(next){
    doSomeAsyncStuff(function(er){
      if(!er) next();
    });
  }
}

Is that right? If not, how? I don't see in the diff what geddy expects other than beforeStart to be a function.

@techwraith
Copy link
Contributor

Don't wrap it in the init callback, just do geddy.beforeStart(function (){ //your code here })

On Thu, Mar 6, 2014 at 3:18 PM, Dan Finlay notifications@github.com wrote:

Sorry, I'm a bit confused on how to use this.

Correct me if I'm wrong. In init.js I could:

var init = function(cb){
geddy.beforeStart = function(next){
doSomeAsyncStuff(function(er){
if(!er) next();
});
}}

Is that right? If not, how? I don't see in the diff what geddy expects
other than beforeStart to be a function.

Reply to this email directly or view it on GitHubhttps://github.com//issues/560#issuecomment-36950305
.

@techwraith
Copy link
Contributor

Oh, I may actually be wrong here. @mde How did you implement this?

@danfinlay
Copy link
Author

Mde just said in IRC:

geddy.beforeStart = function (next) { /* do shit */ next(); }

So I think it's assignment, not a function. But in the init callback or not?

@techwraith
Copy link
Contributor

Yeah, it looks like it's assignment. I don't think it should be in the init callback though.

@mde
Copy link
Contributor

mde commented Mar 7, 2014

The app-loader requires the init.js file, and then immediately runs the exported init function, so you don't need to stick it in the init function, but it doesn't hurt if you do.

@mde
Copy link
Contributor

mde commented Mar 7, 2014

Ugh, @ben-ng, you're totally right -- I didn't look closely enough. The async init is plenty early. It all runs as the app loads, and before the server starts. I haven't documented this stuff yet, so I'm actually kind of inclined to rip it out before people start depending on it. @FlySwatter, does that make sense to you?

@mde
Copy link
Contributor

mde commented Mar 7, 2014

I have backed these changes out. The server doesn't start up until you call the continuation/callback passed into the async init, so you can do whatever setup you need there. Thanks @ben-ng for pointing it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants