Commit 31af6be0 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Fix specs

parent 1c7eb963
...@@ -22,7 +22,7 @@ module Ci ...@@ -22,7 +22,7 @@ module Ci
scope :online, ->() { where('contacted_at > ?', LAST_CONTACT_TIME) } scope :online, ->() { where('contacted_at > ?', LAST_CONTACT_TIME) }
scope :ordered, ->() { order(id: :desc) } scope :ordered, ->() { order(id: :desc) }
after_save :tick_runner_queue after_save :tick_runner_queue, if: :form_editable_changed?
scope :owned_or_shared, ->(project_id) do scope :owned_or_shared, ->(project_id) do
joins('LEFT JOIN ci_runner_projects ON ci_runner_projects.runner_id = ci_runners.id') joins('LEFT JOIN ci_runner_projects ON ci_runner_projects.runner_id = ci_runners.id')
...@@ -126,14 +126,14 @@ module Ci ...@@ -126,14 +126,14 @@ module Ci
end end
def tick_runner_queue def tick_runner_queue
new_update = Time.new.inspect new_update = SecureRandom.hex
Gitlab::Redis.with { |redis| redis.set(runner_queue_key, new_update, ex: RUNNER_QUEUE_EXPIRY_TIME) } Gitlab::Redis.with { |redis| redis.set(runner_queue_key, new_update, ex: RUNNER_QUEUE_EXPIRY_TIME) }
new_update new_update
end end
def ensure_runner_queue_value def ensure_runner_queue_value
Gitlab::Redis.with do |redis| Gitlab::Redis.with do |redis|
value = Time.new.inspect value = SecureRandom.hex
redis.set(runner_queue_key, value, ex: RUNNER_QUEUE_EXPIRY_TIME, nx: true) redis.set(runner_queue_key, value, ex: RUNNER_QUEUE_EXPIRY_TIME, nx: true)
redis.get(runner_queue_key) redis.get(runner_queue_key)
end end
...@@ -149,6 +149,12 @@ module Ci ...@@ -149,6 +149,12 @@ module Ci
"runner:build_queue:#{self.token}" "runner:build_queue:#{self.token}"
end end
def form_editable_changed?
FORM_EDITABLE.any? do |editable|
public_send("#{editable}_changed?")
end
end
def tag_constraints def tag_constraints
unless has_tags? || run_untagged? unless has_tags? || run_untagged?
errors.add(:tags_list, errors.add(:tags_list,
......
module Ci module Ci
class UpdateBuildQueueService class UpdateBuildQueueService
def execute(build) def execute(build)
build.project.runners.select do |runner| build.project.runners.each do |runner|
if runner.can_pick?(build) if runner.can_pick?(build)
runner.tick_runner_queue runner.tick_runner_queue
end end
......
...@@ -226,7 +226,7 @@ module API ...@@ -226,7 +226,7 @@ module API
end end
def render_api_error!(message, status) def render_api_error!(message, status)
error!({ 'message' => message }, status, header) error!({ 'message' => message }, status)
end end
def handle_api_exception(exception) def handle_api_exception(exception)
......
...@@ -277,21 +277,44 @@ describe Ci::Runner, models: true do ...@@ -277,21 +277,44 @@ describe Ci::Runner, models: true do
it 'sets a new last_update value when it is called the first time' do it 'sets a new last_update value when it is called the first time' do
last_update = runner.ensure_runner_queue_value last_update = runner.ensure_runner_queue_value
expect_value_in_redis(last_update) expect_value_in_redis.to eq(last_update)
end end
it 'does not change if it is not expired and called again' do it 'does not change if it is not expired and called again' do
last_update = runner.ensure_runner_queue_value last_update = runner.ensure_runner_queue_value
expect(runner.ensure_runner_queue_value).to eq(last_update) expect(runner.ensure_runner_queue_value).to eq(last_update)
expect_value_in_redis(last_update) expect_value_in_redis.to eq(last_update)
end end
def expect_value_in_redis(last_update) context 'updates runner queue after changing editable value' do
let!(:last_update) { runner.ensure_runner_queue_value }
before do
runner.update(description: 'new runner')
end
it 'sets a new last_update value' do
expect_value_in_redis.not_to eq(last_update)
end
end
context 'does not update runner value after save' do
let!(:last_update) { runner.ensure_runner_queue_value }
before do
runner.touch
end
it 'has an old last_update value' do
expect_value_in_redis.to eq(last_update)
end
end
def expect_value_in_redis
Gitlab::Redis.with do |redis| Gitlab::Redis.with do |redis|
runner_queue_key = runner.send(:runner_queue_key) runner_queue_key = runner.send(:runner_queue_key)
expect(redis.get(runner_queue_key))
expect(redis.get(runner_queue_key)).to eq(last_update)
end end
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