Pergunta

I have this Class which response to perform to be run by "Resque", I have an error at this line recipient.response = response.body wich 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
Foi útil?

Solução

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top