summaryrefslogtreecommitdiff
path: root/app/models/ci/resource_group.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-19 15:07:55 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-19 15:07:55 +0000
commitf92a53a216e6e7d5037ac701efbee5628f91aa9a (patch)
tree1eb957f0277b50002258681f61db869a2b683fec /app/models/ci/resource_group.rb
parente3764d340e2849fccee8c06278d1f5f686edd35b (diff)
downloadgitlab-ce-f92a53a216e6e7d5037ac701efbee5628f91aa9a.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/ci/resource_group.rb')
-rw-r--r--app/models/ci/resource_group.rb39
1 files changed, 39 insertions, 0 deletions
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