Class: Ci::Stage

Inherits:
ApplicationRecord show all
Includes:
HasStatus, Partitionable, Gitlab::OptimisticLocking, Importable, Presentable
Defined in:
app/models/ci/stage.rb

Constant Summary

Constants included from Gitlab::OptimisticLocking

Gitlab::OptimisticLocking::MAX_RETRIES

Constants included from HasStatus

HasStatus::ACTIVE_STATUSES, HasStatus::ALIVE_STATUSES, HasStatus::AVAILABLE_STATUSES, HasStatus::BLOCKED_STATUS, HasStatus::CANCELABLE_STATUSES, HasStatus::COMPLETED_STATUSES, HasStatus::COMPLETED_WITH_MANUAL_STATUSES, HasStatus::DEFAULT_STATUS, HasStatus::EXECUTING_STATUSES, HasStatus::IGNORED_STATUSES, HasStatus::ORDERED_STATUSES, HasStatus::PASSED_WITH_WARNINGS_STATUSES, HasStatus::STARTED_STATUSES, HasStatus::STATUSES_ENUM, HasStatus::STOPPED_STATUSES, HasStatus::UnknownStatusError

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Instance Attribute Summary

Attributes included from Importable

#importing, #user_contributions

Instance Method Summary collapse

Methods included from Presentable

#present

Methods included from Gitlab::OptimisticLocking

log_optimistic_lock_retries, retry_lock, retry_lock_histogram, retry_lock_logger

Methods included from HasStatus

#active?, #blocked?, #complete?, #complete_or_manual?, #incomplete?, #started?

Methods included from Partitionable

registered_models

Methods inherited from ApplicationRecord

model_name, table_name_prefix

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, 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 Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Instance Method Details

#confirm_manual_job?Boolean

We only check jobs that are played by Ci::PlayManualStageService.

Returns:

  • (Boolean)


201
202
203
204
205
206
207
208
# File 'app/models/ci/stage.rb', line 201

def confirm_manual_job?
  manual_jobs = processables.manual
  return false unless manual_jobs.exists?

  manual_jobs.includes(:pipeline, :metadata, [deployment: [environment: :project]]).any? do |job|
    job.playable? && job.manual_confirmation_message
  end
end

#detailed_status(current_user) ⇒ Object



190
191
192
193
194
# File 'app/models/ci/stage.rb', line 190

def detailed_status(current_user)
  Gitlab::Ci::Status::Stage::Factory
    .new(self, current_user)
    .fabricate!
end

#groupsObject



170
171
172
# File 'app/models/ci/stage.rb', line 170

def groups
  @groups ||= Ci::Group.fabricate(project, self)
end

#has_warnings?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'app/models/ci/stage.rb', line 174

def has_warnings?
  number_of_warnings > 0
end

#latest_stage_statusObject

This will be removed with ci_remove_ensure_stage_service



211
212
213
# File 'app/models/ci/stage.rb', line 211

def latest_stage_status
  statuses.latest.composite_status || 'skipped'
end

#manual_playable?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'app/models/ci/stage.rb', line 196

def manual_playable?
  blocked? || skipped?
end

#number_of_warningsObject



178
179
180
181
182
183
184
185
186
187
188
# File 'app/models/ci/stage.rb', line 178

def number_of_warnings
  BatchLoader.for([id, partition_id]).batch(default_value: 0) do |items, loader|
    ::CommitStatus
      .where([:stage_id, :partition_id] => items)
      .latest
      .failed_but_allowed
      .group(:stage_id, :partition_id)
      .count
      .each { |item, amount| loader.call(item, amount) }
  end
end

#ordered_latest_statusesObject



215
216
217
218
# File 'app/models/ci/stage.rb', line 215

def ordered_latest_statuses
  statuses_list = statuses.in_order_of(:status, Ci::HasStatus::ORDERED_STATUSES).latest_ordered.to_a
  (statuses_list.sort_by { |s| [Ci::HasStatus::ORDERED_STATUSES.index(s.status), s.sortable_name] })
end

#ordered_retried_statusesObject



220
221
222
# File 'app/models/ci/stage.rb', line 220

def ordered_retried_statuses
  (statuses.in_order_of(:status, Ci::HasStatus::ORDERED_STATUSES).retried_ordered)
end

#set_status(new_status) ⇒ Object

rubocop: disable Metrics/CyclomaticComplexity – breaking apart hurts readability, consider refactoring issue #439268



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/ci/stage.rb', line 142

def set_status(new_status)
  retry_optimistic_lock(self, name: 'ci_stage_set_status') do
    case new_status
    when 'created' then nil
    when 'waiting_for_resource' then request_resource
    when 'preparing' then prepare
    when 'waiting_for_callback' then wait_for_callback
    when 'pending' then enqueue
    when 'running' then run
    when 'success' then succeed
    when 'failed' then drop
    when 'canceling' then start_cancel
    when 'canceled' then cancel
    when 'manual' then block
    when 'scheduled' then delay
    when 'skipped', nil then skip
    else
      raise Ci::HasStatus::UnknownStatusError, "Unknown status `#{new_status}`"
    end
  end
end

#update_legacy_statusObject

This will be removed with ci_remove_ensure_stage_service



166
167
168
# File 'app/models/ci/stage.rb', line 166

def update_legacy_status
  set_status(latest_stage_status.to_s)
end