Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
f81cf840
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,发现更多精彩内容 >>
提交
f81cf840
编写于
5月 04, 2017
作者:
J
James Lopez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor worker into service
上级
2f7f1ce4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
110 addition
and
63 deletion
+110
-63
app/services/projects/propagate_service.rb
app/services/projects/propagate_service.rb
+47
-0
app/workers/propagate_project_service_worker.rb
app/workers/propagate_project_service_worker.rb
+4
-30
spec/services/projects/propagate_service_spec.rb
spec/services/projects/propagate_service_spec.rb
+40
-0
spec/workers/propagate_project_service_worker_spec.rb
spec/workers/propagate_project_service_worker_spec.rb
+19
-33
未找到文件。
app/services/projects/propagate_service.rb
0 → 100644
浏览文件 @
f81cf840
module
Projects
class
PropagateService
BATCH_SIZE
=
100
def
self
.
propagate!
(
*
args
)
new
(
*
args
).
propagate!
end
def
initialize
(
template
)
@template
=
template
end
def
propagate!
return
unless
@template
&
.
active
Rails
.
logger
.
info
(
"Propagating services for template
#{
@template
.
id
}
"
)
propagate_projects_with_template
end
private
def
propagate_projects_with_template
offset
=
0
loop
do
batch
=
project_ids_batch
(
offset
)
batch
.
each
{
|
project_id
|
create_from_template
(
project_id
)
}
break
if
batch
.
count
<
BATCH_SIZE
offset
+=
BATCH_SIZE
end
end
def
create_from_template
(
project_id
)
Service
.
build_from_template
(
project_id
,
@template
).
save!
end
def
project_ids_batch
(
offset
)
Project
.
joins
(
'LEFT JOIN services ON services.project_id = projects.id'
).
where
(
'services.type != ? OR services.id IS NULL'
,
@template
.
type
).
limit
(
BATCH_SIZE
).
offset
(
offset
).
pluck
(
:id
)
end
end
end
app/workers/propagate_project_service_worker.rb
浏览文件 @
f81cf840
...
@@ -3,44 +3,18 @@ class PropagateProjectServiceWorker
...
@@ -3,44 +3,18 @@ class PropagateProjectServiceWorker
include
Sidekiq
::
Worker
include
Sidekiq
::
Worker
include
DedicatedSidekiqQueue
include
DedicatedSidekiqQueue
sidekiq_options
retry:
3
LEASE_TIMEOUT
=
30
.
minutes
.
to_i
LEASE_TIMEOUT
=
30
.
minutes
.
to_i
def
perform
(
template_id
)
def
perform
(
template_id
)
template
=
Service
.
find_by
(
id:
template_id
)
return
unless
try_obtain_lease_for
(
template_id
)
return
unless
template
&
.
active
return
unless
try_obtain_lease_for
(
template
.
id
)
Rails
.
logger
.
info
(
"Propagating services for template
#{
template
.
id
}
"
)
project_ids_for_template
(
template
)
do
|
project_id
|
Projects
::
PropagateService
.
propagate!
(
Service
.
find_by
(
id:
template_id
))
Service
.
build_from_template
(
project_id
,
template
).
save!
end
end
end
private
private
def
project_ids_for_template
(
template
)
limit
=
100
offset
=
0
loop
do
batch
=
project_ids_batch
(
limit
,
offset
,
template
.
type
)
batch
.
each
{
|
project_id
|
yield
(
project_id
)
}
break
if
batch
.
count
<
limit
offset
+=
limit
end
end
def
project_ids_batch
(
limit
,
offset
,
template_type
)
Project
.
joins
(
'LEFT JOIN services ON services.project_id = projects.id'
).
where
(
'services.type != ? OR services.id IS NULL'
,
template_type
).
limit
(
limit
).
offset
(
offset
).
pluck
(
:id
)
end
def
try_obtain_lease_for
(
template_id
)
def
try_obtain_lease_for
(
template_id
)
Gitlab
::
ExclusiveLease
.
Gitlab
::
ExclusiveLease
.
new
(
"propagate_project_service_worker:
#{
template_id
}
"
,
timeout:
LEASE_TIMEOUT
).
new
(
"propagate_project_service_worker:
#{
template_id
}
"
,
timeout:
LEASE_TIMEOUT
).
...
...
spec/services/projects/propagate_service_spec.rb
0 → 100644
浏览文件 @
f81cf840
require
'spec_helper'
describe
Projects
::
PropagateService
,
services:
true
do
describe
'.propagate!'
do
let!
(
:service_template
)
do
PushoverService
.
create
(
template:
true
,
active:
true
,
properties:
{
device:
'MyDevice'
,
sound:
'mic'
,
priority:
4
,
user_key:
'asdf'
,
api_key:
'123456789'
})
end
let!
(
:project
)
{
create
(
:empty_project
)
}
it
'creates services for projects'
do
expect
{
described_class
.
propagate!
(
service_template
)
}.
to
change
{
Service
.
count
}.
by
(
1
)
end
it
'does not create the service if it exists already'
do
Service
.
build_from_template
(
project
.
id
,
service_template
).
save!
expect
{
described_class
.
propagate!
(
service_template
)
}.
not_to
change
{
Service
.
count
}
end
it
'creates the service containing the template attributes'
do
described_class
.
propagate!
(
service_template
)
service
=
Service
.
find_by
(
type:
service_template
.
type
,
template:
false
)
expect
(
service
.
properties
).
to
eq
(
service_template
.
properties
)
end
end
end
spec/workers/propagate_project_service_worker_spec.rb
浏览文件 @
f81cf840
require
'spec_helper'
require
'spec_helper'
describe
PropagateProjectServiceWorker
do
describe
PropagateProjectServiceWorker
do
describe
'#perform'
do
let!
(
:service_template
)
do
let!
(
:service_template
)
do
PushoverService
.
create
(
PushoverService
.
create
(
template:
true
,
template:
true
,
active:
true
,
active:
true
,
properties:
{
properties:
{
device:
'MyDevice'
,
device:
'MyDevice'
,
sound:
'mic'
,
sound:
'mic'
,
priority:
4
,
priority:
4
,
user_key:
'asdf'
,
user_key:
'asdf'
,
api_key:
'123456789'
api_key:
'123456789'
})
})
end
end
let!
(
:project
)
{
create
(
:empty_project
)
}
before
do
allow_any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
).
and_return
(
true
)
end
it
'creates services for projects'
do
expect
{
subject
.
perform
(
service_template
.
id
)
}.
to
change
{
Service
.
count
}.
by
(
1
)
end
it
'does not create the service if it exists already'
do
before
do
Service
.
build_from_template
(
project
.
id
,
service_template
).
save!
allow_any_instance_of
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:try_obtain
).
and_return
(
true
)
end
expect
{
subject
.
perform
(
service_template
.
id
)
}.
not_to
change
{
Service
.
count
}
describe
'#perform'
do
end
it
'calls the propagate service with the template'
do
expect
(
Projects
::
PropagateService
).
to
receive
(
:propagate!
).
with
(
service_template
)
it
'creates the service containing the template attributes'
do
subject
.
perform
(
service_template
.
id
)
subject
.
perform
(
service_template
.
id
)
service
=
Service
.
find_by
(
type:
service_template
.
type
,
template:
false
)
expect
(
service
.
properties
).
to
eq
(
service_template
.
properties
)
end
end
end
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录