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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
df255455
Commit
df255455
authored
Jan 29, 2018
by
Matija Čupić
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Generelized cached attribute usage in runner
parent
bdd3e39b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
14 deletions
+30
-14
app/models/ci/runner.rb
app/models/ci/runner.rb
+19
-8
spec/models/ci/runner_spec.rb
spec/models/ci/runner_spec.rb
+11
-6
No files found.
app/models/ci/runner.rb
View file @
df255455
...
@@ -68,6 +68,10 @@ module Ci
...
@@ -68,6 +68,10 @@ module Ci
ONLINE_CONTACT_TIMEOUT
.
ago
ONLINE_CONTACT_TIMEOUT
.
ago
end
end
def
cached_contacted_at
runner_info_cache
(
:contacted_at
)
||
self
.
contacted_at
end
def
set_default_values
def
set_default_values
self
.
token
=
SecureRandom
.
hex
(
15
)
if
self
.
token
.
blank?
self
.
token
=
SecureRandom
.
hex
(
15
)
if
self
.
token
.
blank?
end
end
...
@@ -89,10 +93,7 @@ module Ci
...
@@ -89,10 +93,7 @@ module Ci
end
end
def
online?
def
online?
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
cached_contacted_at
&&
cached_contacted_at
>
self
.
class
.
contact_time_deadline
last_seen
=
redis
.
get
(
"
#{
runner_info_redis_cache_key
}
:contacted_at"
)
||
contacted_at
last_seen
&&
last_seen
>
self
.
class
.
contact_time_deadline
end
end
end
def
status
def
status
...
@@ -157,7 +158,7 @@ module Ci
...
@@ -157,7 +158,7 @@ module Ci
end
end
def
update_runner_info
(
params
)
def
update_runner_info
(
params
)
update_runner_info_cache
update_runner_info_cache
(
params
)
# Use a 1h threshold to prevent beating DB updates.
# Use a 1h threshold to prevent beating DB updates.
return
unless
self
.
contacted_at
.
nil?
||
return
unless
self
.
contacted_at
.
nil?
||
...
@@ -184,10 +185,20 @@ module Ci
...
@@ -184,10 +185,20 @@ module Ci
"runner:info:
#{
self
.
id
}
"
"runner:info:
#{
self
.
id
}
"
end
end
def
update_runner_info_cache
def
update_runner_info_cache
(
params
)
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis
.
set
(
"
#{
runner_info_redis_cache_key
}
:contacted_at"
,
Time
.
now
)
params
.
each
do
|
key
,
value
|
redis_key
=
"
#{
runner_info_redis_cache_key
}
:
#{
key
}
"
redis
.
set
(
redis_key
,
value
)
end
end
end
def
runner_info_cache
(
attribute
)
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis_key
=
"
#{
runner_info_redis_cache_key
}
:contacted_at"
redis
.
get
(
"
#{
runner_info_redis_cache_key
}
:
#{
attribute
}
"
)
redis
.
set
(
redis_key
,
Time
.
now
)
end
end
end
end
...
...
spec/models/ci/runner_spec.rb
View file @
df255455
...
@@ -394,7 +394,7 @@ describe Ci::Runner do
...
@@ -394,7 +394,7 @@ describe Ci::Runner do
describe
'#update_runner_info'
do
describe
'#update_runner_info'
do
let
(
:runner
)
{
create
(
:ci_runner
)
}
let
(
:runner
)
{
create
(
:ci_runner
)
}
subject
{
runner
.
update_runner_info
(
contacted_at:
Time
.
now
)
}
subject
{
runner
.
update_runner_info
(
name:
'testing_runner'
)
}
context
'when database was updated recently'
do
context
'when database was updated recently'
do
before
do
before
do
...
@@ -402,7 +402,7 @@ describe Ci::Runner do
...
@@ -402,7 +402,7 @@ describe Ci::Runner do
end
end
it
'updates cache'
do
it
'updates cache'
do
expect_redis_update
expect_redis_update
(
:contacted_at
,
:name
)
subject
subject
end
end
...
@@ -414,22 +414,27 @@ describe Ci::Runner do
...
@@ -414,22 +414,27 @@ describe Ci::Runner do
end
end
it
'updates database'
do
it
'updates database'
do
expect_redis_update
expect_redis_update
(
:contacted_at
,
:name
)
expect
{
subject
}.
to
change
{
runner
.
reload
.
contacted_at
}
expect
{
subject
}.
to
change
{
runner
.
reload
.
contacted_at
}
.
and
change
{
runner
.
reload
.
name
}
end
end
it
'updates cache'
do
it
'updates cache'
do
expect_redis_update
expect_redis_update
(
:contacted_at
,
:name
)
subject
subject
end
end
end
end
def
expect_redis_update
def
expect_redis_update
(
*
params
)
redis
=
double
redis
=
double
expect
(
Gitlab
::
Redis
::
SharedState
).
to
receive
(
:with
).
and_yield
(
redis
)
expect
(
Gitlab
::
Redis
::
SharedState
).
to
receive
(
:with
).
and_yield
(
redis
)
expect
(
redis
).
to
receive
(
:set
).
with
(
"
#{
runner
.
send
(
:runner_info_redis_cache_key
)
}
:contacted_at"
,
anything
)
params
.
each
do
|
param
|
redis_key
=
"
#{
runner
.
send
(
:runner_info_redis_cache_key
)
}
:
#{
param
}
"
expect
(
redis
).
to
receive
(
:set
).
with
(
redis_key
,
anything
)
end
end
end
end
end
...
...
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