Class: Ci::ResourceGroup

Inherits:
ApplicationRecord show all
Defined in:
app/models/ci/resource_group.rb

Constant Summary

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Instance Method Summary collapse

Methods inherited from ApplicationRecord

model_name, table_name_prefix

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Instance Method Details

#assign_resource_to(processable) ⇒ Object

NOTE: This is concurrency-safe method that the subquery in the `UPDATE` works as explicit locking.


26
27
28
# File 'app/models/ci/resource_group.rb', line 26

def assign_resource_to(processable)
  resources.free.limit(1).update_all(build_id: processable.id) > 0
end

#release_resource_from(processable) ⇒ Object


30
31
32
# File 'app/models/ci/resource_group.rb', line 30

def release_resource_from(processable)
  resources.retained_by(processable).update_all(build_id: nil) > 0
end

#upcoming_processablesObject


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/ci/resource_group.rb', line 34

def upcoming_processables
  if unordered?
    processables.waiting_for_resource
  elsif oldest_first?
    processables.waiting_for_resource_or_upcoming
      .order(Arel.sql("commit_id ASC, #{sort_by_job_status}"))
  elsif newest_first?
    processables.waiting_for_resource_or_upcoming
      .order(Arel.sql("commit_id DESC, #{sort_by_job_status}"))
  else
    Ci::Processable.none
  end
end