hirefire / hirefire-resource-ruby

HireFire integration library for Ruby applications
https://hirefire.io
MIT License
37 stars 38 forks source link

Issue with DelayedJob-Mongoid #6

Closed jturolla closed 11 years ago

jturolla commented 11 years ago

ArgumentError at /hirefire/development/info wrong number of arguments (2 for 1)

http://stackoverflow.com/questions/14553414/hirefire-isnt-working-with-heroku-delayed-job-mongoid-latest-versions

mrrooijen commented 11 years ago

Hi,

Could you try running the following in rails console?

c = ::Delayed::Job
c = c.where(:failed_at => nil)
c = c.where(:run_at.lte => Time.now.utc)
c.count

This way we can see whether the issue likes with Mongoid or not.

Also, I assume since you linked to SO, you ensured ActiveRecord isn't present/loaded?

Cheers

jturolla commented 11 years ago

Yes, ActiveRecord is not present.

The output:

1.9.3 (main):0 > c = ::Delayed::Job
=> Delayed::Backend::Mongoid::Job
1.9.3 (main):0 > c = c.where(:failed_at => nil)
=> #<Mongoid::Criteria
  selector: {"failed_at"=>nil}
  options:  {}
  class:    Delayed::Backend::Mongoid::Job
  embedded: false>

1.9.3 (main):0 > c = c.where(:run_at.lte => Time.now.utc)
=> #<Mongoid::Criteria
  selector: {"failed_at"=>nil, "run_at"=>{"$lte"=>2013-05-13 20:04:11 UTC}}
  options:  {}
  class:    Delayed::Backend::Mongoid::Job
  embedded: false>

1.9.3 (main):0 > c.count
=> 0
mrrooijen commented 11 years ago

Hmm. That rules Mongoid out.

Could you provide some information regarding your application setup? E.g.

Could you show me the backtrace so I know where exactly the error is being raised in the hirefire-resource gem (if any)?

It might be a configuration issue, or an incompatibility with the current middleware and something in your environment.

Let me know.

jturolla commented 11 years ago

Oh, of course, sorry for keeping you in the dark:

The Full trace can be found here: https://heroku.honeybadger.io/fault/867/1e238fde913d2639ed2a780a23bb326cb04edd30

ruby -v
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin12.2.0]

rails -v
Rails 3.2.11 (yes, i'm using the same)

source 'https://rubygems.org'

ruby '1.9.3'
gem 'rails', '3.2.11'

# System Level
gem 'unicorn'
gem 'mongoid', '~> 3.0.14'
gem 'devise'
gem 'cancan'
gem 'has_scope'
gem 'libv8', '~> 3.11.8'
gem 'newrelic_rpm'
gem 'rack-cache', :require => 'rack/cache'
gem 'inherited_resources'
gem 'statsmix'
gem 'bson_ext'
gem 'delayed_job_mongoid'
gem "delayed_job_web"
gem 'pdfkit'
# Low Level
gem 'oj'
#gem 'aws-s3'
gem 's3'
#gem 'fog'
gem 'mini_magick'
gem 'twilio-ruby'
gem 'mongoid_search'
gem "heroku-mongo-backup"
gem "hirefire-resource"

# Feature Level
gem 'meta_search',    '>= 1.1.0.pre'
gem 'awesome_nested_fields'
#gem 'rqrcode-rails3'
#gem 'dragonfly', '~>0.9.12'
gem 'rabl'
gem 'activeadmin-mongoid',  :git => 'git://github.com/elia/activeadmin-mongoid.git'
gem 'nested_form'
gem 'brazilian-rails'
gem 'honeybadger'

gem "rghost", :git => 'git://github.com/cncardoso/rghost.git'
gem "rghost_barcode"
gem "parseline"
gem "brcobranca", :git => "https://github.com/jturolla/brcobranca.git", :branch => "master"

# I18n
gem 'rails-i18n'
gem 'devise-i18n'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'therubyracer', :platforms => :ruby
  gem 'sass-rails',   '~> 3.2.3'
  gem 'less-rails'
  gem 'jquery-rails'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'compass-rails'
  gem 'jquery-ui-rails'
  gem 'uglifier', '>= 1.0.3'
  #gem 'asset_sync'
  gem 'compass'
  gem 'twitter-bootstrap-rails', :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git'
  gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'

end

group :development, :test, :staging do
  gem "letter_opener"
  gem 'rspec-rails'
  gem 'pry-rails'
  gem 'guard'
  gem 'guard-rspec'
  gem 'growl'
  gem 'debugger'
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'meta_request'
end

HireFire::Resource.configure do |config|
  config.dyno(:worker) do
    HireFire::Macro::Delayed::Job.queue
  end
end
mrrooijen commented 11 years ago

Hmm. This is very odd. I don't see any immediate issues with any of this. Maybe it's Rack. Could you check what version of Rack you're running? I'll try to reproduce this issue locally.

Does HireFire::Middleware show up properly when you run rake middleware?

Also, could you try the following for me.

Open rails console again and define the following function:

def dynos
  dyno_data = HireFire::Resource.dynos.inject(String.new) do |json, dyno|
    json << %|,{"name":"#{dyno[:name]}","quantity":#{dyno[:quantity].call || "null"}}|; json
  end

  "[#{dyno_data.sub(",","")}]"
end

Taken from the hirefire middleware.

And then run it. It should yield some JSON data. Just to confirm this is working properly. It's hard to see from the backtrace where exactly something is going wrong, but I have a hunch it's somewhere in the middleware.rb (https://github.com/meskyanichi/hirefire-resource/blob/master/lib/hirefire/middleware.rb)

Thanks

jturolla commented 11 years ago

Well, I got the same error as when running the app:

Sorry for the delay.

Hope it helps:

irb(main):001:0> def dynos
irb(main):002:1> dyno_data = HireFire::Resource.dynos.inject(String.new) do |json, dyno|
irb(main):003:2* json << %|,{"name":"#{dyno[:name]}","quantity":#{dyno[:quantity].call || "null"}}|; json
irb(main):004:2> end
irb(main):005:1> "[#{dyno_data.sub(",","")}]"
irb(main):006:1> end
=> nil
irb(main):007:0> dynos
ArgumentError: wrong number of arguments (2 for 1)
  from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.23/lib/mongoid/criteria.rb:551:in `where'
  from /app/vendor/bundle/ruby/1.9.1/gems/hirefire-resource-0.1.0/lib/hirefire/macro/delayed_job.rb:27:in `queue'
  from /app/config/initializers/hirefire.rb:3:in `block (2 levels) in <top (required)>'
  from (irb):3:in `call'
  from (irb):3:in `block in dynos'
  from (irb):2:in `each'
  from (irb):2:in `inject'
  from (irb):2:in `dynos'
  from (irb):7
  from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
  from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
  from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'
irb(main):008:0>

And the Middleware is there:

~ $ rake middleware

    heroku-mongo-backup: Please include 's3', 'aws/s3' or 'fog' gem in applications Gemfile for uploading backup to S3 bucket. (ignore this if using FTP)

    /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
    DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7)
    DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/Rakefile:7)
    use Honeybadger::Rack
    use Rack::Cache
    use ActionDispatch::Static
    use Rack::Lock
    use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000007344b88>
    use Rack::Runtime
    use Rack::MethodOverride
    use ActionDispatch::RequestId
    use Rails::Rack::Logger
    use ActionDispatch::ShowExceptions
    use ActionDispatch::DebugExceptions
    use ActionDispatch::RemoteIp
    use ActionDispatch::Callbacks
    use ActionDispatch::Cookies
    use ActionDispatch::Session::CookieStore
    use ActionDispatch::Flash
    use ActionDispatch::ParamsParser
    use ActionDispatch::Head
    use Rack::ConditionalGet
    use Rack::ETag
    use ActionDispatch::BestStandardsSupport
    use Warden::Manager
    use Rack::Mongoid::Middleware::IdentityMap
    use HireFire::Middleware
    run MinhaEntidade::Application.routes
jturolla commented 11 years ago

I don't know how to check the Rack version :(

mrrooijen commented 11 years ago

No problem. I don't think it's Rack.

According to the backtrace it looks like it's trying to invoke ActiveRecord's where structure rather than Mongoid's.

https://github.com/meskyanichi/hirefire-resource/blob/master/lib/hirefire/macro/delayed_job.rb#L27

Instead of

https://github.com/meskyanichi/hirefire-resource/blob/master/lib/hirefire/macro/delayed_job.rb#L33

This means that ActiveRecord is still being loaded somewhere.

To confirm this, could you open rails console and run:

ActiveRecord

Does this yield an exception or does it simply return without any issues? In case of the latter it's still being loaded. If this is the case, could you show me the contents of your config/application.rb?

Cheers

jturolla commented 11 years ago

Well, typing ActiveRecord returns ActiveRecord, not nil, this may be a clue, but I generated this project with the rails -O flag, wich indicates "without activerecord".

application.rb:

require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module MinhaEntidade
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Custom directories with classes and modules you want to be autoloadable.
    #config.autoload_paths += %W(#{config.root}/extras)
    config.autoload_paths += Dir["#{config.root}/lib/**/"]

    # Only load the plugins named here, in the order given (default is alphabetical).
    # :all can be used as a placeholder for all plugins not explicitly named.
    # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

    # Activate observers that should always be running.
    # config.active_record.observers = :cacher, :garbage_collector, :forum_observer

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    config.time_zone = 'Brasilia'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    config.i18n.default_locale = 'pt-BR'

    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]

    # Enable escaping HTML in JSON.
    config.active_support.escape_html_entities_in_json = true

    config.assets.initialize_on_precompile = false

    # Use SQL instead of Active Record's schema dumper when creating the database.
    # This is necessary if your schema can't be completely dumped by the schema dumper,
    # like if you have constraints or database-specific column types
    # config.active_record.schema_format = :sql

    # Enforce whitelist mode for mass assignment.
    # This will create an empty whitelist of attributes available for mass-assignment for all models
    # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
    # parameters by using an attr_accessible or attr_protected declaration.
    # config.active_record.whitelist_attributes = true

    # Enable the asset pipeline
    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'
  end
end
mrrooijen commented 11 years ago

That's odd. That might be the issue then. I might want to reverse the check then, to see if Mongoid is present before ActiveRecord since that really has to be explicitly loaded in through a gem.

You'd think ActiveRecord wouldn't be defined at all though if you don't include it. Is ActiveRecord::Base also defined?

jturolla commented 11 years ago

Yes, it's also defined.

You should check for the presence of Mongoid in my opinion, or provide the option for the user to specifically choose the ORM, like devise does.

jturolla commented 11 years ago

Do you think you can come up with a fix soon?

Thanks.

mrrooijen commented 11 years ago

Just pushed out a separate branch so you can try it out.

gem "hirefire-resource",
  :github => "meskyanichi/hirefire-resource",
  :branch => "mongoid-activerecord-swap"

Let me know if this works and I'll release a new gem with these changes in place.

jturolla commented 11 years ago

Looks fixed on dev. Pushing to heroku on staging.

jturolla commented 11 years ago

It's working perfecly! :) Thanks!

Tested everything, including adding new jobs in the queue, waiting for workers to run, running the jobs, and shutting workers down.

:+1:

mrrooijen commented 11 years ago

Great. I just pushed out a new gem (v0.1.1). You can use that now as it includes these changes.