From a3c2ae16022f6f049174128be65c7c1a2c71ff6c Mon Sep 17 00:00:00 2001 From: Chris C Cerami Date: Fri, 17 Feb 2017 11:39:52 -0500 Subject: [PATCH] Fallback to server time if Redis time won't work --- Gemfile | 2 +- lib/resque/data_store.rb | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index fcb617cda..236c342fb 100644 --- a/Gemfile +++ b/Gemfile @@ -18,4 +18,4 @@ gem "minitest", "4.7.0" gem "mocha", :require => false gem "rack-test", "~> 0.5" gem "rake" - +gem "pry" diff --git a/lib/resque/data_store.rb b/lib/resque/data_store.rb index 2dc8f21aa..112e6caa9 100644 --- a/lib/resque/data_store.rb +++ b/lib/resque/data_store.rb @@ -7,11 +7,12 @@ class DataStore HEARTBEAT_KEY = "workers:heartbeat" def initialize(redis) - @redis = redis - @queue_access = QueueAccess.new(@redis) - @failed_queue_access = FailedQueueAccess.new(@redis) - @workers = Workers.new(@redis) - @stats_access = StatsAccess.new(@redis) + @redis = redis + @queue_access = QueueAccess.new(@redis) + @failed_queue_access = FailedQueueAccess.new(@redis) + @workers = Workers.new(@redis) + @stats_access = StatsAccess.new(@redis) + @redis_time_available = redis_time_available? end def_delegators :@queue_access, :push_to_queue, @@ -91,10 +92,17 @@ def all_resque_keys end def server_time - time, _ = @redis.time + time, _ = @redis_time_available ? @redis.time : Time.now Time.at(time) end + def redis_time_available? + @redis.time + rescue Redis::CommandError + false + end + private :redis_time_available? + class QueueAccess def initialize(redis) @redis = redis @@ -299,7 +307,6 @@ def redis_key_for_worker(worker) def redis_key_for_worker_start_time(worker) "#{redis_key_for_worker(worker)}:started" end - end class StatsAccess