summaryrefslogtreecommitdiff
path: root/rubocop/cop/sidekiq_load_balancing/worker_data_consistency.rb
blob: badd81ff1381ec9afab01c783187af6db59eaf54 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true

require_relative '../../code_reuse_helpers'

module RuboCop
  module Cop
    module SidekiqLoadBalancing
      # This cop checks for a call to `data_consistency` to exist in Sidekiq workers.
      #
      # @example
      #
      # # bad
      # class BadWorker
      #   def perform
      #   end
      # end
      #
      # # good
      # class GoodWorker
      #   data_consistency :delayed
      #
      #   def perform
      #   end
      # end
      #
      class WorkerDataConsistency < RuboCop::Cop::Base
        include CodeReuseHelpers

        HELP_LINK = 'https://docs.gitlab.com/ee/development/sidekiq/worker_attributes.html#job-data-consistency-strategies'

        MISSING_DATA_CONSISTENCY_MSG = <<~MSG
          Should define data_consistency expectation.
          See #{HELP_LINK} for a more detailed explanation of these settings.
        MSG

        DISCOURAGE_ALWAYS_MSG = "Refrain from using `:always` if possible." \
                                "See #{HELP_LINK} for a more detailed explanation of these settings."

        def_node_search :application_worker?, <<~PATTERN
        `(send nil? :include (const nil? :ApplicationWorker))
        PATTERN

        def_node_matcher :data_consistency_value, <<~PATTERN
          `(send nil? :data_consistency $(sym _) ...)
        PATTERN

        def on_class(node)
          return unless application_worker?(node)

          consistency = data_consistency_value(node)
          return add_offense(node, message: MISSING_DATA_CONSISTENCY_MSG) if consistency.nil?

          add_offense(consistency, message: DISCOURAGE_ALWAYS_MSG) if consistency.value == :always
        end
      end
    end
  end
end