Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add async init hook #560

Closed
flyswatter opened this Issue Mar 3, 2014 · 11 comments

Comments

Projects
None yet
4 participants

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
Contributor

ben-ng commented Mar 4, 2014

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

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.

Contributor

mde commented Mar 4, 2014

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

@mde mde closed this Mar 4, 2014

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.

Contributor

Techwraith commented Mar 6, 2014

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/geddy/geddy/issues/560#issuecomment-36950305
.

Contributor

Techwraith commented Mar 6, 2014

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

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?

Contributor

Techwraith commented Mar 6, 2014

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

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.

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?

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