Class: RuboCop::Cop::Carwow::JobsMustDefineQueue

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/carwow/jobs_must_define_queue.rb

Overview

Jobs are important in our systems and having a unified way to write them is essential. All Job classes must define the queue to be executed in. The ‘queue_as` defines in which queue the Job will be executed and can only be defined once. Even more, the queue defined must be known by the system and declared in the configs: `KnownQueues`.

We understand that each application may introduce its own exceptions, so the Cop can be configured to change the allowed classes to identify job classes and also the known queues.

Examples:


# bad
class Bad < ApplicationJob
end

# bad
class Bad < ApplicationJob
  queue_as :minutes
  queue_as :hours
end

# bad
class Bad < ApplicationJob
  queue_as :unknown_queue
end

# good
class Ok < ApplicationJob
  queue_as :minutes
end

Constant Summary collapse

MISSING_QUEUE_MSG =
'Manually set a queue; the default is not executed!'
UNKNOWN_QUEUE_MSG =
'Unknown queue name'
MULTIPLE_DEFINITIONS_MSG =
'Jobs must define a queue only once!'
NOT_CONFIGURED =
'JobClasses and KnownQueues configs must be set before proceeding.'
RESTRICT_ON_SEND =

optimization: don’t call ‘on_send` unless the method name is in this list

i[queue_as].freeze

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rubocop/cop/carwow/jobs_must_define_queue.rb', line 66

def on_class(node)
  return add_global_offense(NOT_CONFIGURED) if missing_config?

  return if skip_on_class?(node)

  queue_definitions = all_queue_definitions(node)

  case queue_definitions.size
  when 0
    missing_queue_offense(node)
  when 1
    # This is ok!
  else
    multiple_definitions_offense(queue_definitions)
  end
end

#on_send(node) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubocop/cop/carwow/jobs_must_define_queue.rb', line 86

def on_send(node)
  return add_global_offense(NOT_CONFIGURED) if missing_config?

  # If the class is not a Job, no need to proceed.
  return unless application_job_top_level?(node.parent)

  _, _queue_as, queue_name_node = *node
  queue_name, _other = *queue_name_node

  return if known_queues.include?(queue_name.to_s)

  add_offense(queue_name_node, message: UNKNOWN_QUEUE_MSG)
end