我正在尝试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 语句的缓存键中访问错误消息。我究竟做错了什么?异常以什么格式保存?