2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Scalability
|
2022-10-11 01:57:18 +05:30
|
|
|
class CronWorkerContext < RuboCop::Cop::Base
|
2020-03-13 15:44:24 +05:30
|
|
|
MSG = <<~MSG
|
|
|
|
Manually define an ApplicationContext for cronjob-workers. The context
|
|
|
|
is required to add metadata to our logs.
|
|
|
|
|
|
|
|
If there is no relevant metadata, please disable the cop with a comment
|
|
|
|
explaining this.
|
|
|
|
|
2022-04-04 11:22:00 +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
|
|
|
|
|
|
|
|
def_node_matcher :includes_cronjob_queue?, <<~PATTERN
|
|
|
|
(send nil? :include (const nil? :CronjobQueue))
|
|
|
|
PATTERN
|
|
|
|
|
|
|
|
def_node_search :defines_contexts?, <<~PATTERN
|
|
|
|
(send nil? :with_context _)
|
|
|
|
PATTERN
|
|
|
|
|
|
|
|
def_node_search :schedules_with_batch_context?, <<~PATTERN
|
|
|
|
(send (...) {:bulk_perform_async_with_contexts :bulk_perform_in_with_contexts} _*)
|
|
|
|
PATTERN
|
|
|
|
|
|
|
|
def on_send(node)
|
|
|
|
return unless includes_cronjob_queue?(node)
|
|
|
|
return if defines_contexts?(node.parent)
|
|
|
|
return if schedules_with_batch_context?(node.parent)
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
add_offense(node.arguments.first)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|