如何用钩子取消sails.js提升 [英] How to cancel sails.js lift with a hook

查看:35
本文介绍了如何用钩子取消sails.js提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在提升我的 Sails.js 应用程序时,我需要使用 Promise 执行某些初始化任务.

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).

但是,钩子系统只允许在钩子成功完成后调用 cb(),而且我似乎没有任何方法可以制作应用程序:

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:

  • 崩溃,没有超时(hook:X:错误)
  • 在前一个钩子确定成功或失败之前无法继续

有没有办法做到这一点?

Is there any way to do this?

推荐答案

首先,如果初始化任务特定于您的应用程序,您可以只使用 bootstrap 而不是钩子.以错误为参数调用引导回调将导致 Sails 退出.

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.

如果你确实确实需要使用一个钩子,你可以强制Sails以几乎相同的方式退出——从initialize方法中调用回调带有任何非空参数的钩子将发出失败信号,并导致 Sails 退出.通常,Node 应用就是这种情况:任何时候您需要调用回调,传递一个非空值作为第一个参数都会发出错误信号.

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.

因此,如果您的钩子的 initialize 启动了您需要运行的任务,并且在它们全部完成之前不调用 cb(),并调用 cb(<some 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.

这篇关于如何用钩子取消sails.js提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆