Skip to content

Commit

Permalink
rename registry
Browse files Browse the repository at this point in the history
  • Loading branch information
arturictus committed Jul 21, 2023
1 parent f560973 commit f5a16cc
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/sidekiq_alive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
require "sidekiq_alive/helpers"
require "sidekiq_alive/redis"

SIDEKIQ_ALIVE_INDEX_NAME = "sidekiq-alive-index"

module SidekiqAlive
HOSTNAME_REGISTRY = "sidekiq-alive-hostnames"
class << self
def start
Sidekiq.configure_server do |sq_config|
Expand Down Expand Up @@ -66,13 +66,13 @@ def unregister_current_instance
# Delete any pending jobs for this instance
logger.info(shutdown_info)
purge_pending_jobs
redis.zrem(SIDEKIQ_ALIVE_INDEX_NAME, current_instance_register_key)
redis.zrem(HOSTNAME_REGISTRY, current_instance_register_key)
end

def registered_instances
# before we return we make sure we expire old keys
expire_old_keys
redis.zrange(SIDEKIQ_ALIVE_INDEX_NAME, 0, -1)
redis.zrange(HOSTNAME_REGISTRY, 0, -1)
end

def purge_pending_jobs
Expand Down Expand Up @@ -141,7 +141,7 @@ def startup_info
port: config.port,
ttl: config.time_to_live,
queue: current_queue,
register_set: SIDEKIQ_ALIVE_INDEX_NAME,
register_set: HOSTNAME_REGISTRY,
liveness_key: current_lifeness_key,
register_key: current_instance_register_key,
}
Expand All @@ -151,19 +151,19 @@ def startup_info

def successful_startup_text
"Successfully started sidekiq-alive, registered with key: "\
"#{current_instance_register_key} on set #{SIDEKIQ_ALIVE_INDEX_NAME}"
"#{current_instance_register_key} on set #{HOSTNAME_REGISTRY}"
end

def expire_old_keys
# we get every key that should be expired by now
keys_to_expire = redis.zrangebyscore(SIDEKIQ_ALIVE_INDEX_NAME, 0, Time.now.to_i)
keys_to_expire = redis.zrangebyscore(HOSTNAME_REGISTRY, 0, Time.now.to_i)
# then we remove it
keys_to_expire.each { |key| redis.zrem(SIDEKIQ_ALIVE_INDEX_NAME, key) }
keys_to_expire.each { |key| redis.zrem(HOSTNAME_REGISTRY, key) }
end

def register_instance(instance_name)
expiration = Time.now.to_i + config.registration_ttl.to_i
redis.zadd(SIDEKIQ_ALIVE_INDEX_NAME, expiration, instance_name)
redis.zadd(HOSTNAME_REGISTRY, expiration, instance_name)
expire_old_keys
end
end
Expand Down

0 comments on commit f5a16cc

Please sign in to comment.