How to tell if sidekiq is connected to redis server?

大城市里の小女人 提交于 2019-12-04 16:28:15

问题


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

回答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.




回答2:


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.




回答3:


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



回答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



回答5:


begin
  MrWorker.perform_async('do_work', user.id)
rescue Redis::CannotConnectError => e
  MrWorker.new.perform('do_work', user.id)
end


来源:https://stackoverflow.com/questions/15843637/how-to-tell-if-sidekiq-is-connected-to-redis-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!