Commit 3d26a8d0 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Add jobs requesting API

parent b8ca9bc4
...@@ -697,5 +697,43 @@ module API ...@@ -697,5 +697,43 @@ module API
expose :id, :message, :starts_at, :ends_at, :color, :font expose :id, :message, :starts_at, :ends_at, :color, :font
expose :active?, as: :active expose :active?, as: :active
end end
class ArtifactFile < Grape::Entity
expose :filename, :size
end
class JobCredentials < Grape::Entity
expose :type, :url, :username, :password
end
class JobResponse < Grape::Entity
expose :id, :ref, :tag, :sha, :status
expose :name, :token, :stage
expose :project_id
expose :project_name
expose :artifacts_file, using: ArtifactFile, if: ->(build, _) { build.artifacts? }
end
class RequestJobResponse < JobResponse
expose :commands
expose :repo_url
expose :before_sha
expose :allow_git_fetch
expose :token
expose :artifacts_expire_at, if: ->(build, _) { build.artifacts? }
expose :options do |model|
model.options
end
expose :timeout do |model|
model.timeout
end
expose :variables
expose :depends_on_builds, using: JobResponse
expose :credentials, using: JobCredentials
end
end end
end end
module API module API
module Helpers module Helpers
module Runner module Runner
UPDATE_RUNNER_EVERY = 10 * 60
def runner_registration_token_valid? def runner_registration_token_valid?
ActiveSupport::SecurityUtils.variable_size_secure_compare(params[:token], ActiveSupport::SecurityUtils.variable_size_secure_compare(params[:token],
current_application_settings.runners_registration_token) current_application_settings.runners_registration_token)
...@@ -18,6 +20,32 @@ module API ...@@ -18,6 +20,32 @@ module API
def current_runner def current_runner
@runner ||= ::Ci::Runner.find_by_token(params[:token].to_s) @runner ||= ::Ci::Runner.find_by_token(params[:token].to_s)
end end
def update_runner_info
return unless update_runner?
current_runner.contacted_at = Time.now
current_runner.assign_attributes(get_runner_version_from_params)
current_runner.save if current_runner.changed?
end
def update_runner?
# Use a random threshold to prevent beating DB updates.
# It generates a distribution between [40m, 80m].
#
contacted_at_max_age = UPDATE_RUNNER_EVERY + Random.rand(UPDATE_RUNNER_EVERY)
current_runner.contacted_at.nil? ||
(Time.now - current_runner.contacted_at) >= contacted_at_max_age
end
def build_not_found!
if headers['User-Agent'].to_s.match(/gitlab(-ci-multi)?-runner \d+\.\d+\.\d+(~beta\.\d+\.g[0-9a-f]+)? /)
no_content!
else
not_found!
end
end
end end
end end
end end
...@@ -48,5 +48,44 @@ module API ...@@ -48,5 +48,44 @@ module API
Ci::Runner.find_by_token(params[:token]).destroy Ci::Runner.find_by_token(params[:token]).destroy
end end
end end
resource :jobs do
desc 'Request a job' do
success Entities::RequestJobResponse
end
params do
requires :token, type: String, desc: %q(Runner's authentication token)
end
post '/request' do
authenticate_runner!
not_found! unless current_runner.active?
update_runner_info
if current_runner.is_runner_queue_value_latest?(params[:last_update])
header 'X-GitLab-Last-Update', params[:last_update]
Gitlab::Metrics.add_event(:build_not_found_cached)
return build_not_found!
end
new_update = current_runner.ensure_runner_queue_value
result = ::Ci::RegisterBuildService.new(current_runner).execute
if result.valid?
if result.build
Gitlab::Metrics.add_event(:build_found,
project: result.build.project.path_with_namespace)
present result.build, with: Entities::RequestJobResponse
else
Gitlab::Metrics.add_event(:build_not_found)
header 'X-GitLab-Last-Update', new_update
build_not_found!
end
else
# We received build that is invalid due to concurrency conflict
Gitlab::Metrics.add_event(:build_invalid)
conflict!
end
end
end
end end
end end
This diff is collapsed.
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