Fix Sidekiq loading problem: Cannot define multiple included blocks for a Concern

2nd November 2015 – 209 words

Starting Sidekiq in development/test, we got this problem:

Cannot define multiple 'included' blocks for a Concern
/home/user/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.4/lib/active_support/concern.rb:126:in `included'
/home/user/.rvm/gems/ruby-2.1.2/bundler/gems/our-gem-3f4ecd7d8fb2/app/models/somemodel/model:9:in `<module:Somemodel>'
...

Turns out, our file which uses the included do...end feature was loaded twice and that is not supported by a ActiveSupport::Concern.

Fix

  1. Replace all occurrences of require 'file_with_concern' with require_dependency 'file_with_concern'.
    • OR - switch to eager_loading for Sidekiq:
# config/environments/development.rb
MyApp::Application.configure do

  #...
  config.cache_classes = !!Sidekiq.server?
  config.eager_load = !!Sidekiq.server?

See mperham/sidekiq#1791 and mperham/sidekiq#447.


If you are unsure, who calls that file, you can modify the file with an editor and add some statements to the top of the file:

puts "FILE #{__FILE__} LOADED BY:"
puts caller.to_a.reject{|l| l['gems/ruby-2.1.2/gems/']}.join("\n")

# rest of the file

When running the Sidekiq process, you will see the output with the call stack for that file.