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
a2352c02
Commit
a2352c02
authored
May 05, 2017
by
Gabriel Mazetto
Committed by
Robert Speicher
May 10, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move GeoRepositoryFetchWorker logic to service class and use ExclusiveLease
parent
0676f1d5
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
123 additions
and
48 deletions
+123
-48
app/services/geo/repository_update_service.rb
app/services/geo/repository_update_service.rb
+61
-0
app/workers/geo_repository_fetch_worker.rb
app/workers/geo_repository_fetch_worker.rb
+1
-9
spec/services/geo/repository_update_service_spec.rb
spec/services/geo/repository_update_service_spec.rb
+59
-0
spec/workers/geo_repository_fetch_worker_spec.rb
spec/workers/geo_repository_fetch_worker_spec.rb
+2
-39
No files found.
app/services/geo/repository_update_service.rb
0 → 100644
View file @
a2352c02
module
Geo
class
RepositoryUpdateService
attr_reader
:project
,
:clone_url
LEASE_TIMEOUT
=
1
.
hour
.
freeze
LEASE_KEY_PREFIX
=
'geo_repository_fetch'
.
freeze
def
initialize
(
project
,
clone_url
,
logger
=
nil
)
@project
=
project
@clone_url
=
clone_url
@logger
=
logger
end
def
execute
try_obtain_lease
do
project
.
create_repository
unless
project
.
repository_exists?
project
.
repository
.
after_create
if
project
.
empty_repo?
project
.
repository
.
fetch_geo_mirror
(
clone_url
)
project
.
repository
.
expire_all_method_caches
project
.
repository
.
expire_branch_cache
project
.
repository
.
expire_content_cache
end
rescue
Gitlab
::
Shell
::
Error
=>
e
logger
.
error
"Error fetching repository for project
#{
project
.
path_with_namespace
}
:
#{
e
}
"
end
private
def
try_obtain_lease
log
(
'Trying to obtain lease to sync repository'
)
repository_lease
=
Gitlab
::
ExclusiveLease
.
new
(
lease_key
,
timeout:
LEASE_TIMEOUT
).
try_obtain
unless
repository_lease
.
present?
log
(
'Could not obtain lease to sync repository'
)
return
end
begin
yield
ensure
if
repository_lease
.
present?
log
(
'Releasing leases to sync repository'
)
Gitlab
::
ExclusiveLease
.
cancel
(
lease_key
,
repository_lease
)
end
end
end
def
lease_key
@lease_key
||=
"
#{
LEASE_KEY_PREFIX
}
:
#{
project
.
id
}
"
end
def
log
(
message
)
logger
.
info
(
"
#{
self
.
class
.
name
}
:
#{
message
}
for project
#{
project
.
path_with_namespace
}
(
#{
project
.
id
}
)"
)
end
def
logger
@logger
||
Rails
.
logger
end
end
end
app/workers/geo_repository_fetch_worker.rb
View file @
a2352c02
...
@@ -8,14 +8,6 @@ class GeoRepositoryFetchWorker
...
@@ -8,14 +8,6 @@ class GeoRepositoryFetchWorker
def
perform
(
project_id
,
clone_url
)
def
perform
(
project_id
,
clone_url
)
project
=
Project
.
find
(
project_id
)
project
=
Project
.
find
(
project_id
)
Geo
::
RepositoryUpdateService
.
new
(
project
,
clone_url
,
logger
).
execute
project
.
create_repository
unless
project
.
repository_exists?
project
.
repository
.
after_create
if
project
.
empty_repo?
project
.
repository
.
fetch_geo_mirror
(
clone_url
)
project
.
repository
.
expire_all_method_caches
project
.
repository
.
expire_branch_cache
project
.
repository
.
expire_content_cache
rescue
Gitlab
::
Shell
::
Error
=>
e
logger
.
error
"Error fetching repository for project
#{
project
.
path_with_namespace
}
:
#{
e
}
"
end
end
end
end
spec/services/geo/repository_update_service_spec.rb
0 → 100644
View file @
a2352c02
require
'spec_helper'
describe
Geo
::
RepositoryUpdateService
,
services:
true
do
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:clone_url
)
{
project
.
ssh_url_to_repo
}
subject
{
described_class
.
new
(
project
,
clone_url
)
}
describe
'#execute'
do
before
do
allow_any_instance_of
(
Gitlab
::
Geo
).
to
receive_messages
(
secondary?:
true
)
allow
(
project
.
repository
).
to
receive
(
:fetch_geo_mirror
).
and_return
(
true
)
allow
(
project
).
to
receive
(
:repository_exists?
)
{
false
}
allow
(
project
).
to
receive
(
:empty_repo?
)
{
true
}
allow
(
project
.
repository
).
to
receive
(
:expire_all_method_caches
)
allow
(
project
.
repository
).
to
receive
(
:expire_branch_cache
)
allow
(
project
.
repository
).
to
receive
(
:expire_content_cache
)
end
it
'releases the lease'
do
expect
(
Gitlab
::
ExclusiveLease
).
to
receive
(
:cancel
).
once
.
and_call_original
subject
.
execute
end
it
'creates a new repository'
do
expect
(
project
).
to
receive
(
:create_repository
)
subject
.
execute
end
it
'executes after_create hook'
do
expect
(
project
.
repository
).
to
receive
(
:after_create
).
at_least
(
:once
)
subject
.
execute
end
it
'fetches the Geo mirror'
do
expect
(
project
.
repository
).
to
receive
(
:fetch_geo_mirror
)
subject
.
execute
end
it
'expires repository caches'
do
expect
(
project
.
repository
).
to
receive
(
:expire_all_method_caches
)
expect
(
project
.
repository
).
to
receive
(
:expire_branch_cache
)
expect
(
project
.
repository
).
to
receive
(
:expire_content_cache
)
subject
.
execute
end
it
'does not raise exception when git failures occurs'
do
expect
(
project
.
repository
).
to
receive
(
:fetch_geo_mirror
).
and_raise
(
Gitlab
::
Shell
::
Error
)
expect
{
subject
.
execute
}.
not_to
raise_error
end
end
end
spec/workers/geo_repository_fetch_worker_spec.rb
View file @
a2352c02
...
@@ -4,48 +4,11 @@ describe GeoRepositoryFetchWorker do
...
@@ -4,48 +4,11 @@ describe GeoRepositoryFetchWorker do
describe
'#perform'
do
describe
'#perform'
do
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:project
)
{
create
(
:empty_project
)
}
before
do
it
'delegates to Geo::RepositoryUpdateService'
do
allow_any_instance_of
(
Gitlab
::
Geo
).
to
receive_messages
(
secondary?:
true
)
expect_any_instance_of
(
Geo
::
RepositoryUpdateService
).
to
receive
(
:execute
)
allow_any_instance_of
(
Repository
).
to
receive
(
:fetch_geo_mirror
).
and_return
(
true
)
allow_any_instance_of
(
Project
).
to
receive
(
:repository_exists?
)
{
false
}
allow_any_instance_of
(
Project
).
to
receive
(
:empty_repo?
)
{
true
}
allow_any_instance_of
(
Repository
).
to
receive
(
:expire_all_method_caches
)
allow_any_instance_of
(
Repository
).
to
receive
(
:expire_branch_cache
)
allow_any_instance_of
(
Repository
).
to
receive
(
:expire_content_cache
)
end
it
'creates a new repository'
do
expect_any_instance_of
(
Project
).
to
receive
(
:create_repository
)
perform
end
it
'executes after_create hook'
do
expect_any_instance_of
(
Repository
).
to
receive
(
:after_create
).
at_least
(
:once
)
perform
end
it
'fetches the Geo mirror'
do
expect_any_instance_of
(
Repository
).
to
receive
(
:fetch_geo_mirror
)
perform
perform
end
end
it
'expires repository caches'
do
expect_any_instance_of
(
Repository
).
to
receive
(
:expire_all_method_caches
)
expect_any_instance_of
(
Repository
).
to
receive
(
:expire_branch_cache
)
expect_any_instance_of
(
Repository
).
to
receive
(
:expire_content_cache
)
perform
end
it
'does not raise exception when git failures occurs'
do
expect_any_instance_of
(
Repository
).
to
receive
(
:fetch_geo_mirror
).
and_raise
(
Gitlab
::
Shell
::
Error
)
expect
{
perform
}.
not_to
raise_error
end
end
end
def
perform
def
perform
...
...
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