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
bb64b20a
Commit
bb64b20a
authored
Mar 05, 2018
by
Tomasz Maczukin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactorize Ci::Build and Ci::BuildMetadata models
parent
9ef86b1b
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
33 additions
and
36 deletions
+33
-36
app/models/ci/build.rb
app/models/ci/build.rb
+7
-7
app/models/ci/build_metadata.rb
app/models/ci/build_metadata.rb
+11
-5
db/migrate/20180301010859_create_ci_builds_metadata_table.rb
db/migrate/20180301010859_create_ci_builds_metadata_table.rb
+5
-2
db/migrate/20180301011323_add_build_foreign_key_to_ci_builds_metadata.rb
...0301011323_add_build_foreign_key_to_ci_builds_metadata.rb
+0
-15
db/schema.rb
db/schema.rb
+3
-4
spec/models/ci/build_spec.rb
spec/models/ci/build_spec.rb
+7
-3
No files found.
app/models/ci/build.rb
View file @
bb64b20a
...
...
@@ -24,7 +24,7 @@ module Ci
has_one
:job_artifacts_metadata
,
->
{
where
(
file_type:
Ci
::
JobArtifact
.
file_types
[
:metadata
])
},
class_name:
'Ci::JobArtifact'
,
inverse_of: :job
,
foreign_key: :job_id
has_one
:job_artifacts_trace
,
->
{
where
(
file_type:
Ci
::
JobArtifact
.
file_types
[
:trace
])
},
class_name:
'Ci::JobArtifact'
,
inverse_of: :job
,
foreign_key: :job_id
has_one
:metadata
,
class_name:
'Ci::BuildMetadata'
has_one
:
build_
metadata
,
class_name:
'Ci::BuildMetadata'
# The "environment" field for builds is a String, and is the unexpanded name
def
persisted_environment
...
...
@@ -85,10 +85,6 @@ module Ci
before_save
:ensure_token
before_destroy
{
unscoped_project
}
before_create
do
|
build
|
build
.
metadata
=
Ci
::
BuildMetadata
.
new
end
after_create
unless: :importing?
do
|
build
|
run_after_commit
{
BuildHooksWorker
.
perform_async
(
build
.
id
)
}
end
...
...
@@ -161,10 +157,14 @@ module Ci
end
before_transition
pending: :running
do
|
build
|
build
.
metadata
.
save_timeout_state!
unless
build
.
metadata
.
nil?
build
.
metadata
.
save_timeout_state!
end
end
def
metadata
self
.
build_metadata
||=
Ci
::
BuildMetadata
.
new
end
def
detailed_status
(
current_user
)
Gitlab
::
Ci
::
Status
::
Build
::
Factory
.
new
(
self
,
current_user
)
...
...
@@ -242,7 +242,7 @@ module Ci
end
def
timeout
[
project
.
build_timeout
,
runner
&
.
maximum_job_timeout
].
compact
.
min
metadata
.
used_timeout
end
def
triggered_by?
(
current_user
)
...
...
app/models/ci/build_metadata.rb
View file @
bb64b20a
module
Ci
# The purpose of this class is to store Build related data that can be disposed.
# Data that should be persisted forever, should be stored with Ci::Build model.
class
BuildMetadata
<
ActiveRecord
::
Base
extend
Gitlab
::
Ci
::
Model
include
Presentable
...
...
@@ -11,14 +13,18 @@ module Ci
chronic_duration_attr_reader
:used_timeout_human_readable
,
:used_timeout
enum
timeout_source:
{
unknown_timeout_source:
nil
,
project_timeout_source:
1
,
runner_timeout_source:
2
unknown_timeout_source:
1
,
project_timeout_source:
2
,
runner_timeout_source:
3
}
def
save_timeout_state!
self
.
used_timeout
=
build
.
timeout
self
.
timeout_source
=
build
.
timeout
<
build
.
project
.
build_timeout
?
:runner_timeout_source
:
:project_timeout_source
project_timeout
=
build
.
project
&
.
build_timeout
timeout
=
[
project_timeout
,
build
.
runner
&
.
maximum_job_timeout
].
compact
.
min
self
.
used_timeout
=
timeout
self
.
timeout_source
=
timeout
<
project_timeout
?
:runner_timeout_source
:
:project_timeout_source
save!
end
end
...
...
db/migrate/20180301010859_create_ci_builds_metadata_table.rb
View file @
bb64b20a
...
...
@@ -4,10 +4,13 @@ class CreateCiBuildsMetadataTable < ActiveRecord::Migration
DOWNTIME
=
false
def
change
create_table
:ci_builds_metadata
do
|
t
|
create_table
:ci_builds_metadata
,
id:
false
do
|
t
|
t
.
integer
:build_id
,
null:
false
t
.
integer
:used_timeout
t
.
integer
:timeout_source
t
.
integer
:timeout_source
,
null:
false
,
default:
1
t
.
primary_key
:build_id
t
.
foreign_key
:ci_builds
,
column: :build_id
,
on_delete: :cascade
end
end
end
db/migrate/20180301011323_add_build_foreign_key_to_ci_builds_metadata.rb
deleted
100644 → 0
View file @
9ef86b1b
class
AddBuildForeignKeyToCiBuildsMetadata
<
ActiveRecord
::
Migration
include
Gitlab
::
Database
::
MigrationHelpers
DOWNTIME
=
false
disable_ddl_transaction!
def
up
add_concurrent_foreign_key
(
:ci_builds_metadata
,
:ci_builds
,
column: :build_id
)
end
def
down
remove_foreign_key
(
:ci_builds_metadata
,
column: :build_id
)
end
end
db/schema.rb
View file @
bb64b20a
...
...
@@ -329,10 +329,9 @@ ActiveRecord::Schema.define(version: 20180327101207) do
add_index
"ci_builds"
,
[
"updated_at"
],
name:
"index_ci_builds_on_updated_at"
,
using: :btree
add_index
"ci_builds"
,
[
"user_id"
],
name:
"index_ci_builds_on_user_id"
,
using: :btree
create_table
"ci_builds_metadata"
,
force: :cascade
do
|
t
|
t
.
integer
"build_id"
,
null:
false
create_table
"ci_builds_metadata"
,
primary_key:
"build_id"
,
force: :cascade
do
|
t
|
t
.
integer
"used_timeout"
t
.
integer
"timeout_source"
t
.
integer
"timeout_source"
,
default:
1
,
null:
false
end
create_table
"ci_group_variables"
,
force: :cascade
do
|
t
|
...
...
@@ -2035,7 +2034,7 @@ ActiveRecord::Schema.define(version: 20180327101207) do
add_foreign_key
"ci_builds"
,
"ci_pipelines"
,
column:
"auto_canceled_by_id"
,
name:
"fk_a2141b1522"
,
on_delete: :nullify
add_foreign_key
"ci_builds"
,
"ci_stages"
,
column:
"stage_id"
,
name:
"fk_3a9eaa254d"
,
on_delete: :cascade
add_foreign_key
"ci_builds"
,
"projects"
,
name:
"fk_befce0568a"
,
on_delete: :cascade
add_foreign_key
"ci_builds_metadata"
,
"ci_builds"
,
column:
"build_id"
,
name:
"fk_e20479742e"
,
on_delete: :cascade
add_foreign_key
"ci_builds_metadata"
,
"ci_builds"
,
column:
"build_id"
,
on_delete: :cascade
add_foreign_key
"ci_group_variables"
,
"namespaces"
,
column:
"group_id"
,
name:
"fk_33ae4d58d8"
,
on_delete: :cascade
add_foreign_key
"ci_job_artifacts"
,
"ci_builds"
,
column:
"job_id"
,
on_delete: :cascade
add_foreign_key
"ci_job_artifacts"
,
"projects"
,
on_delete: :cascade
...
...
spec/models/ci/build_spec.rb
View file @
bb64b20a
...
...
@@ -1275,17 +1275,21 @@ describe Ci::Build do
set
(
:project2
)
{
create
(
:project
,
:repository
,
group:
group
,
build_timeout:
1000
)
}
set
(
:pipeline2
)
{
create
(
:ci_pipeline
,
project:
project2
,
sha:
project2
.
commit
.
id
,
ref:
project2
.
default_branch
,
status:
'success'
)
}
let
(
:build
)
{
create
(
:ci_build
,
:
manual
,
pipeline:
pipeline2
)
}
let
(
:build
)
{
create
(
:ci_build
,
:
pending
,
pipeline:
pipeline2
)
}
subject
{
build
.
timeout
}
before
do
build
.
run!
end
it
'returns project timeout configuration'
do
is_expected
.
to
eq
(
project2
.
build_timeout
)
end
context
'when runner sets timeout to bigger value'
do
let
(
:runner2
)
{
create
(
:ci_runner
,
maximum_job_timeout:
2000
)
}
let
(
:build
)
{
create
(
:ci_build
,
:
manual
,
pipeline:
pipeline2
,
runner:
runner2
)
}
let
(
:build
)
{
create
(
:ci_build
,
:
pending
,
pipeline:
pipeline2
,
runner:
runner2
)
}
it
'returns project timeout configuration'
do
is_expected
.
to
eq
(
project2
.
build_timeout
)
...
...
@@ -1294,7 +1298,7 @@ describe Ci::Build do
context
'when runner sets timeout to smaller value'
do
let
(
:runner2
)
{
create
(
:ci_runner
,
maximum_job_timeout:
500
)
}
let
(
:build
)
{
create
(
:ci_build
,
:
manual
,
pipeline:
pipeline2
,
runner:
runner2
)
}
let
(
:build
)
{
create
(
:ci_build
,
:
pending
,
pipeline:
pipeline2
,
runner:
runner2
)
}
it
'returns project timeout configuration'
do
is_expected
.
to
eq
(
runner2
.
maximum_job_timeout
)
...
...
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