From 2b27a98db32d788796c4495f5405ba09436fd8ce Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 28 Feb 2017 12:07:04 +0100 Subject: Add support for Workhorse notifications --- app/models/ci/runner.rb | 11 +++++------ .../use-redis-channel-to-post-runner-notifcations.yml | 4 ++++ lib/gitlab/workhorse.rb | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 changelogs/unreleased/use-redis-channel-to-post-runner-notifcations.yml diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb index 4863c34a6a6..34ffc4b77fc 100644 --- a/app/models/ci/runner.rb +++ b/app/models/ci/runner.rb @@ -127,17 +127,16 @@ module Ci def tick_runner_queue SecureRandom.hex.tap do |new_update| - Gitlab::Redis.with do |redis| - redis.set(runner_queue_key, new_update, ex: RUNNER_QUEUE_EXPIRY_TIME) - end + ::Gitlab::Workhorse.ensure_and_notify(runner_queue_key, new_update, + expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: true) end end def ensure_runner_queue_value Gitlab::Redis.with do |redis| - value = SecureRandom.hex - redis.set(runner_queue_key, value, ex: RUNNER_QUEUE_EXPIRY_TIME, nx: true) - redis.get(runner_queue_key) + new_value = SecureRandom.hex + ::Gitlab::Workhorse.ensure_and_notify(runner_queue_key, new_value, + expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: false) end end diff --git a/changelogs/unreleased/use-redis-channel-to-post-runner-notifcations.yml b/changelogs/unreleased/use-redis-channel-to-post-runner-notifcations.yml new file mode 100644 index 00000000000..ff5a58f6232 --- /dev/null +++ b/changelogs/unreleased/use-redis-channel-to-post-runner-notifcations.yml @@ -0,0 +1,4 @@ +--- +title: Use redis channel to post notifications +merge_request: +author: diff --git a/lib/gitlab/workhorse.rb b/lib/gitlab/workhorse.rb index 3ff9f9eb5e7..ca4eba48a8b 100644 --- a/lib/gitlab/workhorse.rb +++ b/lib/gitlab/workhorse.rb @@ -8,6 +8,7 @@ module Gitlab VERSION_FILE = 'GITLAB_WORKHORSE_VERSION'.freeze INTERNAL_API_CONTENT_TYPE = 'application/vnd.gitlab-workhorse+json'.freeze INTERNAL_API_REQUEST_HEADER = 'Gitlab-Workhorse-Api-Request'.freeze + NOTIFICATION_CHANNEL = 'workhorse:notifications'.freeze # Supposedly the effective key size for HMAC-SHA256 is 256 bits, i.e. 32 # bytes https://tools.ietf.org/html/rfc4868#section-2.6 @@ -154,6 +155,19 @@ module Gitlab Rails.root.join('.gitlab_workhorse_secret') end + def ensure_and_notify(key, value, expire: nil, overwrite: true) + Gitlab::Redis.with do |redis| + result = redis.set(key, value, ex: expire, nx: !overwrite) + if result + payload = "#{key}=#{value}" + redis.publish(RUNNER_NOTIFICATION_CHANNEL, payload) + value + else + redis.get(key) + end + end + end + protected def encode(hash) -- cgit v1.2.1