Commit 1b0b8b9c authored by Tomasz Maczukin's avatar Tomasz Maczukin

Change timeout_source to enum

parent 8ffa4809
...@@ -93,6 +93,12 @@ module Ci ...@@ -93,6 +93,12 @@ module Ci
chronic_duration_attr_reader :used_timeout_human_readable, :used_timeout 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
}
class << self class << self
# This is needed for url_for to work, # This is needed for url_for to work,
# as the controller is JobsController # as the controller is JobsController
...@@ -123,10 +129,6 @@ module Ci ...@@ -123,10 +129,6 @@ module Ci
end end
after_transition pending: :running do |build| after_transition pending: :running do |build|
build.used_timeout = build.timeout
build.timeout_source = build.timeout < build.project.build_timeout ? 'runner' : 'project'
build.save!
build.run_after_commit do build.run_after_commit do
BuildHooksWorker.perform_async(id) BuildHooksWorker.perform_async(id)
end end
...@@ -160,6 +162,11 @@ module Ci ...@@ -160,6 +162,11 @@ module Ci
before_transition any => [:running] do |build| before_transition any => [:running] do |build|
build.validates_dependencies! unless Feature.enabled?('ci_disable_validates_dependencies') build.validates_dependencies! unless Feature.enabled?('ci_disable_validates_dependencies')
end end
before_transition pending: :running do |build|
build.used_timeout = build.timeout
build.timeout_source = build.timeout < build.project.build_timeout ? :runner_timeout_source : :project_timeout_source
end
end end
def detailed_status(current_user) def detailed_status(current_user)
......
module Ci module Ci
class BuildPresenter < Gitlab::View::Presenter::Delegated class BuildPresenter < Gitlab::View::Presenter::Delegated
TIMEOUT_SOURCES = {
unknown_timeout_source: nil,
project_timeout_source: 'project',
runner_timeout_source: 'runner'
}.freeze
presents :build presents :build
def erased_by_user? def erased_by_user?
...@@ -18,6 +25,13 @@ module Ci ...@@ -18,6 +25,13 @@ module Ci
end end
end end
def timeout_source
return unless build.timeout_source?
TIMEOUT_SOURCES[build.timeout_source.to_sym] ||
build.timeout_source
end
def trigger_variables def trigger_variables
return [] unless trigger_request return [] unless trigger_request
......
...@@ -7,7 +7,7 @@ class BuildDetailsEntity < JobEntity ...@@ -7,7 +7,7 @@ class BuildDetailsEntity < JobEntity
expose :timeout, if: -> (*) { !build.used_timeout.nil? } do |build| expose :timeout, if: -> (*) { !build.used_timeout.nil? } do |build|
{ value: build.used_timeout_human_readable, { value: build.used_timeout_human_readable,
source: build.timeout_source } source: build.present.timeout_source }
end end
expose :erased_by, if: -> (*) { build.erased? }, using: UserEntity expose :erased_by, if: -> (*) { build.erased? }, using: UserEntity
......
...@@ -5,6 +5,6 @@ class AddUsedTimeoutAndTimeoutSourceColumnsToCiBuilds < ActiveRecord::Migration ...@@ -5,6 +5,6 @@ class AddUsedTimeoutAndTimeoutSourceColumnsToCiBuilds < ActiveRecord::Migration
def change def change
add_column :ci_builds, :used_timeout, :integer add_column :ci_builds, :used_timeout, :integer
add_column :ci_builds, :timeout_source, :string add_column :ci_builds, :timeout_source, :integer
end end
end end
...@@ -312,7 +312,7 @@ ActiveRecord::Schema.define(version: 20180327101207) do ...@@ -312,7 +312,7 @@ ActiveRecord::Schema.define(version: 20180327101207) do
t.boolean "protected" t.boolean "protected"
t.integer "failure_reason" t.integer "failure_reason"
t.integer "used_timeout" t.integer "used_timeout"
t.string "timeout_source" t.integer "timeout_source"
end end
add_index "ci_builds", ["artifacts_expire_at"], name: "index_ci_builds_on_artifacts_expire_at", where: "(artifacts_file <> ''::text)", using: :btree add_index "ci_builds", ["artifacts_expire_at"], name: "index_ci_builds_on_artifacts_expire_at", where: "(artifacts_file <> ''::text)", using: :btree
......
...@@ -2046,20 +2046,18 @@ describe Ci::Build do ...@@ -2046,20 +2046,18 @@ describe Ci::Build do
end end
shared_examples 'saves data on transition' do shared_examples 'saves data on transition' do
it 'saves used_timeout and timeout_source on transition' do it 'saves used_timeout' do
expect(job.used_timeout).to be_nil expect { job.run! }.to change { job.reload.used_timeout }.from(nil).to(expected_timeout)
expect(job.timeout_source).to be_nil end
job.run!
expect(job.used_timeout).to eq(expected_timeout) it 'saves timeout_source' do
expect(job.timeout_source).to eq(expected_timeout_source) expect { job.run! }.to change { job.reload.timeout_source }.from('unknown_timeout_source').to(expected_timeout_source)
end end
end end
context 'when runner timeout overrides project timeout' do context 'when runner timeout overrides project timeout' do
let(:expected_timeout) { 900 } let(:expected_timeout) { 900 }
let(:expected_timeout_source) { 'Runner' } let(:expected_timeout_source) { 'runner_timeout_source' }
before do before do
runner.maximum_job_timeout = 900 runner.maximum_job_timeout = 900
...@@ -2071,7 +2069,7 @@ describe Ci::Build do ...@@ -2071,7 +2069,7 @@ describe Ci::Build do
context "when runner timeout doesn't override project timeout" do context "when runner timeout doesn't override project timeout" do
let(:expected_timeout) { 1800 } let(:expected_timeout) { 1800 }
let(:expected_timeout_source) { 'Project' } let(:expected_timeout_source) { 'project_timeout_source' }
before do before do
runner.maximum_job_timeout = 3600 runner.maximum_job_timeout = 3600
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment