Austin Turner
Austin Turner

Reputation: 21

Sidekiq + sidekiq_scheduler gem (uninitialized constant worker_class::model)

I'm working on an asyc worker thread that sends out an email on a set schedule. I have a lot of back-end functions that go into this function, but I've setup a model that handles managing data for each email. The model cycles through all tenants on our multi tenant database and grabs all users and builds emails tailored to them every day one minute after midnight. I use the gem sidekiq-scheduler

However, when I setup the worker thread through sidekiq_scheduler I get the following error:

NameError: uninitialized constant RecurringMailer::EmailScheduler /../lib/tasks/recurring_mailer.rb:6:in `perform'

Here's my worker class RecurringMailer (lib/tasks/recurring_mailer.rb)

require 'sidekiq-scheduler'
class RecurringMailer
  include Sidekiq::Worker

  def perform
    EmailScheduler.daily_scheduler
    if Date::DAYTIME[Date.today.wday] == "Tuesday"
      EmailScheduler.weekly_scheduler
    end
  end
end

The error is thrown when the worker thread tries to first access the method from the model at EmailScheduler.daily_scheduler

Here's the basic shell of the EmailScheduler model (app/assets/models/email_scheduler.rb)

class EmailScheduler < ApplicationRecord
  def self.weekly_scheduler
    ...
  end

  def self.daily_scheduler
    ...
  end
end

Here's my config/application.rb file

module MySite
  class Application < Rails::Application
    config.autoload_paths += %W(#{config.root}/lib)
    config.autoload_paths << Rails.root.join('app', 'field_types')
    config.active_job.queue_adapter = :sidekiq
  end
end

I have a yaml file setup to manage starting the scheduler on sidekiq starup (config/sidekiq.yml)

:schedule:
  recurring_mailer:
    cron: '1 0 * * *'
    class: RecurringMailer

And when I start the server, I run

bundle exec sidekiq -r ./lib/tasks/recurring_mailer.rb

Any ideas on how to get my models to play nicely with sidekiq workers?

Upvotes: 1

Views: 805

Answers (1)

petarstrainovic
petarstrainovic

Reputation: 49

If you want to access your models you shouldn't run sidekiq with -r flag. Here is what docs say about it:

-r, --require [PATH|DIR] Location of Rails application with workers or file to require

If you want to load the whole app just run it as:

bundle exec sidekiq

Upvotes: 3

Related Questions