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

Startup Code Never Runs on Server #2239

Closed
aldeed opened this Issue Jun 18, 2014 · 1 comment

Comments

Projects
None yet
2 participants
@aldeed
Contributor

aldeed commented Jun 18, 2014

NOTE: Since I got no replies posting this to meteor-core and because it seems like a bug, I'm submitting it here.

I always thought that passing a function to Meteor.startup meant that it would be executed on startup, but if already started, it would be executed immediately. Recently some things weren't working for me, and I discovered that the way I thought it works is correct on the client but incorrect on the server. On the server, if you call Meteor.startup and startup is already complete, the function is never called.

Here's a repo that shows this: https://github.com/aldeed/startup-test

I tried with several older releases, and it seems as though it has always been this way.

(1) Is the difference between client and server behavior intentional?
(2) If yes, can this aspect be clarified in the docs?

My particular issue was with code in a package which could potentially run before or after startup, but if run before, then I wanted to wait until after startup was done. I worked around this issue with something like this:

if (Meteor.isServer) {
  // A function passed to Meteor.startup is only run on the server if
  // the process has not yet started up. So we need a flag to tell
  // us whether to wrap in Meteor.startup or not
  var hasStartedUp = false;
  Meteor.startup(function () {
    hasStartedUp = true;
  });

  function otherFunctionThatMightRunBeforeOrAfter() {
    function stuffToDoNowOrOnStartup() {
      // etc..
    }

    if (hasStartedUp) {
      stuffToDoNowOrOnStartup();
    } else {
      Meteor.startup(stuffToDoNowOrOnStartup);
    }
  }
}

@glasser glasser closed this in 4bec487 Jun 19, 2014

@glasser

This comment has been minimized.

Member

glasser commented Jun 19, 2014

Thanks, good catch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment