Search code examples
ruby-on-railsrubysidekiqactivesupport

Sidekiq -r a single demo_worker.rb file, and require active support in that worker doesnt work


I wanted to require active_support in my demo_worker.rb.

require 'sidekiq'
require 'active_support'

class DemoWorker
end

And then I ran sidekiq -r ./demo_worker.rb

It told me that cannot require active_support, I also tried to require rails, but still wasn't working at all. How do I require active_support in the plain mode of sidekiq?

this is the exact error that I got

cannot load such file -- active_support/core_ext (LoadError)

I've looked around, couldnt find something helpful.


Solution

  • This error happens cause the gem activesupport has not been installed yet (on your local machine), so just run command gem install activesupport, and it'll be ok.

    But note that active_support in Rails use autoload so if we're going to use it's submodules outside Rails we need to require them directly, for example i want to use extension Integer#hours i need to require active_support/core_ext/integer/time

    require 'sidekiq'
    require "active_support/core_ext/integer/time"
    
    class Demo
      include Sidekiq::Worker
    
      def perform(how_hard="super hard", how_long=1)
        sleep how_long
        puts "Workin' #{how_hard} #{8.hours}"
      end
    end