How to tell if sidekiq is connected to redis server?
10,329
Solution 1
You can use Redis info provided by Sidekiq:
redis_info = Sidekiq.redis { |conn| conn.info }
redis_info['connected_clients'] # => "16"
Took it from Sidekiq's Sinatra status app.
Solution 2
I make this method to Rails whit the obove answer, return true
if connected and false
if not.
def redis_connected?
!!Sidekiq.redis(&:info) rescue false
end
Solution 3
It sounds like you want to know if there is a Sidekiq process up and running to process jobs at a given point in time. With Sidekiq 3.0, you can do this:
require 'sidekiq/api'
ps = Sidekiq::ProcessSet.new
if ps.size > 0
MyWorker.perform_async(1,2,3)
else
MyWorker.new.perform(1,2,3)
end
Sidekiq::ProcessSet gives you almost real-time (updated every 5 sec) info about any running Sidekiq processes.
Solution 4
jumping off @overallduka's answer, for those using the okcomputer gem, this is the custom check i set up:
class SidekiqCheck < OkComputer::Check
def check
if sidekiq_accessible?
mark_message "ok"
else
mark_failure
end
end
private
def sidekiq_accessible?
begin
Sidekiq.redis { |conn| conn.info }
rescue Redis::CannotConnectError
end.present?
end
end
OkComputer::Registry.register "sidekiq", SidekiqCheck.new
Solution 5
begin
MrWorker.perform_async('do_work', user.id)
rescue Redis::CannotConnectError => e
MrWorker.new.perform('do_work', user.id)
end
Related videos on Youtube
Author by
keruilin
Updated on June 04, 2022Comments
-
keruilin almost 2 years
Using the console, how can I tell if sidekiq is connected to a redis server? I want to be able to do something like this:
if (sidekiq is connected to redis) # psuedo code MrWorker.perform_async('do_work', user.id) else MrWorker.new.perform('do_work', user.id) end
-
Leo Correa almost 11 yearsIf there's no redis connection or server available that
conn.info
would raise an exceptionRedis::CannotConnectError