mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
FEATURE: unicorn sidekiq will restart sidekiq on complete failure.
(checks every 30 minutes for complete failure)
This commit is contained in:
parent
4aaedb82d0
commit
7c57d74e85
16
app/jobs/regular/run_heartbeat.rb
Normal file
16
app/jobs/regular/run_heartbeat.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
module Jobs
|
||||
class RunHeartbeat < Jobs::Base
|
||||
|
||||
def self.heartbeat_key
|
||||
'heartbeat_last_run'
|
||||
end
|
||||
|
||||
def execute(args)
|
||||
$redis.set(self.class.heartbeat_key, Time.new.to_i.to_s)
|
||||
end
|
||||
|
||||
def self.last_heartbeat
|
||||
$redis.get(heartbeat_key).to_i
|
||||
end
|
||||
end
|
||||
end
|
11
app/jobs/scheduled/heartbeat.rb
Normal file
11
app/jobs/scheduled/heartbeat.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
module Jobs
|
||||
|
||||
# used to ensure at least 1 sidekiq is running correctly
|
||||
class Heartbeat < Jobs::Scheduled
|
||||
every 3.minute
|
||||
|
||||
def execute(args)
|
||||
Jobs.enqueue(:run_heartbeat, {})
|
||||
end
|
||||
end
|
||||
end
|
|
@ -72,6 +72,7 @@ before_fork do |server, worker|
|
|||
sidekiqs = ENV['UNICORN_SIDEKIQS'].to_i
|
||||
if sidekiqs > 0
|
||||
puts "Starting up #{sidekiqs} supervised sidekiqs"
|
||||
|
||||
require 'demon/sidekiq'
|
||||
|
||||
Demon::Sidekiq.start(sidekiqs)
|
||||
|
@ -79,8 +80,26 @@ before_fork do |server, worker|
|
|||
class ::Unicorn::HttpServer
|
||||
alias :master_sleep_orig :master_sleep
|
||||
|
||||
def check_sidekiq_heartbeat
|
||||
@sidekiq_heartbeat_interval ||= 30.minutes
|
||||
@sidekiq_next_heartbeat_check ||= Time.new.to_i + @sidekiq_heartbeat_interval
|
||||
|
||||
if @sidekiq_next_heartbeat_check < Time.new.to_i
|
||||
last_heartbeat = Jobs::RunHeartbeat.last_heartbeat
|
||||
if last_heartbeat < Time.new.to_i - @sidekiq_heartbeat_interval
|
||||
STDERR.puts "Sidekiq heartbeat test failed, restarting"
|
||||
puts "Sidekiq heartbeat test failed, restarting"
|
||||
|
||||
Demon::Sidekiq.restart
|
||||
end
|
||||
@sidekiq_next_heartbeat_check = Time.new.to_i + @sidekiq_heartbeat_interval
|
||||
end
|
||||
end
|
||||
|
||||
def master_sleep(sec)
|
||||
Demon::Sidekiq.ensure_running
|
||||
check_sidekiq_heartbeat
|
||||
|
||||
master_sleep_orig(sec)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,6 +17,14 @@ class Demon::Base
|
|||
end
|
||||
end
|
||||
|
||||
def self.restart
|
||||
return unless @demons
|
||||
@demons.values.each do |demon|
|
||||
demon.stop
|
||||
demon.start
|
||||
end
|
||||
end
|
||||
|
||||
def self.ensure_running
|
||||
@demons.values.each do |demon|
|
||||
demon.ensure_running
|
||||
|
@ -39,6 +47,7 @@ class Demon::Base
|
|||
if @pid
|
||||
Process.kill("HUP",@pid)
|
||||
@pid = nil
|
||||
@started = false
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user