Commit 0be8b3cd authored by Matija Čupić's avatar Matija Čupić

Check cache in Ci::Runner#online?

parent b1c40590
...@@ -88,7 +88,10 @@ module Ci ...@@ -88,7 +88,10 @@ module Ci
end end
def online? 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 end
def status def status
...@@ -152,6 +155,10 @@ module Ci ...@@ -152,6 +155,10 @@ module Ci
ensure_runner_queue_value == value if value.present? ensure_runner_queue_value == value if value.present?
end end
def runner_info_key
"runner:info:#{self.id}"
end
private private
def cleanup_runner_queue def cleanup_runner_queue
......
...@@ -95,6 +95,11 @@ describe Ci::Runner do ...@@ -95,6 +95,11 @@ describe Ci::Runner do
subject { runner.online? } subject { runner.online? }
context 'no cache value' do
before do
stub_redis("#{runner.runner_info_key}:contacted_at", nil)
end
context 'never contacted' do context 'never contacted' do
before do before do
runner.contacted_at = nil runner.contacted_at = nil
...@@ -120,6 +125,31 @@ describe Ci::Runner do ...@@ -120,6 +125,31 @@ describe Ci::Runner do
end end
end end
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
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
describe '#can_pick?' do describe '#can_pick?' do
let(:pipeline) { create(:ci_pipeline) } let(:pipeline) { create(:ci_pipeline) }
let(:build) { create(:ci_build, pipeline: pipeline) } let(:build) { create(:ci_build, pipeline: pipeline) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment