debian-mirror-gitlab/rubocop/cop/scalability/bulk_perform_with_context.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.7 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
require_relative '../../migration_helpers'
require_relative '../../code_reuse_helpers'
module RuboCop
module Cop
module Scalability
2022-10-11 01:57:18 +05:30
class BulkPerformWithContext < RuboCop::Cop::Base
2020-03-13 15:44:24 +05:30
include RuboCop::MigrationHelpers
include RuboCop::CodeReuseHelpers
MSG = <<~MSG
Prefer using `Worker.bulk_perform_async_with_contexts` and
`Worker.bulk_perform_in_with_context` over the methods without a context
if your worker deals with specific projects or namespaces
The context is required to add metadata to our logs.
If there is already a parent context that will apply to the jobs
being scheduled, please disable this cop with a comment explaing which
context will be applied.
2022-08-27 11:52:29 +05:30
Read more about it https://docs.gitlab.com/ee/development/sidekiq/logging.html#worker-context
2020-03-13 15:44:24 +05:30
MSG
2022-04-04 11:22:00 +05:30
BACKGROUND_MIGRATION_WORKER_NAMES = %w[BackgroundMigrationWorker CiDatabaseWorker].freeze
2020-03-13 15:44:24 +05:30
def_node_matcher :schedules_in_batch_without_context?, <<~PATTERN
(send (...) {:bulk_perform_async :bulk_perform_in} _*)
PATTERN
def on_send(node)
return if in_migration?(node) || in_spec?(node)
return unless schedules_in_batch_without_context?(node)
2022-04-04 11:22:00 +05:30
return if scheduled_for_background_migration?(node)
2020-03-13 15:44:24 +05:30
2022-10-11 01:57:18 +05:30
add_offense(node)
2020-03-13 15:44:24 +05:30
end
private
def in_spec?(node)
file_path_for_node(node).end_with?("_spec.rb")
end
2022-04-04 11:22:00 +05:30
def scheduled_for_background_migration?(node)
BACKGROUND_MIGRATION_WORKER_NAMES.include?(name_of_receiver(node))
end
2020-03-13 15:44:24 +05:30
end
end
end
end