我的工作人员begin rescue
方法中有perform
个阻止
begin
HTTParty.get(url)
rescue
## call failed for some reason, log and stop performing
break
end
## do more stuff here with the result of the call if it didn't fail
## this can fail too so a further
begin
##would be cumbersome
rescue
end
然而我得到Invalid break (SyntaxError)
还有另一种方法告诉sidekiq这项工作基本上已经完成了吗?我不想让它重试,而是完全退出。
答案 0 :(得分:2)
回来:
begin
HTTParty.get(url)
rescue => e
logger.warn(e.message)
return
end
答案 1 :(得分:1)
你不需要休息。如果begin中的代码失败,则执行将暂停,并执行rescue
块内的内容。要静默忽略并返回,只需将救援内容留空即可。
def perform
begin
HTTParty.get(url)
rescue
## call failed for some reason, log and stop performing
end
end
您还可以缩短方法
def perform
HTTParty.get(url)
rescue
## call failed for some reason, log and stop performing
end
您可能只想明确拯救某些例外情况。我不确定你想要拯救什么(这取决于HTTParty可以提出什么)。
def perform
HTTParty.get(url)
rescue WhateverError
## call failed for some reason, log and stop performing
end
这是一个例子
def perform
HTTParty.get(url)
rescue WhateverError => e
Rails.logger.error "Kaboom! #{e.message}"
end