debian-mirror-gitlab/app/workers/concerns/worker_attributes.rb

179 lines
5.3 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
module WorkerAttributes
extend ActiveSupport::Concern
2020-07-28 23:09:34 +05:30
include Gitlab::ClassAttributes
2019-12-21 20:55:43 +05:30
2019-12-26 22:10:19 +05:30
# Resource boundaries that workers can declare through the
2020-04-08 14:13:33 +05:30
# `resource_boundary` attribute
2019-12-26 22:10:19 +05:30
VALID_RESOURCE_BOUNDARIES = [:memory, :cpu, :unknown].freeze
2020-04-08 14:13:33 +05:30
# Urgencies that workers can declare through the `urgencies` attribute
VALID_URGENCIES = [:high, :low, :throttled].freeze
2021-04-29 21:17:54 +05:30
VALID_DATA_CONSISTENCIES = [:always, :sticky, :delayed].freeze
2020-03-13 15:44:24 +05:30
NAMESPACE_WEIGHTS = {
auto_devops: 2,
auto_merge: 3,
chaos: 2,
deployment: 3,
mail_scheduler: 2,
notifications: 2,
pipeline_cache: 3,
pipeline_creation: 4,
pipeline_default: 3,
pipeline_hooks: 2,
pipeline_processing: 5,
# EE-specific
epics: 2,
incident_management: 2,
security_scans: 2
}.stringify_keys.freeze
2019-12-21 20:55:43 +05:30
class_methods do
2020-07-28 23:09:34 +05:30
def feature_category(value, *extras)
2019-12-21 20:55:43 +05:30
raise "Invalid category. Use `feature_category_not_owned!` to mark a worker as not owned" if value == :not_owned
2020-07-28 23:09:34 +05:30
class_attributes[:feature_category] = value
2019-12-21 20:55:43 +05:30
end
# Special case: mark this work as not associated with a feature category
# this should be used for cross-cutting concerns, such as mailer workers.
def feature_category_not_owned!
2020-07-28 23:09:34 +05:30
class_attributes[:feature_category] = :not_owned
2019-12-21 20:55:43 +05:30
end
def get_feature_category
2020-07-28 23:09:34 +05:30
get_class_attribute(:feature_category)
2019-12-21 20:55:43 +05:30
end
def feature_category_not_owned?
2020-07-28 23:09:34 +05:30
get_feature_category == :not_owned
2019-12-21 20:55:43 +05:30
end
2020-04-08 14:13:33 +05:30
# This should be set to :high for jobs that need to be run
# immediately, or, if they are delayed, risk creating
# inconsistencies in the application that could being perceived by
# the user as incorrect behavior (ie, a bug)
#
# See
# doc/development/sidekiq_style_guide.md#urgency
2019-12-26 22:10:19 +05:30
# for details
2020-04-08 14:13:33 +05:30
def urgency(urgency)
raise "Invalid urgency: #{urgency}" unless VALID_URGENCIES.include?(urgency)
2020-07-28 23:09:34 +05:30
class_attributes[:urgency] = urgency
2019-12-26 22:10:19 +05:30
end
2020-04-08 14:13:33 +05:30
def get_urgency
2020-07-28 23:09:34 +05:30
class_attributes[:urgency] || :low
2019-12-26 22:10:19 +05:30
end
2021-04-29 21:17:54 +05:30
def data_consistency(data_consistency, feature_flag: nil)
raise ArgumentError, "Invalid data consistency: #{data_consistency}" unless VALID_DATA_CONSISTENCIES.include?(data_consistency)
raise ArgumentError, 'Data consistency is already set' if class_attributes[:data_consistency]
class_attributes[:data_consistency_feature_flag] = feature_flag if feature_flag
class_attributes[:data_consistency] = data_consistency
validate_worker_attributes!
end
def validate_worker_attributes!
# Since the deduplication should always take into account the latest binary replication pointer into account,
# not the first one, the deduplication will not work with sticky or delayed.
# Follow up issue to improve this: https://gitlab.com/gitlab-org/gitlab/-/issues/325291
if idempotent? && get_data_consistency != :always
raise ArgumentError, "Class can't be marked as idempotent if data_consistency is not set to :always"
end
end
def get_data_consistency
class_attributes[:data_consistency] || :always
end
def get_data_consistency_feature_flag_enabled?
return true unless class_attributes[:data_consistency_feature_flag]
Feature.enabled?(class_attributes[:data_consistency_feature_flag], default_enabled: :yaml)
end
2019-12-26 22:10:19 +05:30
# Set this attribute on a job when it will call to services outside of the
# application, such as 3rd party applications, other k8s clusters etc See
2020-08-09 01:23:26 +05:30
# doc/development/sidekiq_style_guide.md#jobs-with-external-dependencies for
2019-12-26 22:10:19 +05:30
# details
def worker_has_external_dependencies!
2020-07-28 23:09:34 +05:30
class_attributes[:external_dependencies] = true
2019-12-26 22:10:19 +05:30
end
# Returns a truthy value if the worker has external dependencies.
2020-08-09 01:23:26 +05:30
# See doc/development/sidekiq_style_guide.md#jobs-with-external-dependencies
2019-12-26 22:10:19 +05:30
# for details
def worker_has_external_dependencies?
2020-07-28 23:09:34 +05:30
class_attributes[:external_dependencies]
2019-12-26 22:10:19 +05:30
end
def worker_resource_boundary(boundary)
raise "Invalid boundary" unless VALID_RESOURCE_BOUNDARIES.include? boundary
2020-07-28 23:09:34 +05:30
class_attributes[:resource_boundary] = boundary
2019-12-26 22:10:19 +05:30
end
def get_worker_resource_boundary
2020-07-28 23:09:34 +05:30
class_attributes[:resource_boundary] || :unknown
2019-12-26 22:10:19 +05:30
end
2020-04-08 14:13:33 +05:30
def idempotent!
2020-07-28 23:09:34 +05:30
class_attributes[:idempotent] = true
2021-04-29 21:17:54 +05:30
validate_worker_attributes!
2020-04-08 14:13:33 +05:30
end
def idempotent?
2020-07-28 23:09:34 +05:30
class_attributes[:idempotent]
2020-04-08 14:13:33 +05:30
end
2020-03-13 15:44:24 +05:30
def weight(value)
2020-07-28 23:09:34 +05:30
class_attributes[:weight] = value
2020-03-13 15:44:24 +05:30
end
def get_weight
2020-07-28 23:09:34 +05:30
class_attributes[:weight] ||
2020-03-13 15:44:24 +05:30
NAMESPACE_WEIGHTS[queue_namespace] ||
1
end
2020-06-23 00:09:42 +05:30
def tags(*values)
2020-07-28 23:09:34 +05:30
class_attributes[:tags] = values
2020-06-23 00:09:42 +05:30
end
def get_tags
2020-07-28 23:09:34 +05:30
Array(class_attributes[:tags])
2020-06-23 00:09:42 +05:30
end
def deduplicate(strategy, options = {})
2020-07-28 23:09:34 +05:30
class_attributes[:deduplication_strategy] = strategy
class_attributes[:deduplication_options] = options
2020-06-23 00:09:42 +05:30
end
def get_deduplicate_strategy
2020-07-28 23:09:34 +05:30
class_attributes[:deduplication_strategy] ||
2020-06-23 00:09:42 +05:30
Gitlab::SidekiqMiddleware::DuplicateJobs::DuplicateJob::DEFAULT_STRATEGY
end
def get_deduplication_options
2020-07-28 23:09:34 +05:30
class_attributes[:deduplication_options] || {}
2019-12-21 20:55:43 +05:30
end
2021-04-17 20:07:23 +05:30
def big_payload!
class_attributes[:big_payload] = true
end
def big_payload?
class_attributes[:big_payload]
end
2019-12-21 20:55:43 +05:30
end
end