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.
21 thoughts on “A test to avoid class name misspelled in Sidekiq Periodic Jobs”
A couple of days ago, Tesla CEO Elon Musk suggested that his company might sell its bitcoin holdings, causing the cryptocurrency to plummet.
sourse: https://bitcoindata.org/tesla_turns_away_from_bitcoin/
This follows Musk’s announcement earlier this month that his company will no longer accept Bitcoin as payment for its electric vehicles due to the fossil fuels needed to create a digital currency.
Hey! Have you heard about Unifarm? Stake and farm with unifarm for up to 250 APY and get value for your investment. It’s strong and reliable. http://www.unifarm.co
Hope to see qureka Lite company asking this mining company to unite more marketable live line like paxful company is doing with game cards in the store
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
METAMUSK team is launching the first token Airdrop of 500kk METAMUSK (≈2.5 BNB). Get on official website –> https://muskmetaverse.me
Hope to see qureka Lite company asking this mining company to unite more marketable live line like paxful company is doing with game cards in the store https://nasilsite.com/
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
He is trustworthy. Verified ✅
A couple of days ago, Tesla CEO Elon Musk suggested that his company might sell its bitcoin holdings, causing the cryptocurrency to plummet. https://www.bizdekalmasin.com/coin-gelecegi/mbl-coin-yorum-moviebloc-coin-gelecegi/
You saved me from a lot of trouble. Really appreciate your work.
A very useful article. Helped me alot in a bunch of ways.
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.
i try for like it
I was looking for this! Thank you.
If you have to deal with argumentative research paper topics to select the most appealing one, remember that the struggle is real. Browse the website to get the most exciting ideas, read well-structured samples, and get inspired for your work. https://superbgrade.com/blog/30-good-argumentative-research-paper-topics-and-ideas
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.
So sweet of you for sharing them! Thanks!
Can I gather more relatable data from your web page? Please suggest to me a new & different web page which will deliver me time tracksuit unique ideas for the readers.
HackenProof
good share bro
wonderful tnx alot