使用 Resque、Rails 3 和 Active-Recored 时出现问题

发布于 2024-09-27 22:20:56 字数 883 浏览 1 评论 0原文

我有这个类,它的响应要由“Resque”运行,我在这一行有一个错误 recipient.response = response.body 这是:

#Hash:0x00000003969da0 的未定义方法响应='

我认为这是因为工作人员和 ActiveRecord 无法一起工作。
PS 我已经加载了我的环境并将此类放置在 lib 目录中

使用:
红宝石 1.9.2
轨道 3
雷斯克 1.10.0

class Msg
  def self.perform(message,sender,host, path, recipient)
    message_logger ||= Logger.new("#{Rails.root}/log/message.log")
    response = Net::HTTP.get_response(host, path)
    begin
      recipient.response = response.body
      recipient.sent_at = Time.zone.now
      recipient.save
      # Logging
      log = "Message #{
      message.sent_at}\n\tRespone:\n\t\tBody: #{response.body}\n\t\tCode: #{response.code}\n"
      message_logger.info(log)
    rescue Exception => e
      message_logger.error(e.message + '/n' + e.backtrace.inspect)
    end
  end
end

I have this Class which response to perform to be run by "Resque", I have an error at this line recipient.response = response.body which is:

undefined method response=' for #Hash:0x00000003969da0

I think that because the worker and ActiveRecord can't work together.
P.S I already loaded my environment and this class placed in lib directory

Using:
Ruby 1.9.2
Rails 3
Resque 1.10.0

class Msg
  def self.perform(message,sender,host, path, recipient)
    message_logger ||= Logger.new("#{Rails.root}/log/message.log")
    response = Net::HTTP.get_response(host, path)
    begin
      recipient.response = response.body
      recipient.sent_at = Time.zone.now
      recipient.save
      # Logging
      log = "Message #{
      message.sent_at}\n\tRespone:\n\t\tBody: #{response.body}\n\t\tCode: #{response.code}\n"
      message_logger.info(log)
    rescue Exception => e
      message_logger.error(e.message + '/n' + e.backtrace.inspect)
    end
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2024-10-04 22:20:56

Resque 使用 json 序列化。 JSON 序列化不允许您使用完整的方法反序列化对象。

如果您有一个 Recipient 实例(名为“recipient”)并希望在方法中使用它来执行/持久响应,那么您应该将收件人的 id 排入队列,并在调用执行时从持久层中获取它。

https://github.com/defunkt/resque(查看持久化部分)

Resque 与 DelayedJob 不同/Background Job 等都是这样。 (这就是我喜欢它的原因。同一个队列可以由多个 ruby​​ 实现、jruby、mri 等共享)

Resque uses json serialization. JSON serialization would not allow you to deserialize an object with the method intact.

If you have an instance of Recipient (named "recipient") and want to use it in the method to perform/persist a response then you should enqueue the id of the recipient and fetch it from your persistence layer when perform is called.

https://github.com/defunkt/resque (checkout the section on Persistence)

Resque is different from DelayedJob/Background Job and other in this way. (which is why I like it. the same queue can be shared by multiple ruby implementations, jruby, mri, ...)

挖个坑埋了你 2024-10-04 22:20:56

这听起来根本不像 resque 和 activerecord 的问题。它表示您传入的参数recipient 是一个哈希值。使作业排队的代码在哪里?您还可以查看您看到该错误消息的工作程序的日志输出,以了解传递到作业的参数是什么。

That doesn't sound like an issue with resque and activerecord at all. It says the parameter recipient that you passed in was a hash. Where's the code that enqueued the job? You can also take a look a the log output from the worker where you saw that error message to see what the parameters passed into the job were.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文