From 45cecff1a6c8e1a9f25fb7db7a1d478a2835ec52 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 26 Nov 2017 13:20:31 -0600 Subject: [PATCH] Move with_timeout / without_timeout to ApplicationRecord. --- app/logical/bulk_revert.rb | 2 +- app/logical/moderator/dashboard/report.rb | 18 +++++++-------- app/models/application_record.rb | 23 +++++++++++++++++++ .../initializers/active_record_extensions.rb | 21 ----------------- ...000519_add_created_at_index_to_versions.rb | 2 +- ...30231_add_forum_post_id_to_tag_requests.rb | 2 +- ...856_add_unique_name_constraint_to_users.rb | 2 +- script/fixes/046_fix_nicovideo_artist_urls.rb | 2 +- 8 files changed, 37 insertions(+), 35 deletions(-) diff --git a/app/logical/bulk_revert.rb b/app/logical/bulk_revert.rb index 283b651bf..510bbccff 100644 --- a/app/logical/bulk_revert.rb +++ b/app/logical/bulk_revert.rb @@ -10,7 +10,7 @@ class BulkRevert ModAction.log("Processed bulk revert for #{constraints.inspect} by #{creator.name}") CurrentUser.scoped(creator) do - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do find_post_versions.order("updated_at, id").each do |version| version.undo! end diff --git a/app/logical/moderator/dashboard/report.rb b/app/logical/moderator/dashboard/report.rb index 39ce34f8b..9f1628d08 100644 --- a/app/logical/moderator/dashboard/report.rb +++ b/app/logical/moderator/dashboard/report.rb @@ -9,37 +9,37 @@ module Moderator end def artists - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do Queries::Artist.all(min_date, max_level) end end def comments - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do Queries::Comment.all(min_date, max_level) end end def mod_actions - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do Queries::ModAction.all end end def notes - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do Queries::Note.all(min_date, max_level) end end def appeals - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do Queries::PostAppeal.all(min_date) end end def flags - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do Queries::PostFlag.all(min_date) end end @@ -49,19 +49,19 @@ module Moderator end def posts - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do Queries::Upload.all(min_date, max_level) end end def user_feedbacks - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do Queries::UserFeedback.all end end def wiki_pages - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do Queries::WikiPage.all(min_date, max_level) end end diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 5b881b4b8..12463bf97 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -43,6 +43,29 @@ class ApplicationRecord < ActiveRecord::Base end end + concerning :ActiveRecordExtensions do + class_methods do + def without_timeout + connection.execute("SET STATEMENT_TIMEOUT = 0") unless Rails.env == "test" + yield + ensure + connection.execute("SET STATEMENT_TIMEOUT = #{CurrentUser.user.try(:statement_timeout) || 3_000}") unless Rails.env == "test" + end + + def with_timeout(n, default_value = nil, new_relic_params = {}) + connection.execute("SET STATEMENT_TIMEOUT = #{n}") unless Rails.env == "test" + yield + rescue ::ActiveRecord::StatementInvalid => x + if Rails.env.production? + NewRelic::Agent.notice_error(x, :custom_params => new_relic_params.merge(:user_id => CurrentUser.id, :user_ip_addr => CurrentUser.ip_addr)) + end + return default_value + ensure + connection.execute("SET STATEMENT_TIMEOUT = #{CurrentUser.user.try(:statement_timeout) || 3_000}") unless Rails.env == "test" + end + end + end + concerning :PostgresExtensions do class_methods do def columns(*params) diff --git a/config/initializers/active_record_extensions.rb b/config/initializers/active_record_extensions.rb index c4270c5a3..55d46a950 100644 --- a/config/initializers/active_record_extensions.rb +++ b/config/initializers/active_record_extensions.rb @@ -3,27 +3,6 @@ module Danbooru module ActiveRecord extend ActiveSupport::Concern - module ClassMethods - def without_timeout - connection.execute("SET STATEMENT_TIMEOUT = 0") unless Rails.env == "test" - yield - ensure - connection.execute("SET STATEMENT_TIMEOUT = #{CurrentUser.user.try(:statement_timeout) || 3_000}") unless Rails.env == "test" - end - - def with_timeout(n, default_value = nil, new_relic_params = {}) - connection.execute("SET STATEMENT_TIMEOUT = #{n}") unless Rails.env == "test" - yield - rescue ::ActiveRecord::StatementInvalid => x - if Rails.env.production? - NewRelic::Agent.notice_error(x, :custom_params => new_relic_params.merge(:user_id => CurrentUser.id, :user_ip_addr => CurrentUser.ip_addr)) - end - return default_value - ensure - connection.execute("SET STATEMENT_TIMEOUT = #{CurrentUser.user.try(:statement_timeout) || 3_000}") unless Rails.env == "test" - end - end - %w(execute select_value select_values select_all).each do |method_name| define_method("#{method_name}_sql") do |sql, *params| self.class.connection.__send__(method_name, self.class.sanitize_sql_array([sql, *params])) diff --git a/db/migrate/20170319000519_add_created_at_index_to_versions.rb b/db/migrate/20170319000519_add_created_at_index_to_versions.rb index 38b6df765..e8ff515ac 100644 --- a/db/migrate/20170319000519_add_created_at_index_to_versions.rb +++ b/db/migrate/20170319000519_add_created_at_index_to_versions.rb @@ -1,6 +1,6 @@ class AddCreatedAtIndexToVersions < ActiveRecord::Migration def change - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do add_index :note_versions, :created_at add_index :artist_versions, :created_at add_index :wiki_page_versions, :created_at diff --git a/db/migrate/20170330230231_add_forum_post_id_to_tag_requests.rb b/db/migrate/20170330230231_add_forum_post_id_to_tag_requests.rb index 7b1e3d343..055e40c72 100644 --- a/db/migrate/20170330230231_add_forum_post_id_to_tag_requests.rb +++ b/db/migrate/20170330230231_add_forum_post_id_to_tag_requests.rb @@ -1,6 +1,6 @@ class AddForumPostIdToTagRequests < ActiveRecord::Migration def change - ActiveRecord::Base.without_timeout do + ApplicationRecord.without_timeout do add_column :tag_aliases, :forum_post_id, :integer add_column :tag_implications, :forum_post_id, :integer add_column :bulk_update_requests, :forum_post_id, :integer diff --git a/db/migrate/20170414005856_add_unique_name_constraint_to_users.rb b/db/migrate/20170414005856_add_unique_name_constraint_to_users.rb index 3bf8d608f..5243ee926 100644 --- a/db/migrate/20170414005856_add_unique_name_constraint_to_users.rb +++ b/db/migrate/20170414005856_add_unique_name_constraint_to_users.rb @@ -1,6 +1,6 @@ class AddUniqueNameConstraintToUsers < ActiveRecord::Migration def up - ActiveRecord::Base.without_timeout do + User.without_timeout do remove_index :users, :name execute "create unique index index_users_on_name on users(lower(name))" end diff --git a/script/fixes/046_fix_nicovideo_artist_urls.rb b/script/fixes/046_fix_nicovideo_artist_urls.rb index f7b8319a0..b2830d5b1 100644 --- a/script/fixes/046_fix_nicovideo_artist_urls.rb +++ b/script/fixes/046_fix_nicovideo_artist_urls.rb @@ -2,7 +2,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')) -ActiveRecord::Base.without_timeout do +ArtistUrl.without_timeout do ArtistUrl.where("normalized_url like ?", "\%nicovideo\%").find_each do |url| before = url.normalized_url url.normalize