Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cron_worker_context.rb « scalability « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8e754af4dcf6f65b1c2990829981f6887345487e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

module RuboCop
  module Cop
    module Scalability
      class CronWorkerContext < RuboCop::Cop::Cop
        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.

          Read more about it https://docs.gitlab.com/ee/development/sidekiq_style_guide.html#worker-context
        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)

          add_offense(node.arguments.first, location: :expression)
        end
      end
    end
  end
end