Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
bdd3e39b
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
bdd3e39b
编写于
1月 29, 2018
作者:
M
Matija Čupić
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move info update implementation to Ci::Runner model
上级
92ac2b9e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
71 addition
and
34 deletion
+71
-34
app/models/ci/runner.rb
app/models/ci/runner.rb
+23
-3
lib/api/helpers/runner.rb
lib/api/helpers/runner.rb
+1
-26
spec/models/ci/runner_spec.rb
spec/models/ci/runner_spec.rb
+47
-5
未找到文件。
app/models/ci/runner.rb
浏览文件 @
bdd3e39b
...
...
@@ -5,6 +5,7 @@ module Ci
RUNNER_QUEUE_EXPIRY_TIME
=
60
.
minutes
ONLINE_CONTACT_TIMEOUT
=
1
.
hour
UPDATE_DB_RUNNER_INFO_EVERY
=
1
.
hour
AVAILABLE_SCOPES
=
%w[specific shared active paused online]
.
freeze
FORM_EDITABLE
=
%i[description tag_list active run_untagged locked access_level]
.
freeze
...
...
@@ -89,7 +90,7 @@ module Ci
def
online?
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
last_seen
=
redis
.
get
(
"
#{
self
.
runner_info
_key
}
:contacted_at"
)
||
contacted_at
last_seen
=
redis
.
get
(
"
#{
runner_info_redis_cache
_key
}
:contacted_at"
)
||
contacted_at
last_seen
&&
last_seen
>
self
.
class
.
contact_time_deadline
end
end
...
...
@@ -155,8 +156,16 @@ module Ci
ensure_runner_queue_value
==
value
if
value
.
present?
end
def
runner_info_key
"runner:info:
#{
self
.
id
}
"
def
update_runner_info
(
params
)
update_runner_info_cache
# Use a 1h threshold to prevent beating DB updates.
return
unless
self
.
contacted_at
.
nil?
||
(
Time
.
now
-
self
.
contacted_at
)
>=
UPDATE_DB_RUNNER_INFO_EVERY
self
.
contacted_at
=
Time
.
now
self
.
assign_attributes
(
params
)
self
.
save
if
self
.
changed?
end
private
...
...
@@ -171,6 +180,17 @@ module Ci
"runner:build_queue:
#{
self
.
token
}
"
end
def
runner_info_redis_cache_key
"runner:info:
#{
self
.
id
}
"
end
def
update_runner_info_cache
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis_key
=
"
#{
runner_info_redis_cache_key
}
:contacted_at"
redis
.
set
(
redis_key
,
Time
.
now
)
end
end
def
tag_constraints
unless
has_tags?
||
run_untagged?
errors
.
add
(
:tags_list
,
...
...
lib/api/helpers/runner.rb
浏览文件 @
bdd3e39b
...
...
@@ -5,7 +5,6 @@ module API
JOB_TOKEN_HEADER
=
'HTTP_JOB_TOKEN'
.
freeze
JOB_TOKEN_PARAM
=
:token
UPDATE_RUNNER_EVERY
=
10
*
60
def
runner_registration_token_valid?
ActiveSupport
::
SecurityUtils
.
variable_size_secure_compare
(
params
[
:token
],
...
...
@@ -21,37 +20,13 @@ module API
def
authenticate_runner!
forbidden!
unless
current_runner
update_runner_info
current_runner
.
update_runner_info
(
get_runner_version_from_params
)
end
def
current_runner
@runner
||=
::
Ci
::
Runner
.
find_by_token
(
params
[
:token
].
to_s
)
end
def
update_runner_info
update_runner_info_cache
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 1h threshold to prevent beating DB updates.
current_runner
.
contacted_at
.
nil?
||
(
Time
.
now
-
current_runner
.
contacted_at
)
>=
UPDATE_RUNNER_EVERY
end
def
update_runner_info_cache
Gitlab
::
Redis
::
SharedState
.
with
do
|
redis
|
redis_key
=
"
#{
current_runner
.
runner_info_key
}
:contacted_at"
redis
.
set
(
redis_key
,
Time
.
now
)
end
end
def
validate_job!
(
job
)
not_found!
unless
job
...
...
spec/models/ci/runner_spec.rb
浏览文件 @
bdd3e39b
...
...
@@ -97,7 +97,7 @@ describe Ci::Runner do
context
'no cache value'
do
before
do
stub_redis
(
"
#{
runner
.
runner_info_key
}
:contacted_at"
,
nil
)
stub_redis
_runner_contacted_at
(
nil
)
end
context
'never contacted'
do
...
...
@@ -128,7 +128,7 @@ describe Ci::Runner do
context
'with cache value'
do
context
'contacted long time ago time'
do
before
do
stub_redis
(
"
#{
runner
.
runner_info_key
}
:contacted_at"
,
1
.
year
.
ago
.
to_s
)
stub_redis
_runner_contacted_at
(
1
.
year
.
ago
.
to_s
)
end
it
{
is_expected
.
to
be_falsey
}
...
...
@@ -136,17 +136,17 @@ describe Ci::Runner do
context
'contacted 1s ago'
do
before
do
stub_redis
(
"
#{
runner
.
runner_info_key
}
:contacted_at"
,
1
.
second
.
ago
.
to_s
)
stub_redis
_runner_contacted_at
(
1
.
second
.
ago
.
to_s
)
end
it
{
is_expected
.
to
be_truthy
}
end
end
def
stub_redis
(
key
,
value
)
def
stub_redis
_runner_contacted_at
(
value
)
redis
=
double
allow
(
Gitlab
::
Redis
::
SharedState
).
to
receive
(
:with
).
and_yield
(
redis
)
allow
(
redis
).
to
receive
(
:get
).
with
(
key
).
and_return
(
value
)
allow
(
redis
).
to
receive
(
:get
).
with
(
"
#{
runner
.
send
(
:runner_info_redis_cache_key
)
}
:contacted_at"
).
and_return
(
value
)
end
end
...
...
@@ -391,6 +391,48 @@ describe Ci::Runner do
end
end
describe
'#update_runner_info'
do
let
(
:runner
)
{
create
(
:ci_runner
)
}
subject
{
runner
.
update_runner_info
(
contacted_at:
Time
.
now
)
}
context
'when database was updated recently'
do
before
do
runner
.
update
(
contacted_at:
Time
.
now
)
end
it
'updates cache'
do
expect_redis_update
subject
end
end
context
'when database was not updated recently'
do
before
do
runner
.
update
(
contacted_at:
2
.
hours
.
ago
)
end
it
'updates database'
do
expect_redis_update
expect
{
subject
}.
to
change
{
runner
.
reload
.
contacted_at
}
end
it
'updates cache'
do
expect_redis_update
subject
end
end
def
expect_redis_update
redis
=
double
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
)
end
end
describe
'#destroy'
do
let
(
:runner
)
{
create
(
:ci_runner
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录