summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Artur <felipefac@gmail.com>2019-02-18 11:51:44 -0300
committerFelipe Artur <felipefac@gmail.com>2019-02-18 11:51:44 -0300
commita9886c7dd434d6936c455c9877f6c695cafebe0a (patch)
treeece41b57e979bc31814bbc51342b8bb5c5fd673d
parentb1346db3a0b49b295a9702921285fdb30029563c (diff)
downloadgitlab-ce-a9886c7dd434d6936c455c9877f6c695cafebe0a.tar.gz
Fix rubocop
-rw-r--r--lib/gitlab/background_migration.rb2
-rw-r--r--lib/gitlab/background_migration/concerns/reschedulable.rb50
-rw-r--r--lib/gitlab/background_migration/delete_diff_files.rb2
-rw-r--r--lib/gitlab/background_migration/helpers/reschedulable.rb59
4 files changed, 61 insertions, 52 deletions
diff --git a/lib/gitlab/background_migration.rb b/lib/gitlab/background_migration.rb
index b308e94bfa0..948488c844c 100644
--- a/lib/gitlab/background_migration.rb
+++ b/lib/gitlab/background_migration.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-Dir[Rails.root.join("lib/gitlab/background_migration/concerns/*.rb")].each { |f| require f }
+Dir[Rails.root.join("lib/gitlab/background_migration/helpers/*.rb")].each { |f| require f }
module Gitlab
module BackgroundMigration
diff --git a/lib/gitlab/background_migration/concerns/reschedulable.rb b/lib/gitlab/background_migration/concerns/reschedulable.rb
deleted file mode 100644
index fbf3d799743..00000000000
--- a/lib/gitlab/background_migration/concerns/reschedulable.rb
+++ /dev/null
@@ -1,50 +0,0 @@
-# frozen_string_literal: true
-
-module Gitlab
- module BackgroundMigration
- module Reschedulable
- extend ActiveSupport::Concern
-
- def reschedule_if_needed(args, &block)
- if should_reschedule?
- BackgroundMigrationWorker.perform_in(vacuum_wait_time, self.class.name.demodulize, args)
- else
- yield
- end
- end
-
- # Override this on base class if you need a different reschedule condition
- def should_reschedule?
- raise NotImplementedError, "#{self.class} does not implement #{__method__}"
- end
-
- def wait_for_deadtuple_vacuum?(table_name)
- return false unless Gitlab::Database.postgresql?
-
- dead_tuples_count_for(table_name) >= dead_tuples_threshold
- end
-
- def dead_tuples_count_for(table_name)
- dead_tuple =
- execute_statement("SELECT n_dead_tup FROM pg_stat_all_tables "\
- "WHERE relname = '#{table_name}'")[0]
-
- dead_tuple&.fetch('n_dead_tup', 0).to_i
- end
-
- def execute_statement(sql)
- ActiveRecord::Base.connection.execute(sql)
- end
-
- # Override in subclass if you need a different dead tuple threshold
- def dead_tuples_threshold
- @dead_tuples_threshold ||= 50_000
- end
-
- # Override in subclass if you need a different vacuum wait time
- def vacuum_wait_time
- @vacuum_wait_time ||= 5.minutes
- end
- end
- end
-end
diff --git a/lib/gitlab/background_migration/delete_diff_files.rb b/lib/gitlab/background_migration/delete_diff_files.rb
index 0a7cbd5c30f..11851c23ee3 100644
--- a/lib/gitlab/background_migration/delete_diff_files.rb
+++ b/lib/gitlab/background_migration/delete_diff_files.rb
@@ -4,7 +4,7 @@
module Gitlab
module BackgroundMigration
class DeleteDiffFiles
- include Reschedulable
+ include Helpers::Reschedulable
class MergeRequestDiff < ActiveRecord::Base
self.table_name = 'merge_request_diffs'
diff --git a/lib/gitlab/background_migration/helpers/reschedulable.rb b/lib/gitlab/background_migration/helpers/reschedulable.rb
new file mode 100644
index 00000000000..7810f2627c8
--- /dev/null
+++ b/lib/gitlab/background_migration/helpers/reschedulable.rb
@@ -0,0 +1,59 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module BackgroundMigration
+ module Helpers
+ # == Reschedulable helper
+ #
+ # Allows background migrations to be reschedule if a condition is not met.
+ #
+ # Check DeleteDiffFiles migration which reschedules itself if dead tuple count
+ # on DB is not acceptable.
+ #
+ module Reschedulable
+ extend ActiveSupport::Concern
+
+ def reschedule_if_needed(args, &block)
+ if should_reschedule?
+ BackgroundMigrationWorker.perform_in(vacuum_wait_time, self.class.name.demodulize, args)
+ else
+ yield
+ end
+ end
+
+ # Override this on base class if you need a different reschedule condition
+ def should_reschedule?
+ raise NotImplementedError, "#{self.class} does not implement #{__method__}"
+ end
+
+ def wait_for_deadtuple_vacuum?(table_name)
+ return false unless Gitlab::Database.postgresql?
+
+ dead_tuples_count_for(table_name) >= dead_tuples_threshold
+ end
+
+ def dead_tuples_count_for(table_name)
+ dead_tuple =
+ execute_statement("SELECT n_dead_tup FROM pg_stat_all_tables "\
+ "WHERE relname = '#{table_name}'")[0]
+
+ dead_tuple&.fetch('n_dead_tup', 0).to_i
+ end
+
+ def execute_statement(sql)
+ ActiveRecord::Base.connection.execute(sql)
+ end
+
+ # Override in subclass if you need a different dead tuple threshold
+ def dead_tuples_threshold
+ @dead_tuples_threshold ||= 50_000
+ end
+
+ # Override in subclass if you need a different vacuum wait time
+ def vacuum_wait_time
+ @vacuum_wait_time ||= 5.minutes
+ end
+ end
+ end
+ end
+end