如何从 Rake 任务中提早返回?
我有一个 rake 任务,我在开始时进行一些检查,如果其中一项检查失败,我想尽早从 rake 任务返回,我不想执行任何剩余的代码。
我认为解决方案是将返回放在我想从代码返回的位置,但出现以下错误
unexpected return
I have a rake task where I do some checks at the beginning, if one of the checks fails I would like to return early from the rake task, I don't want to execute any of the remaining code.
I thought the solution would be to place a return where I wanted to return from the code but I get the following error
unexpected return
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Rake 任务基本上是一个块。除 lambda 之外的块不支持 return,但您可以使用
next
跳到下一条语句,这在 rake 任务中与在方法中使用 return 具有相同的效果。或者您可以将代码移动到方法中并在方法中使用 return 。
我更喜欢第二个选择。
A Rake task is basically a block. A block, except lambdas, doesn't support return but you can skip to the next statement using
next
which in a rake task has the same effect of using return in a method.Or you can move the code in a method and use return in the method.
I prefer the second choice.
您可以在任务内部使用
abort(message)
来通过消息中止该任务。You can use
abort(message)
from inside the task to abort that task with a message.返回错误 ❌
如果您返回错误(即退出代码为
1
),您将需要使用abort
,它还采用一个可选的字符串参数,该参数将在退出时输出:
在命令行上:
成功返回✅
如果您没有返回错误(即退出代码
0
),您需要使用exit
,但不一个字符串参数。在命令行上:
如果您在 cron 作业中使用它,或者需要在之后根据 rake 任务是否成功执行某些操作,那么这一点很重要。
奖励:从没有堆栈跟踪的
rescue
块中返回错误。默认情况下,如果您在
rescue
块内使用abort
,它将输出整个堆栈跟踪,即使您只使用abort
而不重新执行- 引发错误。为了解决这个问题,您可以向
exit
命令提供非零退出代码,例如:Return with an Error ❌
If you're returning with an error (i.e. an exit code of
1
) you'll want to useabort
, which also takes an optional string param that will get outputted on exit:On the command line:
Return with Success ✅
If you're returning without an error (i.e. an exit code of
0
) you'll want to useexit
, which does not take a string param.On the command line:
This is important if you're using this in a cron job or something that needs to do something afterwards based on whether the rake task was successful or not.
Bonus: Return with an Error from a
rescue
block without the stacktrace.By default, if you use
abort
inside of arescue
block, it will output the entire stack trace, even if you just useabort
without re-raising the error.To get around this, you can supply a non-zero exit code to the
exit
command, like:我倾向于使用
abort
在这种情况下这是一个更好的选择,例如:I tend to use
abort
which is a better alternative in such situations, for example:如果您需要突破多个块级别,可以使用fail 。
例如
(请参阅https://stackoverflow.com/a/3753955/11543。)
If you need to break out of multiple block levels, you can use fail.
For example
(See https://stackoverflow.com/a/3753955/11543.)
如果您的意思是退出 rake 任务而不导致“rake 中止!”要打印消息,那么您可以使用“abort”或“exit”。但是,当在救援块中使用“abort”时,会终止任务并打印整个错误(即使不使用 --trace)。所以我用的是“退出”。
If you meant exiting from a rake task without causing the "rake aborted!" message to be printed, then you can use either "abort" or "exit". But "abort", when used in a rescue block, terminates the task as well as prints the whole error (even without using --trace). So "exit" is what I use.
我使用了 Simone Carletti 建议的
next
方法,因为在测试 rake 任务时,abort
(实际上只是exit
的包装器)不是期望的行为。例子:
I used
next
approach suggested by Simone Carletti, since when testing rake task,abort
, which in fact is just a wrapper forexit
, is not the desired behavior.Example: