ActiveJob rescue_从成功中返回而没有引发失败

时间:2018-11-19 12:28:17

标签: ruby ruby-on-rails-5 rails-activejob

我正在尝试在ApplicationJob中捕获一些错误,但是我不希望这些错误引发异常(并返回失败的作业执行),但是我希望某些错误被忽略(基本上会返回成功)。 我可以在每个作业中单独使用常规begin-rescue来完成此任务,但是我想在多个作业之间共享这种救助方式。

问题是rescue_from捕获了错误,但无论如何都会引发错误,并且作业在执行时失败。

application_job.rb

class ApplicationJob < ActiveJob::Base

  rescue_from StandardError do |exception|
    puts "Rescuing"
    true
  end

end

test_job.rb

class TestJob < ApplicationJob


  def perform(*args)
    raise ActiveResource::ResourceNotFound, "Wow"
  end

end

执行作业时记录:

Performing TestJob (Job ID: fd58cbd5-eb03-4fdc-8301-1ea70a9b1d8b) from Async(default)
Error performing TestJob (Job ID: fd58cbd5-eb03-4fdc-8301-1ea70a9b1d8b) from Async(default) in 1.03ms: ActiveResource::ResourceNotFound (Failed.):
...
.../.rvm/rubies/ruby-2.4.4/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
-e:1:in `<main>'
Rescuing
 => #<ActiveResource::ResourceNotFound: Failed.>

1 个答案:

答案 0 :(得分:0)

这个问题已经有几天了,但还没有回答。

在定义救援之前,您必须在作业类中[TikTags.HashTags(title: "Dance", tags: []), TikTags.HashTags(title: "Dance", tags: ["#dance", "#music", "#dancer", "#love", "#viral", "#hiphop", "#like", "#instagood", "#fashion", "#tiktok", "#instagram", "#follow", "#ballet", "#rap", "#explore", "#trending", "#explorepage", "#likeforlikes", "#art", "#dancelife", "#followme", "#video", "#youtube", "#dancechallenge", "#d", "#cute", "#pocodance"]), TikTags.HashTags(title: "Funny", tags: ["#dance", "#music", "#dancer", "#love", "#viral", "#hiphop", "#like", "#instagood", "#fashion", "#tiktok", "#instagram", "#follow", "#ballet", "#rap", "#explore", "#trending", "#explorepage", "#likeforlikes", "#art", "#dancelife", "#followme", "#video", "#youtube", "#dancechallenge", "#d", "#cute", "#pocodance"]), TikTags.HashTags(title: "Funny", tags: ["#funny", "#lol", "#lmao", "#lmfao", "#hilarious", "#laugh", "#laughing", "#tweegram", "#fun", "#friends", "#photooftheday", "#friend", "#wacky", "#crazy", "#silly", "#witty", "#instahappy", "#joke", "#jokes", "#joking", "#epic", "#instagood", "#instafun", "#funnypictures", "#haha", "#humor"]), TikTags.HashTags(title: "Gaming", tags: ["#funny", "#lol", "#lmao", "#lmfao", "#hilarious", "#laugh", "#laughing", "#tweegram", "#fun", "#friends", "#photooftheday", "#friend", "#wacky", "#crazy", "#silly", "#witty", "#instahappy", "#joke", "#jokes", "#joking", "#epic", "#instagood", "#instafun", "#funnypictures", "#haha", "#humor"]), TikTags.HashTags(title: "Gaming", tags: ["#gamergirl", "#gamestagram", "#video", "#winning", "#gaminglife", "#pcgaming", "#gamer", "#online", "#playing", "#playinggames", "#videogames", "#instagaming", "#gamerguy", "#instagamer", "#onlinegaming", "#game", "#play", "#gamingsetup", "#instagood", "#videogameaddict", "#photooftheday", "#games", "#gaming", "#pc", "#gamin", "#instagame", "#gamingmemes"])]

另见与此主题相关的另一个问题:rescue_from not working in ActiveJob