Search
Close this search box.

A test to avoid class name misspelled in Sidekiq Periodic Jobs

sidekiq-rails.width-808

In Sidekiq Enterprise, it supports periodic jobs (aka cron or recurring jobs) to register your periodic jobs with a schedule upon startup and Sidekiq will fire off corresponding jobs on that schedule.

However, we may misspell the class name at the register file and it would make Sidekiq not execute the periodic jobs. In this article, I’m going to share an example of a test to check your class name correctness at the Sidekiq register file.

Here is the example of the Sidekiq periodic jobs register file, and I misspelled the class name from SomeHourlyWorkerClass to SomeHourlyWorkerClasss

# config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
  config.periodic do |mgr|
    mgr.register('0 * * * *', "SomeHourlyWorkerClasss") # misspelled
    mgr.register('* * * * *', "SomeWorkerClass", retry: 2, queue: 'foo')
  end
end

and I run bundle exec sidekiq to start the sidekiq worker, it would show the errors and Sidekiq will not execute the periodic jobs.

❯ bundle exec sidekiq
2021-07-28T13:30:56.720Z 25890 TID-oway1dvum INFO: Booting Sidekiq 5.2.7 with redis options {:url=>nil, :id=>"Sidekiq-server-PID-25890"}
uninitialized constant SomeHourlyWorkerClasss

To avoid this situation, we move the jobs to a new file called lib/periodic_jobs.rb to make the jobs testable.

# lib/periodic_jobs.rb
PERIODIC_JOBS = ->(mgr) {
  mgr.register('0 * * * *', "SomeHourlyWorkerClasss") # misspelled
  mgr.register('* * * * *', "SomeWorkerClass", retry: 2, queue: 'foo')
}

and require the periodic_jobs.rb at sidekiq config file.

Sidekiq.configure_server do |config|
  require 'periodic_jobs'
  # file is in lib/periodic_jobs.rb
  config.periodic(&PERIODIC_JOBS)
end

We also need to create a test to check class name correctness, at spec/lib/periodic_jobs_spec.rb

# spec/lib/periodic_jobs_spec.rb
class Verifier
  def register(_sched, name, *_rest)
    name.constantize if name.is_a?(String)
  end
end

describe "PERIODIC_JOBS" do
  it "verify workers' clasname are valid" do
    require "periodic_jobs"
    PERIODIC_JOBS.call(Verifier.new)
  end
end

run the test with command rspec spec/lib/periodic_jobs_spec.rb

Randomized with seed 2330
F

Failures:

  1) PERIODIC_JOBS verify workers' clasname are valid
     Failure/Error: mgr.register('0 * * * *', "SomeHourlyWorkerClasss")
     
     NameError:
       uninitialized constant SomeHourlyWorkerClasss
     # ./lib/periodic_jobs.rb:16:in `block in <top (required)>'
     # ./spec/lib/periodic_jobs_spec.rb:10:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:115:in `block (3 levels) in <top (required)>'
     # ./spec/spec_helper.rb:114:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:109:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:101:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # NameError:
     #   uninitialized constant SomeHourlyWorkerClasss
     #   ./lib/periodic_jobs.rb:16:in `block in <top (required)>'

Top 1 slowest examples (0.04514 seconds, 5.2% of total time):
  PERIODIC_JOBS verify workers' clasname are valid
    0.04514 seconds ./spec/lib/periodic_jobs_spec.rb:8

Finished in 0.86432 seconds (files took 2 minutes 1.2 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/lib/periodic_jobs_spec.rb:8 # PERIODIC_JOBS verify workers' clasname are valid

Randomized with seed 2330

The spec will be pointed out which class name is an error, you can also integrate this test to your CI/CD to prevent class name misspelled.

Twitter
Telegram
Facebook
LinkedIn
Email

21 thoughts on “A test to avoid class name misspelled in Sidekiq Periodic Jobs”

  1. FREE MINING COIN ON PHONE
    REMITANO the largest cryptocurrency exchange in Southeast Asia
    Free RENEC COINS MINING on your phone
    Link Earn RENEC: https://bit.ly/3mDpGn7
    With only 3 steps and 30 seconds a day, you can mine RENEC coin completely free. RENEC coin value can be up to $50/coin

  2. During this period of uncertainty in the crypto market, we need the help of an expert in the form of a financial advisor that specializes in beating the market makers and maximizing profits for clients. Contact Mr Kyle Morgan on whatsapp +12087322211

  3. So sweet of you for sharing them! I am waiting for more of these. please keep continue to have posts of these kind to share with everyone It will help us a lot.

  4. Cassie Steele

    How kind of you to share them! I’m looking forward to drift boss more of these. Please keep publishing articles of like nature for everyone to read. It will be highly helpful.

Leave a Comment

Your email address will not be published. Required fields are marked *