summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/ci/runner.rb9
-rw-r--r--spec/models/ci/runner_spec.rb54
2 files changed, 50 insertions, 13 deletions
diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb
index dcbb397fb78..7e616ee9144 100644
--- a/app/models/ci/runner.rb
+++ b/app/models/ci/runner.rb
@@ -88,7 +88,10 @@ module Ci
end
def online?
- contacted_at && contacted_at > self.class.contact_time_deadline
+ Gitlab::Redis::SharedState.with do |redis|
+ last_seen = redis.get("#{self.runner_info_key}:contacted_at") || contacted_at
+ last_seen && last_seen > self.class.contact_time_deadline
+ end
end
def status
@@ -152,6 +155,10 @@ module Ci
ensure_runner_queue_value == value if value.present?
end
+ def runner_info_key
+ "runner:info:#{self.id}"
+ end
+
private
def cleanup_runner_queue
diff --git a/spec/models/ci/runner_spec.rb b/spec/models/ci/runner_spec.rb
index b2b64e6ff48..fe9e5ec9436 100644
--- a/spec/models/ci/runner_spec.rb
+++ b/spec/models/ci/runner_spec.rb
@@ -95,28 +95,58 @@ describe Ci::Runner do
subject { runner.online? }
- context 'never contacted' do
+ context 'no cache value' do
before do
- runner.contacted_at = nil
+ stub_redis("#{runner.runner_info_key}:contacted_at", nil)
end
- it { is_expected.to be_falsey }
- end
+ context 'never contacted' do
+ before do
+ runner.contacted_at = nil
+ end
- context 'contacted long time ago time' do
- before do
- runner.contacted_at = 1.year.ago
+ it { is_expected.to be_falsey }
+ end
+
+ context 'contacted long time ago time' do
+ before do
+ runner.contacted_at = 1.year.ago
+ end
+
+ it { is_expected.to be_falsey }
end
- it { is_expected.to be_falsey }
+ context 'contacted 1s ago' do
+ before do
+ runner.contacted_at = 1.second.ago
+ end
+
+ it { is_expected.to be_truthy }
+ end
end
- context 'contacted 1s ago' do
- before do
- runner.contacted_at = 1.second.ago
+ context 'with cache value' do
+ context 'contacted long time ago time' do
+ before do
+ stub_redis("#{runner.runner_info_key}:contacted_at", 1.year.ago.to_s)
+ end
+
+ it { is_expected.to be_falsey }
+ end
+
+ context 'contacted 1s ago' do
+ before do
+ stub_redis("#{runner.runner_info_key}:contacted_at", 1.second.ago.to_s)
+ end
+
+ it { is_expected.to be_truthy }
end
+ end
- it { is_expected.to be_truthy }
+ def stub_redis(key, value)
+ redis = double
+ allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis)
+ allow(redis).to receive(:get).with(key).and_return(value)
end
end