jobs: migrate mass updates to ActiveJob.

Also fixes a bug where mod actions weren't logged on mass updates.
Creating the mod action silently failed because it was called when
CurrentUser wasn' set.
This commit is contained in:
evazion
2019-08-19 00:46:31 -05:00
parent 48488d04db
commit dab43d96c9
7 changed files with 144 additions and 162 deletions

View File

@@ -1,13 +1,12 @@
module Moderator
class TagsController < ApplicationController
before_action :moderator_only
rescue_from TagBatchChange::Error, :with => :error
def edit
end
def update
Delayed::Job.enqueue(TagBatchChange.new(params[:tag][:antecedent], params[:tag][:consequent], CurrentUser.user.id, CurrentUser.ip_addr), :queue => "default")
TagBatchChangeJob.perform_later(params[:tag][:antecedent], params[:tag][:consequent], CurrentUser.user, CurrentUser.ip_addr)
redirect_to edit_moderator_tag_path, :notice => "Post changes queued"
end

View File

@@ -0,0 +1,79 @@
class TagBatchChangeJob < ApplicationJob
class Error < Exception ; end
queue_as :default
def perform(antecedent, consequent, updater, updater_ip_addr)
raise Error.new("antecedent is missing") if antecedent.blank?
normalized_antecedent = TagAlias.to_aliased(::Tag.scan_tags(antecedent.mb_chars.downcase))
normalized_consequent = TagAlias.to_aliased(::Tag.scan_tags(consequent.mb_chars.downcase))
CurrentUser.without_safe_mode do
CurrentUser.scoped(updater, updater_ip_addr) do
migrate_posts(normalized_antecedent, normalized_consequent)
migrate_saved_searches(normalized_antecedent, normalized_consequent)
migrate_blacklists(normalized_antecedent, normalized_consequent)
ModAction.log("processed mass update: #{antecedent} -> #{consequent}", :mass_update)
end
end
end
def self.estimate_update_count(antecedent, consequent)
CurrentUser.without_safe_mode do
PostReadOnly.tag_match(antecedent).count
end
end
def migrate_posts(normalized_antecedent, normalized_consequent)
::Post.tag_match(normalized_antecedent.join(" ")).find_each do |post|
post.reload
tags = (post.tag_array - normalized_antecedent + normalized_consequent).join(" ")
post.update(tag_string: tags)
end
end
def migrate_saved_searches(normalized_antecedent, normalized_consequent)
tags = Tag.scan_tags(normalized_antecedent.join(" "), strip_metatags: true)
# https://www.postgresql.org/docs/current/static/functions-array.html
saved_searches = SavedSearch.where("string_to_array(query, ' ') @> ARRAY[?]", tags)
saved_searches.find_each do |ss|
ss.query = (ss.query.split - tags + normalized_consequent).uniq.join(" ")
ss.save
end
end
# this can't handle negated tags or other special cases
def migrate_blacklists(normalized_antecedent, normalized_consequent)
query = normalized_antecedent
adds = normalized_consequent
arel = query.inject(User.none) do |scope, x|
scope.or(User.where("blacklisted_tags like ?", "%" + x.to_escaped_for_sql_like + "%"))
end
arel.find_each do |user|
changed = false
begin
repl = user.blacklisted_tags.split(/\r\n|\r|\n/).map do |line|
list = Tag.scan_tags(line)
if (list & query).size != query.size
next line
end
changed = true
(list - query + adds).join(" ")
end
if changed
user.update(blacklisted_tags: repl.join("\n"))
end
rescue Exception => e
DanbooruLogger.log(e)
end
end
end
end

View File

@@ -89,7 +89,7 @@ class AliasAndImplicationImporter
sum + TagImplication.new(antecedent_name: token[1], consequent_name: token[2]).estimate_update_count
when :mass_update
sum + Moderator::TagBatchChange.new(token[1], token[2]).estimate_update_count
sum + TagBatchChangeJob.estimate_update_count(token[1], token[2])
when :change_category
sum + Tag.find_by_name(token[1]).try(:post_count) || 0
@@ -156,7 +156,7 @@ private
tag_implication.reject!(update_topic: false)
when :mass_update
Delayed::Job.enqueue(Moderator::TagBatchChange.new(token[1], token[2], CurrentUser.id, CurrentUser.ip_addr), :queue => "default")
TagBatchChangeJob.perform_later(token[1], token[2], CurrentUser.user, CurrentUser.ip_addr)
when :change_category
tag = Tag.find_by_name(token[1])

View File

@@ -1,80 +0,0 @@
module Moderator
class TagBatchChange < Struct.new(:antecedent, :consequent, :updater_id, :updater_ip_addr)
class Error < Exception ; end
def perform
raise Error.new("antecedent is missing") if antecedent.blank?
normalized_antecedent = TagAlias.to_aliased(::Tag.scan_tags(antecedent.mb_chars.downcase))
normalized_consequent = TagAlias.to_aliased(::Tag.scan_tags(consequent.mb_chars.downcase))
updater = User.find(updater_id)
CurrentUser.without_safe_mode do
CurrentUser.scoped(updater, updater_ip_addr) do
migrate_posts(normalized_antecedent, normalized_consequent)
migrate_saved_searches(normalized_antecedent, normalized_consequent)
migrate_blacklists(normalized_antecedent, normalized_consequent)
end
end
ModAction.log("processed mass update: #{antecedent} -> #{consequent}",:mass_update)
end
def estimate_update_count
PostReadOnly.tag_match(antecedent).count
end
def migrate_posts(normalized_antecedent, normalized_consequent)
::Post.tag_match(normalized_antecedent.join(" ")).find_each do |post|
post.reload
tags = (post.tag_array - normalized_antecedent + normalized_consequent).join(" ")
post.update(tag_string: tags)
end
end
def migrate_saved_searches(normalized_antecedent, normalized_consequent)
if SavedSearch.enabled?
tags = Tag.scan_tags(normalized_antecedent.join(" "), strip_metatags: true)
# https://www.postgresql.org/docs/current/static/functions-array.html
saved_searches = SavedSearch.where("string_to_array(query, ' ') @> ARRAY[?]", tags)
saved_searches.find_each do |ss|
ss.query = (ss.query.split - tags + normalized_consequent).uniq.join(" ")
ss.save
end
end
end
# this can't handle negated tags or other special cases
def migrate_blacklists(normalized_antecedent, normalized_consequent)
query = normalized_antecedent
adds = normalized_consequent
arel = query.inject(User.none) do |scope, x|
scope.or(User.where("blacklisted_tags like ?", "%" + x.to_escaped_for_sql_like + "%"))
end
arel.find_each do |user|
changed = false
begin
repl = user.blacklisted_tags.split(/\r\n|\r|\n/).map do |line|
list = Tag.scan_tags(line)
if (list & query).size != query.size
next line
end
changed = true
(list - query + adds).join(" ")
end
if changed
user.update(blacklisted_tags: repl.join("\n"))
end
rescue Exception => e
DanbooruLogger.log(e)
end
end
end
end
end