Class: RuboCop::Cop::Carwow::Jobs

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

Overview

Jobs are important in our systems and having observability well configured is essential. All Job classes must behave as we expect, and only a few well-defined classes can be super classes for other jobs.

We use ActiveJob as framework for declaring jobs, being ‘ActiveJob::Base` the top level class. However, our jobs are generally split into two big hierarchies: ApplicationJob and ScheduledJob; and all jobs should inherit from these classes rather than `ActiveJob::Base`.

We understand that each application may introduce its own exceptions, so the Cop can be configured to change the allowed super classes and even the top level class!

Examples:

# bad
class Bad < ActiveJob::Base; end

# bad
class Bad < NotAllowedJob; end

# good
class Good < ApplicationJob; end

# good
class ScheduledJob < ApplicationJob; end

# good
class ApplicationJob < ActiveJob::Base; end

Constant Summary collapse

MSG =
'Jobs should subclass from one of: %s.'
TOP_LEVEL_MSG =
'Only %s can inherit from %s.'
FORCE_TOP_LEVEL_MSG =
'It should inherit from %s or %s.'
NOT_CONFIGURED =
'AllowedSuperclasses and TopLevelJob configs must be set before proceeding.'

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/carwow/jobs.rb', line 55

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

  return if any_class_in_the_file_allowed?(node)
  return if defining_allowed_job?(*node)

  register_offenses(node)
end