Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
gitlab-ce
Commits
3d26a8d0
Commit
3d26a8d0
authored
Feb 15, 2017
by
Tomasz Maczukin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add jobs requesting API
parent
b8ca9bc4
Changes
4
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
362 additions
and
0 deletions
+362
-0
lib/api/entities.rb
lib/api/entities.rb
+38
-0
lib/api/helpers/runner.rb
lib/api/helpers/runner.rb
+28
-0
lib/api/runner.rb
lib/api/runner.rb
+39
-0
spec/requests/api/runner_spec.rb
spec/requests/api/runner_spec.rb
+257
-0
No files found.
lib/api/entities.rb
View file @
3d26a8d0
...
...
@@ -697,5 +697,43 @@ module API
expose
:id
,
:message
,
:starts_at
,
:ends_at
,
:color
,
:font
expose
:active?
,
as: :active
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
lib/api/helpers/runner.rb
View file @
3d26a8d0
module
API
module
Helpers
module
Runner
UPDATE_RUNNER_EVERY
=
10
*
60
def
runner_registration_token_valid?
ActiveSupport
::
SecurityUtils
.
variable_size_secure_compare
(
params
[
:token
],
current_application_settings
.
runners_registration_token
)
...
...
@@ -18,6 +20,32 @@ module API
def
current_runner
@runner
||=
::
Ci
::
Runner
.
find_by_token
(
params
[
:token
].
to_s
)
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
lib/api/runner.rb
View file @
3d26a8d0
...
...
@@ -48,5 +48,44 @@ module API
Ci
::
Runner
.
find_by_token
(
params
[
:token
]).
destroy
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
spec/requests/api/runner_spec.rb
View file @
3d26a8d0
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment