Ruby:返回非零退出状态

3
我希望我的 Ruby 脚本在某个条件下返回任何非零的退出状态。只要它不是零,退出状态本身并不重要。我测试了如何实现这一点:
test_exit.rb:
exit 4

test_exit_wrap.rb:

system("ruby test_exit.rb")
puts $?.exitstatus

命令行:

> ruby test_exit_wrap.rb
4

这很棒,因为这正是我想要的。然后我尝试在我的项目中复制这个。 pullrequest.rb:
results = [install_result.class, clicks_result.class, event_result.class, tuples_result.class, match_result.class]

if results.include? NilClass 
    puts "result included nil"
    exit 4
end

rakefile.rb:

command = "irb #{@script} #{@app_id} #{start_date} #{end_date} #{start_time} #{end_time} #{@timezone} #{@database}"
system("#{command}")
puts $?.exitstatus

if $?.exitstatus != 0
    puts "!!!!!!!!!!!!!!!!!!!!!! FAILED JOB !!!!!!!!!!!!!!!!!!!!!!!!!!"
    @backjob_queue << fail
end

命令行:

  pullkochava.rb(main):032:1>  
  result included nil
  => nil
  pullrequest.rb(main):032:0>
  0

(Clipping these out, of course, hope it still makes sense; also @script is equal to pullrequest.rb, I've double checked)我们可以看到,如果results.include?块肯定会被执行,但是exit 4似乎丢失了。0来自于puts $?. exitstatus,我永远无法让它返回除0以外的任何东西。我尝试过引发错误、不同的退出代码和中止。中止确实返回一个非零的退出代码;然而它也中止了耙。它将puts FAILED JOB,但rakefile没有保存@backjob_queue到我的数据库。

我错过了什么吗?如何返回一个不会中止rake的非零退出状态?

1个回答

6

我立即意识到了我的错误,但以防其他人需要这个答案:

你的系统命令应该是"ruby blahblahblah.rb"而不是"irb blahblahblah.rb"。两者都可以运行代码,但除非你中止,否则irb将返回退出状态0。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接