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
Boxiang Sun
gitlab-ce
Commits
c92e1d73
Commit
c92e1d73
authored
Feb 05, 2018
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve runner attribute cachine
parent
3366f377
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
41 deletions
+38
-41
app/models/ci/runner.rb
app/models/ci/runner.rb
+22
-30
spec/models/ci/runner_spec.rb
spec/models/ci/runner_spec.rb
+15
-10
spec/requests/api/runner_spec.rb
spec/requests/api/runner_spec.rb
+1
-1
No files found.
app/models/ci/runner.rb
View file @
c92e1d73
...
...
@@ -2,12 +2,14 @@ module Ci
class
Runner
<
ActiveRecord
::
Base
extend
Gitlab
::
Ci
::
Model
include
Gitlab
::
SQL
::
Pattern
include
Gitlab
::
Utils
::
StrongMemoize
RUNNER_QUEUE_EXPIRY_TIME
=
60
.
minutes
ONLINE_CONTACT_TIMEOUT
=
1
.
hour
UPDATE_DB_RUNNER_INFO_EVERY
=
40
.
minutes
AVAILABLE_SCOPES
=
%w[specific shared active paused online]
.
freeze
FORM_EDITABLE
=
%i[description tag_list active run_untagged locked access_level]
.
freeze
CACHED_ATTRIBUTES_EXPIRY_TIME
=
24
.
hours
has_many
:builds
has_many
:runner_projects
,
dependent: :destroy
# rubocop:disable Cop/ActiveRecordDependent
...
...
@@ -68,29 +70,15 @@ module Ci
ONLINE_CONTACT_TIMEOUT
.
ago
end
def
name
cached_attribute
(
:name
)
||
read_attribute
(
:name
)
end
def
version
cached_attribute
(
:version
)
||
read_attribute
(
:version
)
end
def
revision
cached_attribute
(
:revision
)
||
read_attribute
(
:revision
)
end
def
platform
cached_attribute
(
:platform
)
||
read_attribute
(
:platform
)
end
def
architecture
cached_attribute
(
:architecture
)
||
read_attribute
(
:architecture
)
def
self
.
cached_attr_reader
(
*
attributes
)
attributes
.
each
do
|
attribute
|
define_method
(
"
#{
attribute
}
"
)
do
cached_attribute
(
attribute
)
||
read_attribute
(
attribute
)
end
end
end
def
contacted_at
cached_attribute
(
:contacted_at
)
||
read_attribute
(
:contacted_at
)
end
cached_attr_reader
:version
,
:revision
,
:platform
,
:architecture
,
:contacted_at
def
set_default_values
self
.
token
=
SecureRandom
.
hex
(
15
)
if
self
.
token
.
blank?
...
...
@@ -178,7 +166,7 @@ module Ci
end
def
update_cached_info
(
values
)
values
=
values
&
.
slice
(
:
name
,
:
version
,
:revision
,
:platform
,
:architecture
)
||
{}
values
=
values
&
.
slice
(
:version
,
:revision
,
:platform
,
:architecture
)
||
{}
values
[
:contacted_at
]
=
Time
.
now
cache_attributes
(
values
)
...
...
@@ -201,8 +189,8 @@ module Ci
"runner:build_queue:
#{
self
.
token
}
"
end
def
cache_attribute_key
(
key
)
"runner:info:
#{
self
.
id
}
:
#{
key
}
"
def
cache_attribute_key
"runner:info:
#{
self
.
id
}
"
end
def
persist_cached_data?
...
...
@@ -217,17 +205,21 @@ module Ci
end
def
cached_attribute
(
key
)
@cached_attributes
=
{}
@cached_attributes
[
key
]
||=
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis
.
get
(
cache_attribute_key
(
key
))
(
cached_attributes
||
{})[
key
]
end
def
cached_attributes
strong_memoize
(
:cached_attributes
)
do
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
data
=
redis
.
get
(
cache_attribute_key
)
JSON
.
parse
(
data
,
symbolize_names:
true
)
if
data
end
end
end
def
cache_attributes
(
values
)
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
values
.
each
do
|
key
,
value
|
redis
.
set
(
cache_attribute_key
(
key
),
value
,
ex:
24
.
hours
)
end
redis
.
set
(
cache_attribute_key
,
values
.
to_json
,
ex:
CACHED_ATTRIBUTES_EXPIRY_TIME
)
end
end
...
...
spec/models/ci/runner_spec.rb
View file @
c92e1d73
...
...
@@ -95,6 +95,12 @@ describe Ci::Runner do
subject
{
runner
.
online?
}
before
do
allow_any_instance_of
(
described_class
).
to
receive
(
:cached_attribute
).
and_call_original
allow_any_instance_of
(
described_class
).
to
receive
(
:cached_attribute
)
.
with
(
:platform
).
and_return
(
"darwin"
)
end
context
'no cache value'
do
before
do
stub_redis_runner_contacted_at
(
nil
)
...
...
@@ -147,8 +153,9 @@ describe Ci::Runner do
def
stub_redis_runner_contacted_at
(
value
)
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
cache_key
=
runner
.
send
(
:cache_attribute_key
,
:contacted_at
)
expect
(
redis
).
to
receive
(
:get
).
with
(
cache_key
).
and_return
(
value
).
at_least
(
:once
)
cache_key
=
runner
.
send
(
:cache_attribute_key
)
expect
(
redis
).
to
receive
(
:get
).
with
(
cache_key
)
.
and_return
({
contacted_at:
value
}.
to_json
).
at_least
(
:once
)
end
end
end
...
...
@@ -405,7 +412,7 @@ describe Ci::Runner do
end
it
'updates cache'
do
expect_redis_update
(
:architecture
,
:contacted_at
)
expect_redis_update
subject
end
...
...
@@ -417,25 +424,23 @@ describe Ci::Runner do
end
it
'updates database'
do
expect_redis_update
(
:architecture
,
:contacted_at
)
expect_redis_update
expect
{
subject
}.
to
change
{
runner
.
reload
.
read_attribute
(
:contacted_at
)
}
.
and
change
{
runner
.
reload
.
read_attribute
(
:architecture
)
}
end
it
'updates cache'
do
expect_redis_update
(
:architecture
,
:contacted_at
)
expect_redis_update
subject
end
end
def
expect_redis_update
(
*
params
)
def
expect_redis_update
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
params
.
each
do
|
param
|
redis_key
=
runner
.
send
(
:cache_attribute_key
,
param
)
expect
(
redis
).
to
receive
(
:set
).
with
(
redis_key
,
anything
,
any_args
)
end
redis_key
=
runner
.
send
(
:cache_attribute_key
)
expect
(
redis
).
to
receive
(
:set
).
with
(
redis_key
,
anything
,
any_args
)
end
end
end
...
...
spec/requests/api/runner_spec.rb
View file @
c92e1d73
...
...
@@ -409,7 +409,7 @@ describe API::Runner do
expect
{
request_job
}.
to
change
{
runner
.
reload
.
contacted_at
}
end
%w(
name
version revision platform architecture)
.
each
do
|
param
|
%w(version revision platform architecture)
.
each
do
|
param
|
context
"when info parameter '
#{
param
}
' is present"
do
let
(
:value
)
{
"
#{
param
}
_value"
}
...
...
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