Class: Ci::PipelineSchedule

Inherits:
ApplicationRecord show all
Extended by:
Gitlab::Utils::Override
Includes:
BatchNullifyDependentAssociations, CronSchedulable, EachBatch, Gitlab::Utils::StrongMemoize, Importable, Limitable, StripAttribute
Defined in:
app/models/ci/pipeline_schedule.rb

Constant Summary collapse

VALID_REF_FORMAT_REGEX =
%r{\A(#{Gitlab::Git::TAG_REF_PREFIX}|#{Gitlab::Git::BRANCH_REF_PREFIX})[\S]+}
SORT_ORDERS =
{
  id_asc: { order_by: 'id', sort: 'asc' },
  id_desc: { order_by: 'id', sort: 'desc' },
  description_asc: { order_by: 'description', sort: 'asc' },
  description_desc: { order_by: 'description', sort: 'desc' },
  ref_asc: { order_by: 'ref', sort: 'asc' },
  ref_desc: { order_by: 'ref', sort: 'desc' },
  next_run_at_asc: { order_by: 'next_run_at', sort: 'asc' },
  next_run_at_desc: { order_by: 'next_run_at', sort: 'desc' },
  created_at_asc: { order_by: 'created_at', sort: 'asc' },
  created_at_desc: { order_by: 'created_at', sort: 'desc' },
  updated_at_asc: { order_by: 'updated_at', sort: 'asc' },
  updated_at_desc: { order_by: 'updated_at', sort: 'desc' }
}.freeze

Constants included from Limitable

Limitable::GLOBAL_SCOPE

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from BatchNullifyDependentAssociations

#nullify_dependent_associations_in_batches

Methods included from Limitable

#exceeds_limits?

Methods included from StripAttribute

#strip_attributes!

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

Class Method Details

.sort_by_attribute(method) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
# File 'app/models/ci/pipeline_schedule.rb', line 67

def self.sort_by_attribute(method)
  sort_order = SORT_ORDERS[method]
  raise ArgumentError, "order undefined" unless sort_order

  reorder(sort_order[:order_by] => sort_order[:sort])
end

Instance Method Details

#daily_limitObject



113
114
115
# File 'app/models/ci/pipeline_schedule.rb', line 113

def daily_limit
  project.actual_limits.limit_for(:ci_daily_pipeline_schedule_triggers)
end

#deactivate!Object



86
87
88
# File 'app/models/ci/pipeline_schedule.rb', line 86

def deactivate!
  update_attribute(:active, false)
end

#destroyObject

Using destroy instead of before_destroy as we want nullify_dependent_associations_in_batches to run first and not in a transaction block. This prevents timeouts for schedules with numerous pipelines



135
136
137
138
139
# File 'app/models/ci/pipeline_schedule.rb', line 135

def destroy
  nullify_dependent_associations_in_batches

  super
end

#expand_short_refObject



141
142
143
144
145
146
# File 'app/models/ci/pipeline_schedule.rb', line 141

def expand_short_ref
  return if ref.blank? || VALID_REF_FORMAT_REGEX.match?(ref) || ambiguous_ref?

  # In case the ref doesn't exist default to the initial value
  self.ref = project.repository.expand_ref(ref) || ref
end

#for_tag?Boolean

Returns:

  • (Boolean)


123
124
125
126
127
# File 'app/models/ci/pipeline_schedule.rb', line 123

def for_tag?
  return false unless ref.present?

  ref.start_with? 'refs/tags/'
end

#inactive?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'app/models/ci/pipeline_schedule.rb', line 82

def inactive?
  !active?
end

#inputs_hashObject



148
149
150
# File 'app/models/ci/pipeline_schedule.rb', line 148

def inputs_hash
  inputs.to_h { |input| [input.name, input.value] }
end

#job_variablesObject



90
91
92
# File 'app/models/ci/pipeline_schedule.rb', line 90

def job_variables
  variables&.map(&:to_hash_variable) || []
end

#last_pipelineObject



152
153
154
# File 'app/models/ci/pipeline_schedule.rb', line 152

def last_pipeline
  pipelines.last
end

#own!(user) ⇒ Object



78
79
80
# File 'app/models/ci/pipeline_schedule.rb', line 78

def own!(user)
  update(owner: user)
end

#owned_by?(current_user) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/ci/pipeline_schedule.rb', line 74

def owned_by?(current_user)
  owner == current_user
end

#ref_for_displayObject



117
118
119
120
121
# File 'app/models/ci/pipeline_schedule.rb', line 117

def ref_for_display
  return unless ref.present?

  ref.gsub(%r{^refs/(heads|tags)/}, '')
end

#schedule_next_run!Object



106
107
108
109
110
111
# File 'app/models/ci/pipeline_schedule.rb', line 106

def schedule_next_run!
  return if cron_values_changed?

  set_next_run_at
  super
end

#set_next_run_atObject



96
97
98
99
100
# File 'app/models/ci/pipeline_schedule.rb', line 96

def set_next_run_at
  self.next_run_at = ::Ci::PipelineSchedules::CalculateNextRunService # rubocop: disable CodeReuse/ServiceClass
                       .new(project)
                       .execute(self, fallback_method: method(:calculate_next_run_at))
end

#worker_cron_expressionObject



129
130
131
# File 'app/models/ci/pipeline_schedule.rb', line 129

def worker_cron_expression
  Settings.cron_jobs['pipeline_schedule_worker']['cron']
end