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
Léo-Paul Géneau
gitlab-ce
Commits
264bf229
Commit
264bf229
authored
May 03, 2017
by
James Lopez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add propagate service worker and updated spec and controller
parent
e81ea165
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
7 deletions
+73
-7
app/controllers/admin/services_controller.rb
app/controllers/admin/services_controller.rb
+1
-1
app/models/service.rb
app/models/service.rb
+10
-0
app/workers/propagate_project_service_worker.rb
app/workers/propagate_project_service_worker.rb
+49
-0
spec/workers/propagate_project_service_worker_spec.rb
spec/workers/propagate_project_service_worker_spec.rb
+13
-6
No files found.
app/controllers/admin/services_controller.rb
View file @
264bf229
...
...
@@ -15,7 +15,7 @@ class Admin::ServicesController < Admin::ApplicationController
end
def
update
if
service
.
update_a
ttributes
(
service_params
[
:service
])
if
service
.
update_a
nd_propagate
(
service_params
[
:service
])
redirect_to
admin_application_settings_services_path
,
notice:
'Application settings saved successfully'
else
...
...
app/models/service.rb
View file @
264bf229
...
...
@@ -254,6 +254,16 @@ class Service < ActiveRecord::Base
service
end
def
update_and_propagate
(
service_params
)
return
false
unless
update_attributes
(
service_params
)
if
service_params
[
:active
]
==
1
PropagateProjectServiceWorker
.
perform_async
(
service_params
[
:id
])
end
true
end
private
def
cache_project_has_external_issue_tracker
...
...
app/workers/propagate_project_service_worker.rb
0 → 100644
View file @
264bf229
# Worker for updating any project specific caches.
class
PropagateProjectServiceWorker
include
Sidekiq
::
Worker
include
DedicatedSidekiqQueue
LEASE_TIMEOUT
=
30
.
minutes
.
to_i
def
perform
(
template_id
)
template
=
Service
.
find_by
(
id:
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
|
Service
.
build_from_template
(
project_id
,
template
).
save!
end
end
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
)
Gitlab
::
ExclusiveLease
.
new
(
"propagate_project_service_worker:
#{
template_id
}
"
,
timeout:
LEASE_TIMEOUT
).
try_obtain
end
end
spec/workers/propagate_project_service_worker_spec.rb
View file @
264bf229
require
'spec_helper'
describe
Pr
uneOldEvents
Worker
do
describe
Pr
opagateProjectService
Worker
do
describe
'#perform'
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
)
}
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
}.
to
change
{
Service
.
count
}.
by
(
1
)
expect
{
subject
.
perform
(
service_template
.
id
)
}.
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
{
subject
.
perform
}.
not_to
change
{
Service
.
count
}
expect
{
subject
.
perform
(
service_template
.
id
)
}.
not_to
change
{
Service
.
count
}
end
it
'creates the service containing the template attributes'
do
subject
.
perform
subject
.
perform
(
service_template
.
id
)
service
=
Service
.
find_by
(
service_template
.
merge
(
project_id:
project
.
id
,
template:
false
)
)
service
=
Service
.
find_by
(
type:
service_template
.
type
,
template:
false
)
expect
(
service
).
not_to
be_nil
expect
(
service
.
properties
).
to
eq
(
service_template
.
properties
)
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