Commit 2d652fcf authored by Kamil Trzcinski's avatar Kamil Trzcinski

Update notification code

parent 3f5191de
...@@ -127,17 +127,15 @@ module Ci ...@@ -127,17 +127,15 @@ module Ci
def tick_runner_queue def tick_runner_queue
SecureRandom.hex.tap do |new_update| SecureRandom.hex.tap do |new_update|
::Gitlab::Workhorse.ensure_and_notify(runner_queue_key, new_update, ::Gitlab::Workhorse.set_key_and_notify(runner_queue_key, new_update,
expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: true) expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: true)
end end
end end
def ensure_runner_queue_value def ensure_runner_queue_value
Gitlab::Redis.with do |redis| new_value = SecureRandom.hex
new_value = SecureRandom.hex ::Gitlab::Workhorse.set_key_and_notify(runner_queue_key, new_value,
::Gitlab::Workhorse.ensure_and_notify(runner_queue_key, new_value, expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: false)
expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: false)
end
end end
def is_runner_queue_value_latest?(value) def is_runner_queue_value_latest?(value)
......
...@@ -155,12 +155,11 @@ module Gitlab ...@@ -155,12 +155,11 @@ module Gitlab
Rails.root.join('.gitlab_workhorse_secret') Rails.root.join('.gitlab_workhorse_secret')
end end
def ensure_and_notify(key, value, expire: nil, overwrite: true) def set_key_and_notify(key, value, expire: nil, overwrite: true)
Gitlab::Redis.with do |redis| Gitlab::Redis.with do |redis|
result = redis.set(key, value, ex: expire, nx: !overwrite) result = redis.set(key, value, ex: expire, nx: !overwrite)
if result if result
payload = "#{key}=#{value}" redis.publish(NOTIFICATION_CHANNEL, "#{key}=#{value}")
redis.publish(NOTIFICATION_CHANNEL, payload)
value value
else else
redis.get(key) redis.get(key)
......
...@@ -200,11 +200,11 @@ describe Gitlab::Workhorse, lib: true do ...@@ -200,11 +200,11 @@ describe Gitlab::Workhorse, lib: true do
end end
end end
describe '.ensure_and_notify' do describe '.set_key_and_notify' do
let(:key) { 'test-key' } let(:key) { 'test-key' }
let(:value) { 'test-value' } let(:value) { 'test-value' }
subject { described_class.ensure_and_notify(key, value, overwrite: overwrite) } subject { described_class.set_key_and_notify(key, value, overwrite: overwrite) }
shared_examples 'set and notify' do shared_examples 'set and notify' do
it 'set and return the same value' do it 'set and return the same value' do
...@@ -245,7 +245,7 @@ describe Gitlab::Workhorse, lib: true do ...@@ -245,7 +245,7 @@ describe Gitlab::Workhorse, lib: true do
is_expected.to eq(old_value) is_expected.to eq(old_value)
end end
it 'set and notify' do it 'does not notify' do
expect_any_instance_of(Redis).not_to receive(:publish) expect_any_instance_of(Redis).not_to receive(:publish)
subject subject
......
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