debian-mirror-gitlab/app/workers/issue_placement_worker.rb

72 lines
2.5 KiB
Ruby
Raw Normal View History

2020-11-24 15:15:51 +05:30
# frozen_string_literal: true
2022-01-26 12:08:38 +05:30
# DEPRECATED. Will be removed in 14.7 https://gitlab.com/gitlab-org/gitlab/-/merge_requests/72803
# Please use Issues::PlacementWorker instead
#
2021-12-11 22:18:48 +05:30
# todo: remove this worker and it's queue definition from all_queues after Issues::PlacementWorker is deployed
# We want to keep it for one release in case some jobs are already scheduled in the old queue so we need the worker
# to be available to finish those. All new jobs will be queued into the new queue.
2020-11-24 15:15:51 +05:30
class IssuePlacementWorker
include ApplicationWorker
2021-10-27 15:23:28 +05:30
data_consistency :always
2021-06-08 01:23:25 +05:30
sidekiq_options retry: 3
2020-11-24 15:15:51 +05:30
idempotent!
2021-06-08 01:23:25 +05:30
deduplicate :until_executed, including_scheduled: true
2021-12-11 22:18:48 +05:30
feature_category :team_planning
2020-11-24 15:15:51 +05:30
urgency :high
worker_resource_boundary :cpu
weight 2
# Move at most the most recent 100 issues
QUERY_LIMIT = 100
# rubocop: disable CodeReuse/ActiveRecord
def perform(issue_id, project_id = nil)
issue = find_issue(issue_id, project_id)
return unless issue
2021-06-08 01:23:25 +05:30
# Temporary disable moving null elements because of performance problems
# For more information check https://gitlab.com/gitlab-com/gl-infra/production/-/issues/4321
return if issue.blocked_for_repositioning?
2020-11-24 15:15:51 +05:30
# Move the oldest 100 unpositioned items to the end.
# This is to deal with out-of-order execution of the worker,
# while preserving creation order.
to_place = Issue
.relative_positioning_query_base(issue)
2021-11-18 22:05:49 +05:30
.with_null_relative_position
2020-11-24 15:15:51 +05:30
.order({ created_at: :asc }, { id: :asc })
.limit(QUERY_LIMIT + 1)
.to_a
leftover = to_place.pop if to_place.count > QUERY_LIMIT
Issue.move_nulls_to_end(to_place)
2021-06-08 01:23:25 +05:30
Issues::BaseService.new(project: nil).rebalance_if_needed(to_place.max_by(&:relative_position))
2022-01-26 12:08:38 +05:30
Issues::PlacementWorker.perform_async(nil, leftover.project_id) if leftover.present?
2020-11-24 15:15:51 +05:30
rescue RelativePositioning::NoSpaceLeft => e
Gitlab::ErrorTracking.log_exception(e, issue_id: issue_id, project_id: project_id)
2022-01-26 12:08:38 +05:30
Issues::RebalancingWorker.perform_async(nil, *root_namespace_id_to_rebalance(issue, project_id))
2020-11-24 15:15:51 +05:30
end
def find_issue(issue_id, project_id)
2021-01-03 14:25:43 +05:30
return Issue.id_in(issue_id).take if issue_id
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
project = Project.id_in(project_id).take
2020-11-24 15:15:51 +05:30
return unless project
2021-01-03 14:25:43 +05:30
project.issues.take
2020-11-24 15:15:51 +05:30
end
2021-01-03 14:25:43 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2021-09-04 01:27:46 +05:30
private
def root_namespace_id_to_rebalance(issue, project_id)
project_id = project_id.presence || issue.project_id
Project.find(project_id)&.self_or_root_group_ids
end
2020-11-24 15:15:51 +05:30
end