summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-04 15:08:40 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-04 15:08:40 +0000
commit6b833f1e0340e00fdee074da9c42c0d4e07a46d2 (patch)
tree6fc3a7a2f8a02fec8d1e7561b453d33eb4048dad /rubocop
parent88a0824944720b6edaaef56376713541b9a02118 (diff)
downloadgitlab-ce-6b833f1e0340e00fdee074da9c42c0d4e07a46d2.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/scalability/bulk_perform_with_context.rb46
-rw-r--r--rubocop/cop/scalability/cron_worker_context.rb39
-rw-r--r--rubocop/rubocop.rb2
3 files changed, 87 insertions, 0 deletions
diff --git a/rubocop/cop/scalability/bulk_perform_with_context.rb b/rubocop/cop/scalability/bulk_perform_with_context.rb
new file mode 100644
index 00000000000..3c5d7f39680
--- /dev/null
+++ b/rubocop/cop/scalability/bulk_perform_with_context.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+require_relative '../../migration_helpers'
+require_relative '../../code_reuse_helpers'
+
+module RuboCop
+ module Cop
+ module Scalability
+ class BulkPerformWithContext < RuboCop::Cop::Cop
+ include RuboCop::MigrationHelpers
+ include RuboCop::CodeReuseHelpers
+
+ MSG = <<~MSG
+ Prefer using `Worker.bulk_perform_async_with_contexts` and
+ `Worker.bulk_perform_in_with_context` over the methods without a context
+ if your worker deals with specific projects or namespaces
+ The context is required to add metadata to our logs.
+
+ If there is already a parent context that will apply to the jobs
+ being scheduled, please disable this cop with a comment explaing which
+ context will be applied.
+
+ Read more about it https://docs.gitlab.com/ee/development/sidekiq_style_guide.html#worker-context
+ MSG
+
+ def_node_matcher :schedules_in_batch_without_context?, <<~PATTERN
+ (send (...) {:bulk_perform_async :bulk_perform_in} _*)
+ PATTERN
+
+ def on_send(node)
+ return if in_migration?(node) || in_spec?(node)
+ return unless schedules_in_batch_without_context?(node)
+ return if name_of_receiver(node) == "BackgroundMigrationWorker"
+
+ add_offense(node, location: :expression)
+ end
+
+ private
+
+ def in_spec?(node)
+ file_path_for_node(node).end_with?("_spec.rb")
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/cop/scalability/cron_worker_context.rb b/rubocop/cop/scalability/cron_worker_context.rb
new file mode 100644
index 00000000000..d8eba0f098e
--- /dev/null
+++ b/rubocop/cop/scalability/cron_worker_context.rb
@@ -0,0 +1,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
diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb
index d39683c271f..5284cef5346 100644
--- a/rubocop/rubocop.rb
+++ b/rubocop/rubocop.rb
@@ -44,6 +44,8 @@ require_relative 'cop/qa/element_with_pattern'
require_relative 'cop/qa/ambiguous_page_object_name'
require_relative 'cop/sidekiq_options_queue'
require_relative 'cop/scalability/file_uploads'
+require_relative 'cop/scalability/bulk_perform_with_context'
+require_relative 'cop/scalability/cron_worker_context'
require_relative 'cop/destroy_all'
require_relative 'cop/ruby_interpolation_in_translation'
require_relative 'code_reuse_helpers'