jobs: migrate aliases/implications to ActiveJob.

This commit is contained in:
evazion
2019-08-16 20:49:35 -05:00
parent 2bbdc5d143
commit 2b6361369e
5 changed files with 32 additions and 14 deletions

View File

@@ -0,0 +1,8 @@
class ProcessTagAliasJob < ApplicationJob
queue_as :default
queue_with_priority 20
def perform(tag_alias, update_topic: true)
tag_alias.process!(update_topic: update_topic)
end
end

View File

@@ -0,0 +1,8 @@
class ProcessTagImplicationJob < ApplicationJob
queue_as :default
queue_with_priority 20
def perform(tag_implication, update_topic: true)
tag_implication.process!(update_topic: update_topic)
end
end

View File

@@ -6,11 +6,9 @@ class TagAlias < TagRelationship
validate :mininum_antecedent_count, on: :create, unless: :skip_secondary_validations
module ApprovalMethods
def approve!(update_topic: true, approver: CurrentUser.user)
CurrentUser.scoped(approver) do
update(status: "queued", approver_id: approver.id)
delay(:queue => "default").process!(update_topic: update_topic)
end
def approve!(approver: CurrentUser.user, update_topic: true)
update(approver: approver, status: "queued")
ProcessTagAliasJob.perform_later(self, update_topic: update_topic)
end
end

View File

@@ -163,8 +163,8 @@ class TagImplication < TagRelationship
end
def approve!(approver: CurrentUser.user, update_topic: true)
update(status: "queued", approver_id: approver.id)
delay(:queue => "default").process!(update_topic: update_topic)
update(approver: approver, status: "queued")
ProcessTagImplicationJob.perform_later(self, update_topic: update_topic)
end
def reject!(update_topic: true)

View File

@@ -245,8 +245,11 @@ class TagImplicationTest < ActiveSupport::TestCase
p1 = FactoryBot.create(:post, :tag_string => "aaa bbb ccc")
ti1 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "xxx")
ti2 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "yyy")
ti1.approve!
ti2.approve!
perform_enqueued_jobs do
ti1.approve!
ti2.approve!
end
assert_equal("aaa bbb ccc xxx yyy", p1.reload.tag_string)
end
@@ -261,12 +264,13 @@ class TagImplicationTest < ActiveSupport::TestCase
should "update the topic when processed" do
assert_difference("ForumPost.count") do
@implication.approve!
perform_enqueued_jobs do
@implication.approve!
end
end
@post.reload
@topic.reload
assert_match(/The tag implication .* has been approved/, @post.body)
assert_equal("[APPROVED] Tag implication: aaa -> bbb", @topic.title)
assert_match(/The tag implication .* has been approved/, @post.reload.body)
assert_equal("[APPROVED] Tag implication: aaa -> bbb", @topic.reload.title)
end
should "update the topic when rejected" do