简体   繁体   中英

How to cancel sails.js lift with a hook

I need to perform certain initialization tasks using promises when lifting my Sails.js app.

If these tasks fail in any way, the app should not continue lifting and must log an error. In addition, these tasks may not complete in a finite, predefined time (see the hook definition ).

However, the hook system only allows to call cb() when the hook has successfully completed, and there seems not to be any way in which I can make the app:

  • crash, without a timeout ( hook:X:error )
  • not able to continue until the previous hook has definitely succeeded or failed

Is there any way to do this?

First off, if the initialization tasks are specific to your application, you may be able to just use the bootstrap instead of a hook. Calling the bootstrap callback with an error as the argument will cause Sails to bail out.

If you do definitely need to use a hook, you can force Sails to exit in much the same way--calling the callback from within the initialize method of the hook with any non-null argument will signal a failure, and cause Sails to exit. This is the case with Node apps in general: any time you are expected to call a callback, passing a non-null value as the first argument will signal an error.

So, if your hook's initialize kicks off the tasks you need to run, and doesn't call cb() until they are all completed, and calls cb(<some error>) at any point where the tasks fail, then you'll have a situation where Sails won't load until the hook is finished, and bails out if the hook fails to complete its tasks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM