1

我正在尝试error通过使用 Rails.cache(绑定到 Memcached)在延迟作业中使用钩子将异常传递给我的控制器。

我的控制器方法具有以下代码行,并且我已经测试了该作业是否正常运行:

Delayed::Job.enqueue(BuildDetail.new)

BuildDetail 类在我的 /lib/jobs/build_detail.rb 文件中定义:

class BuildDetail

  def perform
    # do some stuff here
  end

  def success(job)
    Rails.cache.write("job_done", true, :expires_in => 4.hours)
  end

  def error(job, exception)
    Rails.cache.write("job_errors", exception, :expires_in => 4.hours)
  end

然后在另一个控制器方法中,我希望能够执行以下操作:

def other_controller_method
  job_errors = Rails.cache.read("job_errors")

  case job_errors
    when Timeout::Error
      redirect_to reports_path
      flash[:error] = "You have timed out!"
    else
  #...something else here
  end

end

但是,我似乎无法从 case 语句的缓存键中访问错误消息。我究竟做错了什么?异常以什么格式保存?

4

1 回答 1

0

因此,在一些帮助下,我发现将后台作业异常保存在缓存键中是一个非常可行的解决方案。从那里,我做了几件不同的事情——用 JQuery 处理一些错误,用实际的 Rails 服务器处理一些错误。我的问题是(在成为 Rails 新手时)我忘记了异常是特定的对象类型 - 并且假设我可以传递缓存值并将其作为字符串引用。

于 2013-02-18T17:04:23.053 回答