Module: SidekiqAlive

Defined in:
lib/sidekiq_alive.rb,
lib/sidekiq_alive/config.rb,
lib/sidekiq_alive/server.rb,
lib/sidekiq_alive/worker.rb,
lib/sidekiq_alive/version.rb

Defined Under Namespace

Classes: Config, Server, Worker

Constant Summary collapse

VERSION =
'2.1.7'

Class Method Summary collapse

Class Method Details

.alive?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/sidekiq_alive.rb', line 93

def self.alive?
  redis.ttl(current_lifeness_key) != -2
end


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/sidekiq_alive.rb', line 131

def self.banner
  <<~BANNER

  =================== SidekiqAlive =================

  Hostname: #{hostname}
  Liveness key: #{current_lifeness_key}
  Port: #{config.port}
  Time to live: #{config.time_to_live}s
  Current instance register key: #{current_instance_register_key}
  Worker running on queue: #{@queue}


  starting ...
  BANNER
end

.configObject



107
108
109
# File 'lib/sidekiq_alive.rb', line 107

def self.config
  @config ||= SidekiqAlive::Config.instance
end

.current_instance_register_keyObject



79
80
81
# File 'lib/sidekiq_alive.rb', line 79

def self.current_instance_register_key
  "#{config.registered_instance_key}::#{hostname}"
end

.current_lifeness_keyObject



111
112
113
# File 'lib/sidekiq_alive.rb', line 111

def self.current_lifeness_key
  "#{config.liveness_key}::#{hostname}"
end

.current_queueObject



39
40
41
# File 'lib/sidekiq_alive.rb', line 39

def self.current_queue
  "#{config.queue_prefix}-#{hostname}"
end

.deep_scan(keyword, keys = [], cursor = 0) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/sidekiq_alive.rb', line 58

def self.deep_scan(keyword, keys = [], cursor = 0)
  loop do
    cursor, found_keys = SidekiqAlive.redis.scan(cursor, match: keyword, count: 1000)
    keys += found_keys if found_keys
    break if cursor.to_i == 0
  end
  keys
end

.hostnameObject



115
116
117
# File 'lib/sidekiq_alive.rb', line 115

def self.hostname
  ENV['HOSTNAME'] || 'HOSTNAME_NOT_SET'
end

.loggerObject



103
104
105
# File 'lib/sidekiq_alive.rb', line 103

def self.logger
  Sidekiq.logger
end

.purge_pending_jobsObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sidekiq_alive.rb', line 67

def self.purge_pending_jobs
  # TODO:
  # Sidekiq 6 allows better way to find scheduled jobs:
  # https://github.com/mperham/sidekiq/wiki/API#scan
  scheduled_set = Sidekiq::ScheduledSet.new
  jobs = scheduled_set.select { |job| job.klass == 'SidekiqAlive::Worker' && job.queue == current_queue }
  logger.info("[SidekiqAlive] Purging #{jobs.count} pending for #{hostname}")
  jobs.each(&:delete)
  logger.info("[SidekiqAlive] Removing queue #{current_queue}")
  Sidekiq::Queue.new(current_queue).clear
end

.redisObject



89
90
91
# File 'lib/sidekiq_alive.rb', line 89

def self.redis
  Sidekiq.redis { |r| r }
end

.register_current_instanceObject



43
44
45
# File 'lib/sidekiq_alive.rb', line 43

def self.register_current_instance
  register_instance(current_instance_register_key)
end

.register_instance(instance_name) ⇒ Object



158
159
160
161
162
# File 'lib/sidekiq_alive.rb', line 158

def self.register_instance(instance_name)
  redis.set(instance_name,
            Time.now.to_i,
            ex: config.registration_ttl.to_i)
end

.registered_instancesObject



54
55
56
# File 'lib/sidekiq_alive.rb', line 54

def self.registered_instances
  deep_scan("#{config.registered_instance_key}::*")
end

.setup {|config| ... } ⇒ Object

CONFIG —————————————

Yields:



99
100
101
# File 'lib/sidekiq_alive.rb', line 99

def self.setup
  yield(config)
end

.shutdown_infoObject



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sidekiq_alive.rb', line 119

def self.shutdown_info
  <<~BANNER

  =================== Shutting down SidekiqAlive =================

  Hostname: #{hostname}
  Liveness key: #{current_lifeness_key}
  Current instance register key: #{current_instance_register_key}

  BANNER
end

.startObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sidekiq_alive.rb', line 8

def self.start
  SidekiqAlive::Worker.sidekiq_options queue: current_queue
  Sidekiq.configure_server do |sq_config|

    (sq_config.respond_to?(:[]) ? sq_config[:queues] : sq_config.options[:queues]).unshift(current_queue)

    sq_config.on(:startup) do
      SidekiqAlive.tap do |sa|
        sa.logger.info(banner)
        sa.register_current_instance
        sa.store_alive_key
        sa::Worker.perform_async(hostname)
        @server_pid = fork do
          sa::Server.run!
        end
        sa.logger.info(successful_startup_text)
      end
    end

    sq_config.on(:quiet) do
      SidekiqAlive.unregister_current_instance
    end

    sq_config.on(:shutdown) do
      Process.kill('TERM', @server_pid) unless @server_pid.nil?
      Process.wait(@server_pid) unless @server_pid.nil?
      SidekiqAlive.unregister_current_instance
    end
  end
end

.store_alive_keyObject



83
84
85
86
87
# File 'lib/sidekiq_alive.rb', line 83

def self.store_alive_key
  redis.set(current_lifeness_key,
            Time.now.to_i,
            ex: config.time_to_live.to_i)
end

.successful_startup_textObject



148
149
150
151
152
153
154
155
156
# File 'lib/sidekiq_alive.rb', line 148

def self.successful_startup_text
  <<~BANNER
  Registered instances:

  - #{registered_instances.join("\n\s\s- ")}

  =================== SidekiqAlive Ready! =================
  BANNER
end

.unregister_current_instanceObject



47
48
49
50
51
52
# File 'lib/sidekiq_alive.rb', line 47

def self.unregister_current_instance
  # Delete any pending jobs for this instance
  logger.info(shutdown_info)
  purge_pending_jobs
  redis.del(current_instance_register_key)
end