如何从 Rake 任务中提早返回?

发布于 2024-08-22 08:54:00 字数 156 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半夏半凉 2024-08-29 08:54:00

Rake 任务基本上是一个块。除 lambda 之外的块不支持 return,但您可以使用 next 跳到下一条语句,这在 rake 任务中与在方法中使用 return 具有相同的效果。

task :foo do
  puts "printed"
  next
  puts "never printed"
end

或者您可以将代码移动到方法中并在方法中使用 return 。

task :foo do
  do_something
end

def do_something
  puts "startd"
  return
  puts "end"
end

我更喜欢第二个选择。

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.

task :foo do
  puts "printed"
  next
  puts "never printed"
end

Or you can move the code in a method and use return in the method.

task :foo do
  do_something
end

def do_something
  puts "startd"
  return
  puts "end"
end

I prefer the second choice.

请别遗忘我 2024-08-29 08:54:00

您可以在任务内部使用 abort(message) 来通过消息中止该任务。

You can use abort(message) from inside the task to abort that task with a message.

七月上 2024-08-29 08:54:00

返回错误 ❌

如果您返回错误(即退出代码为1),您将需要使用abort,它还采用一个可选的字符串参数,该参数将在退出时输出:

task :check do
  
  # If any of your checks fail, you can exit early like this.
  abort( "One of the checks has failed!" ) if check_failed?

end

在命令行上:

$ rake check && echo "All good"
#=> One of the checks has failed!

成功返回✅

如果您没有返回错误(即退出代码 0),您需要使用 exit,但一个字符串参数。

task :check do
  
  # If any of your checks fail, you can exit early like this.
  exit if check_failed?
  
end

在命令行上:

$ rake check && echo "All good"
#=> All good

如果您在 cron 作业中使用它,或者需要在之后根据 rake 任务是否成功执行某些操作,那么这一点很重要。


奖励:从没有堆栈跟踪的 rescue 块中返回错误。

默认情况下,如果您在 rescue 块内使用 abort,它将输出整个堆栈跟踪,即使您只使用 abort 而不重新执行- 引发错误。

为了解决这个问题,您可以向 exit 命令提供非零退出代码,例如:


task :check do

  begin
    do_the_thing_that_raises_an_exception
  rescue => error
    puts error.message
 
    exit( 1 )
  end

end

Return with an Error ❌

If you're returning with an error (i.e. an exit code of 1) you'll want to use abort, which also takes an optional string param that will get outputted on exit:

task :check do
  
  # If any of your checks fail, you can exit early like this.
  abort( "One of the checks has failed!" ) if check_failed?

end

On the command line:

$ rake check && echo "All good"
#=> One of the checks has failed!

Return with Success ✅

If you're returning without an error (i.e. an exit code of 0) you'll want to use exit, which does not take a string param.

task :check do
  
  # If any of your checks fail, you can exit early like this.
  exit if check_failed?
  
end

On the command line:

$ rake check && echo "All good"
#=> All good

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 a rescue block, it will output the entire stack trace, even if you just use abort without re-raising the error.

To get around this, you can supply a non-zero exit code to the exit command, like:


task :check do

  begin
    do_the_thing_that_raises_an_exception
  rescue => error
    puts error.message
 
    exit( 1 )
  end

end
偏爱你一生 2024-08-29 08:54:00

我倾向于使用 abort 在这种情况下这是一个更好的选择,例如:

task :foo do
  something = false
  abort 'Failed to proceed' unless something
end

I tend to use abort which is a better alternative in such situations, for example:

task :foo do
  something = false
  abort 'Failed to proceed' unless something
end
醉梦枕江山 2024-08-29 08:54:00

如果您需要突破多个块级别,可以使用fail

例如

task :something do
  [1,2,3].each do |i|
    ...
    fail "some error" if ...
  end
end

(请参阅https://stackoverflow.com/a/3753955/11543。)

If you need to break out of multiple block levels, you can use fail.

For example

task :something do
  [1,2,3].each do |i|
    ...
    fail "some error" if ...
  end
end

(See https://stackoverflow.com/a/3753955/11543.)

最美不过初阳 2024-08-29 08:54:00

如果您的意思是退出 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.

陪我终i 2024-08-29 08:54:00

我使用了 Simone Carletti 建议的 next 方法,因为在测试 rake 任务时,abort(实际上只是 exit 的包装器)不是期望的行为。

例子:

task auto_invoice: :environment do
  if Application.feature_disabled?(:auto_invoice)
    $stderr.puts 'Feature is disabled, aborting.'
  next
end

I used next approach suggested by Simone Carletti, since when testing rake task, abort, which in fact is just a wrapper for exit, is not the desired behavior.

Example:

task auto_invoice: :environment do
  if Application.feature_disabled?(:auto_invoice)
    $stderr.puts 'Feature is disabled, aborting.'
  next
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文