Skip to content

fix: ensure the startup is completed before calling startupCompleted #7525

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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,38 +82,40 @@ class ParseServer {

logging.setLogger(loggerController);

if (cloud) {
addParseCloud();
if (typeof cloud === 'function') {
cloud(Parse);
} else if (typeof cloud === 'string') {
require(path.resolve(process.cwd(), cloud));
} else {
throw "argument 'cloud' must either be a string or a function";
}
}

// Note: Tests will start to fail if any validation happens after this is called.
databaseController
.performInitialization()
.then(() => hooksController.load())
Promise.resolve()
.then(async () => await databaseController.performInitialization())
.then(async () => await hooksController.load())
.then(async () => {
if (schema) {
await new DefinedSchemas(schema, this.config).execute();
}
})
.then(async () => {
if (serverStartComplete) {
serverStartComplete();
await serverStartComplete();
}
})
.catch(error => {
.catch(async (error) => {
if (serverStartComplete) {
serverStartComplete(error);
await serverStartComplete(error);
} else {
console.error(error);
process.exit(1);
}
});
Comment on lines +97 to 117
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to help to cross de finish line, here the full correct structure.

    // Note: Tests will start to fail if any validation happens after this is called.
    databaseController.performInitialization()
      .then(() => hooksController.load())
      .then(async () => {
        if (schema) {
          await new DefinedSchemas(schema, this.config).execute();
        }
      })
      .then(async () => {
        if (serverStartComplete) {
          await Promise.resolve(serverStartComplete());
        }
      })
      .catch(async (error) => {
        if (serverStartComplete) {
          await Promise.resolve(serverStartComplete(error));
        } else {
          console.error(error);
          process.exit(1);
        }
      });

Then in a futur PR that refactor the start up (BK change) the structure will be

try {
  await databaseController.performInitialization()
  await hooksController.load()
  if (schema) {
    await new DefinedSchemas(schema, this.config).execute();
  }
  if (serverStartComplete) {
    await Promise.resolve(serverStartComplete());
  }
} catch(e){
  if (serverStartComplete) {
      await Promise.resolve(serverStartComplete(error));
  } else {
    console.error(error);
    process.exit(1);
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, not sure what's the difference.

I don't remember where, but we had a discussion about starting Promises chains with Promise.resolve() to be consistent with other promises chains in Server and JS SDK

  1. await Promise.resolve(serverStartComplete());

I don't think we need to wrap it around Promise.resolve, await will work either way.


if (cloud) {
addParseCloud();
if (typeof cloud === 'function') {
cloud(Parse);
} else if (typeof cloud === 'string') {
require(path.resolve(process.cwd(), cloud));
} else {
throw "argument 'cloud' must either be a string or a function";
}
}

if (security && security.enableCheck && security.enableCheckLog) {
new CheckRunner(options.security).run();
}
Expand Down