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
Boxiang Sun
gitlab-ce
Commits
538ad92a
Commit
538ad92a
authored
Feb 05, 2018
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix transactions-related race condition in stages code
parent
b5c69ce3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
1 deletion
+57
-1
app/services/ci/ensure_stage_service.rb
app/services/ci/ensure_stage_service.rb
+6
-1
spec/services/ci/ensure_stage_service_spec.rb
spec/services/ci/ensure_stage_service_spec.rb
+51
-0
No files found.
app/services/ci/ensure_stage_service.rb
View file @
538ad92a
...
...
@@ -7,6 +7,8 @@ module Ci
# stage.
#
class
EnsureStageService
<
BaseService
PipelineStageError
=
Class
.
new
(
StandardError
)
def
execute
(
build
)
@build
=
build
...
...
@@ -22,8 +24,11 @@ module Ci
private
def
ensure_stage
def
ensure_stage
(
attempts:
2
)
find_stage
||
create_stage
rescue
ActiveRecord
::
RecordNotUnique
retry
if
(
attempts
-=
1
)
>
0
raise
PipelineStageError
,
'Fix me!'
end
def
find_stage
...
...
spec/services/ci/ensure_stage_service_spec.rb
0 → 100644
View file @
538ad92a
require
'spec_helper'
describe
Ci
::
EnsureStageService
,
'#execute'
do
set
(
:project
)
{
create
(
:project
)
}
set
(
:user
)
{
create
(
:user
)
}
let
(
:stage
)
{
create
(
:ci_stage_entity
)
}
let
(
:job
)
{
build
(
:ci_build
)
}
let
(
:service
)
{
described_class
.
new
(
project
,
user
)
}
context
'when build has a stage assigned'
do
it
'does not create a new stage'
do
job
.
assign_attributes
(
stage_id:
stage
.
id
)
expect
{
service
.
execute
(
job
)
}.
not_to
change
{
Ci
::
Stage
.
count
}
end
end
context
'when build does not have a stage assigned'
do
it
'creates a new stage'
do
job
.
assign_attributes
(
stage_id:
nil
,
stage:
'test'
)
expect
{
service
.
execute
(
job
)
}.
to
change
{
Ci
::
Stage
.
count
}.
by
(
1
)
end
end
context
'when build is invalid'
do
it
'does not create a new stage'
do
job
.
assign_attributes
(
stage_id:
nil
,
ref:
nil
)
expect
{
service
.
execute
(
job
)
}.
not_to
change
{
Ci
::
Stage
.
count
}
end
end
context
'when new stage can not be created because of an exception'
do
before
do
allow
(
Ci
::
Stage
).
to
receive
(
:create!
)
.
and_raise
(
ActiveRecord
::
RecordNotUnique
.
new
(
'Duplicates!'
))
end
it
'retries up to two times'
do
job
.
assign_attributes
(
stage_id:
nil
)
expect
(
service
).
to
receive
(
:find_stage
).
exactly
(
2
).
times
expect
{
service
.
execute
(
job
)
}
.
to
raise_error
(
Ci
::
EnsureStageService
::
PipelineStageError
)
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