nesquena / backburner

Simple and reliable beanstalkd job queue for ruby
http://nesquena.github.com/backburner
MIT License
428 stars 68 forks source link

Working with Mongoid? #80

Closed tdegrunt closed 9 years ago

tdegrunt commented 9 years ago

Trying to use backburner with Mongoid. Am I doing something wrong?

I have a User class:

class User
  include Mongoid::Document
  include ActiveModel::SecurePassword
  include Mongoid::Timestamps
  include Backburner::Performable

  def bill(currency, price, description)
    puts "bill"
  end

  ...
end

Then I'm trying in the rails console, with pry:

[15] pry(main)> u1 = User.find('52683dcb3fb06d4544000001')
=> #<User _id: 52683dcb3fb06d4544000001...>
[16] pry(main)> u1.id.to_s
=> "52683dcb3fb06d4544000001"
[17] pry(main)> u1.async.bill('eur', 12.60, 'Lol')
ArgumentError: wrong number of arguments (1 for 0)
from /usr/local/lib/ruby/gems/2.1.0/gems/bson-2.3.0/lib/bson/object_id.rb:185:in `to_s'
[19] pry(main)> u1.id.to_s(1)
ArgumentError: wrong number of arguments (1 for 0)
from /usr/local/lib/ruby/gems/2.1.0/gems/bson-2.3.0/lib/bson/object_id.rb:185:in `to_s'
...
[22] pry(main)> u1.id.to_json
=> "52683dcb3fb06d4544000001"

This seems to be the relevant section of the backtrace:

[15] pry(#<ArgumentError>):1> backtrace
=> ["/usr/local/lib/ruby/gems/2.1.0/gems/bson-2.3.0/lib/bson/object_id.rb:185:in `to_s'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/core_ext/object/json.rb:140:in `block in as_json'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/core_ext/object/json.rb:140:in `map'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/core_ext/object/json.rb:140:in `as_json'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/core_ext/object/json.rb:159:in `block in as_json'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/core_ext/object/json.rb:159:in `each'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/core_ext/object/json.rb:159:in `map'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/core_ext/object/json.rb:159:in `as_json'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/json/encoding.rb:34:in `encode'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/json/encoding.rb:21:in `encode'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/core_ext/object/json.rb:37:in `to_json_with_active_support_encoder'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/backburner-0.4.6/lib/backburner/worker.rb:35:in `block in enqueue'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/backburner-0.4.6/lib/backburner/worker.rb:161:in `retryable_command'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/backburner-0.4.6/lib/backburner/worker.rb:33:in `enqueue'",
 "/usr/local/lib/ruby/gems/2.1.0/gems/backburner-0.4.6/lib/backburner/async_proxy.rb:22:in `method_missing'",
 "(pry):6:in `<main>'",
tdegrunt commented 9 years ago

Okay, this is my own fault I guess. I read somewhere that to fix the JSON serialization for the _id, one needed an initialiser with:

module BSON
  class ObjectId
    alias :to_json :to_s
    alias :as_json :to_s
  end
end

Everything was working, once I changed this initializer to:

module BSON
  class ObjectId
    def to_json(arg=nil)
      to_s
    end
    def as_json(arg=nil)
      to_s
    end
  end
end

Closing ...