A test to avoid class name misspelled in Sidekiq Periodic Jobs

Computing

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.

The post A test to avoid class name misspelled in Sidekiq Periodic Jobs appeared first on CoinGecko Blog.

You May Also Like