diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-19 15:07:55 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-19 15:07:55 +0000 |
commit | f92a53a216e6e7d5037ac701efbee5628f91aa9a (patch) | |
tree | 1eb957f0277b50002258681f61db869a2b683fec /app | |
parent | e3764d340e2849fccee8c06278d1f5f686edd35b (diff) | |
download | gitlab-ce-f92a53a216e6e7d5037ac701efbee5628f91aa9a.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app')
-rw-r--r-- | app/models/ci/build.rb | 2 | ||||
-rw-r--r-- | app/models/ci/resource.rb | 13 | ||||
-rw-r--r-- | app/models/ci/resource_group.rb | 39 | ||||
-rw-r--r-- | app/models/project.rb | 1 | ||||
-rw-r--r-- | app/services/ci/retry_build_service.rb | 2 |
5 files changed, 56 insertions, 1 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index e3f0e07bb8f..fbf76f2c36a 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -23,6 +23,7 @@ module Ci belongs_to :runner belongs_to :trigger_request belongs_to :erased_by, class_name: 'User' + belongs_to :resource_group, class_name: 'Ci::ResourceGroup', inverse_of: :builds RUNNER_FEATURES = { upload_multiple_artifacts: -> (build) { build.publishes_artifacts_reports? }, @@ -34,6 +35,7 @@ module Ci }.freeze has_one :deployment, as: :deployable, class_name: 'Deployment' + has_one :resource, class_name: 'Ci::Resource', inverse_of: :build has_many :trace_sections, class_name: 'Ci::BuildTraceSection' has_many :trace_chunks, class_name: 'Ci::BuildTraceChunk', foreign_key: :build_id diff --git a/app/models/ci/resource.rb b/app/models/ci/resource.rb new file mode 100644 index 00000000000..ee5b6546165 --- /dev/null +++ b/app/models/ci/resource.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Ci + class Resource < ApplicationRecord + extend Gitlab::Ci::Model + + belongs_to :resource_group, class_name: 'Ci::ResourceGroup', inverse_of: :resources + belongs_to :build, class_name: 'Ci::Build', inverse_of: :resource + + scope :free, -> { where(build: nil) } + scope :retained_by, -> (build) { where(build: build) } + end +end diff --git a/app/models/ci/resource_group.rb b/app/models/ci/resource_group.rb new file mode 100644 index 00000000000..eb18f3da0bf --- /dev/null +++ b/app/models/ci/resource_group.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Ci + class ResourceGroup < ApplicationRecord + extend Gitlab::Ci::Model + + belongs_to :project, inverse_of: :resource_groups + + has_many :resources, class_name: 'Ci::Resource', inverse_of: :resource_group + has_many :builds, class_name: 'Ci::Build', inverse_of: :resource_group + + validates :key, + length: { maximum: 255 }, + format: { with: Gitlab::Regex.environment_name_regex, + message: Gitlab::Regex.environment_name_regex_message } + + before_create :ensure_resource + + ## + # NOTE: This is concurrency-safe method that the subquery in the `UPDATE` + # works as explicit locking. + def assign_resource_to(build) + resources.free.limit(1).update_all(build_id: build.id) > 0 + end + + def release_resource_from(build) + resources.retained_by(build).update_all(build_id: nil) > 0 + end + + private + + def ensure_resource + # Currently we only support one resource per group, which means + # maximum one build can be set to the resource group, thus builds + # belong to the same resource group are executed once at time. + self.resources.build if self.resources.empty? + end + end +end diff --git a/app/models/project.rb b/app/models/project.rb index cfdcdbed502..1a84827b5ec 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -285,6 +285,7 @@ class Project < ApplicationRecord has_many :pipeline_schedules, class_name: 'Ci::PipelineSchedule' has_many :project_deploy_tokens has_many :deploy_tokens, through: :project_deploy_tokens + has_many :resource_groups, class_name: 'Ci::ResourceGroup', inverse_of: :project has_one :auto_devops, class_name: 'ProjectAutoDevops', inverse_of: :project, autosave: true has_many :custom_attributes, class_name: 'ProjectCustomAttribute' diff --git a/app/services/ci/retry_build_service.rb b/app/services/ci/retry_build_service.rb index 7a5e33c61ba..5abfbd26641 100644 --- a/app/services/ci/retry_build_service.rb +++ b/app/services/ci/retry_build_service.rb @@ -5,7 +5,7 @@ module Ci CLONE_ACCESSORS = %i[pipeline project ref tag options name allow_failure stage stage_id stage_idx trigger_request yaml_variables when environment coverage_regex - description tag_list protected needs].freeze + description tag_list protected needs resource_group].freeze def execute(build) reprocess!(build).tap do |new_build| |