lib/rails_failover/redis/handler.rb in rails_failover-0.5.9 vs lib/rails_failover/redis/handler.rb in rails_failover-0.6.0
- old
+ new
@@ -33,29 +33,17 @@
def ensure_failover_thread_running
return if @thread&.alive?
logger&.warn "Failover for Redis has been initiated"
- begin
- RailsFailover::Redis.on_failover_callback&.call
- rescue => e
- logger&.warn("RailsFailover::Redis.on_failover_callback failed: #{e.class} #{e.message}\n#{e.backtrace.join("\n")}")
- end
-
@thread = Thread.new do
loop do
ensure_primary_clients_disconnected
try_fallback_to_primary
if all_primaries_up
logger&.warn "Fallback to primary for Redis has been completed."
-
- begin
- RailsFailover::Redis.on_fallback_callback&.call
- rescue => e
- logger&.warn("RailsFailover::Redis.on_fallback_callback failed: #{e.class} #{e.message}\n#{e.backtrace.join("\n")}")
- end
break
end
end
end
end
@@ -126,25 +114,35 @@
mon_synchronize do
primaries_down[options[:id]]
end
end
+ def primaries_down_count
+ mon_synchronize do
+ primaries_down.count
+ end
+ end
+
private
def all_primaries_up
mon_synchronize { primaries_down.empty? }
end
def primary_up(options)
- mon_synchronize do
- primaries_down.delete(options[:id])
+ already_up = mon_synchronize do
+ !primaries_down.delete(options[:id])
end
+ RailsFailover::Redis.on_fallback_callback!(options[:id]) if !already_up
end
def primary_down(options)
+ already_down = false
mon_synchronize do
+ already_down = !!primaries_down[options[:id]]
primaries_down[options[:id]] = options.dup
end
+ RailsFailover::Redis.on_failover_callback!(options[:id]) if !already_down
end
def clients
process_pid = Process.pid
return @clients[process_pid] if @clients[process_pid]