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,28 +95,58 @@ describe Ci::Runner do ...@@ -95,28 +95,58 @@ describe Ci::Runner do
subject { runner.online? } subject { runner.online? }
context 'never contacted' do context 'no cache value' do
before do before do
runner.contacted_at = nil stub_redis("#{runner.runner_info_key}:contacted_at", nil)
end end
it { is_expected.to be_falsey } context 'never contacted' do
end before do
runner.contacted_at = nil
end
context 'contacted long time ago time' do it { is_expected.to be_falsey }
before do end
runner.contacted_at = 1.year.ago
context 'contacted long time ago time' do
before do
runner.contacted_at = 1.year.ago
end
it { is_expected.to be_falsey }
end 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 end
context 'contacted 1s ago' do context 'with cache value' do
before do context 'contacted long time ago time' do
runner.contacted_at = 1.second.ago 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
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
end end
......
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