Search code examples
ruby-on-railsrubysidekiq

Unable to call Rails Models inside Sidekiq worker class


I'm using the sidekiq scheduler gem to schedule and run tasks in the background. The setup works fine and tasks get scheduled and executed correctly, however, if I want run methods from inside my rails models I can't. This is how I run sidekiq:

sidekiq -r ./app/workers/get_new_users.rb -e production

How can I get my get_new_users script to call methods in my models?

require 'sidekiq-scheduler'


class GetNewUsers
  include Sidekiq::Worker


  def perform
    puts User.find(1)
  end
end

Thanks


Solution

  • Just drop the -r parameter and sidekiq will then load your rails environment (read: all your models and other things)

    You can also drop -e (which will then be taken from RAILS_ENV env var, which should be set anyway).

    And it's a good idea to run your binaries through bundler. So, your command becomes just this:

     bundle exec sidekiq