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.0.1'.freeze

Class Method Summary collapse

Class Method Details

.alive?Boolean


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

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

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sidekiq_alive.rb', line 121

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


97
98
99
# File 'lib/sidekiq_alive.rb', line 97

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

.current_instance_register_keyObject


69
70
71
# File 'lib/sidekiq_alive.rb', line 69

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

.current_lifeness_keyObject


101
102
103
# File 'lib/sidekiq_alive.rb', line 101

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

.current_queueObject


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

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

.hostnameObject


105
106
107
# File 'lib/sidekiq_alive.rb', line 105

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

.loggerObject


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

def self.logger
  Sidekiq.logger
end

.purge_pending_jobsObject


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

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


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

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

.register_current_instanceObject


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

def self.register_current_instance
  register_instance(current_instance_register_key)
end

.register_instance(instance_name) ⇒ Object


148
149
150
151
152
# File 'lib/sidekiq_alive.rb', line 148

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

.registered_instancesObject


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

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

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

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

Yields:


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

def self.setup
  yield(config)
end

.shutdown_infoObject


109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sidekiq_alive.rb', line 109

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
# 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.options[:queues] << 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


73
74
75
76
77
# File 'lib/sidekiq_alive.rb', line 73

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


138
139
140
141
142
143
144
145
146
# File 'lib/sidekiq_alive.rb', line 138

def self.successful_startup_text
  <<~BANNER
  Registered instances:

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

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

.unregister_current_instanceObject


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

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