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

Check cache in Ci::Runner#online?

parent b1c40590
......@@ -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
......
......@@ -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
......
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