ruby-on-railsactiverecordruby-on-rails-3ruby-1.9resque

Problem using Resque, Rails 3 and Active-Recored


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

Solution

  • 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, ...)