rescue_from 在 ActiveJob 中不起作用 [英] rescue_from not working in ActiveJob

查看:43
本文介绍了rescue_from 在 ActiveJob 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级:

class TestUpdateJob < ActiveJob::Base

  include ActiveSupport::Rescuable
  rescue_from UserMediaException::UserNotFound, with: :destroy_user

  def self.call
    um = UserMedia.call('81274092340912873',100)
  end

  def destroy_user
    print "User to be destroyed"
  end


end

UserMedia.call('81274092340912873',100) 引发 UserMediaException::UserNotFound 异常.

我正在尝试测试 rescue_from,但我无法让它工作.永远不会打印消息要销毁的用户".

I'm trying to test rescue_from, but I'm not able to make it work. The message "User to be destroyed" never gets printed.

我正在拯救的自定义异常 (UserMediaException::UserNotFound) 继承自 StandardError.

The custom exception that I'm rescuing (UserMediaException::UserNotFound) inherits from StandardError.

知道为什么会这样吗?

推荐答案

根据 ActiveJobs 文档,所有作业类都必须在 perform 实例方法中具有其业务逻辑.

According to ActiveJobs documentation, all jobs class must have their business logic in a perform instance method.

你的代码应该是:

class TestUpdateJob < ActiveJob::Base

  include ActiveSupport::Rescuable
  rescue_from UserMediaException::UserNotFound, with: :destroy_user

  def perform
    um = UserMedia.call('81274092340912873',100)
  end

  private

  def destroy_user
    print "User to be destroyed"
  end

end

在您可以使用带有 perform_later 的作业进行排队或 perform_now 直接执行后:

After you can use your jobs with perform_later to enqueue it or perform_now to execute it directly :

TestUpdateJob.perform_later # Enqueue your job and execute it asynchronously
TestUpdateJob.perform_now # Execute your job now

这篇关于rescue_from 在 ActiveJob 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆