Refactoring: Migrated Observer::Ticket::Article::CommunicateEmail::BackgroundJob (Delayed::Job) to TicketArticleCommunicateEmailJob (ActiveJob).

This commit is contained in:
Rolf Schmidt 2019-02-20 12:18:47 +01:00 committed by Thorsten Eckel
parent ff0330d5c7
commit 6839f040e0
3 changed files with 26 additions and 37 deletions

View file

@ -1,10 +1,11 @@
class Observer::Ticket::Article::CommunicateEmail::BackgroundJob class TicketArticleCommunicateEmailJob < ApplicationJob
def initialize(id)
@article_id = id
end
def perform retry_on StandardError, attempts: 4, wait: lambda { |executions|
record = Ticket::Article.find(@article_id) executions * 25.seconds
}
def perform(article_id)
record = Ticket::Article.find(article_id)
# build subject # build subject
ticket = Ticket.lookup(id: record.ticket_id) ticket = Ticket.lookup(id: record.ticket_id)
@ -163,16 +164,4 @@ class Observer::Ticket::Article::CommunicateEmail::BackgroundJob
raise message raise message
end end
def max_attempts
4
end
def reschedule_at(current_time, attempts)
if Rails.env.production?
return current_time + attempts * 25.seconds
end
current_time + 5.seconds
end
end end

View file

@ -27,6 +27,6 @@ class Observer::Ticket::Article::CommunicateEmail < ActiveRecord::Observer
return true if type.name != 'email' return true if type.name != 'email'
# send background job # send background job
Delayed::Job.enqueue(Observer::Ticket::Article::CommunicateEmail::BackgroundJob.new(record.id)) TicketArticleCommunicateEmailJob.perform_later(record.id)
end end
end end

View file

@ -2,6 +2,7 @@ require 'test_helper'
class EmailDeliverTest < ActiveSupport::TestCase class EmailDeliverTest < ActiveSupport::TestCase
test 'basic check' do test 'basic check' do
travel_to DateTime.current
if ENV['MAIL_SERVER'].blank? if ENV['MAIL_SERVER'].blank?
raise "Need MAIL_SERVER as ENV variable like export MAIL_SERVER='mx.example.com'" raise "Need MAIL_SERVER as ENV variable like export MAIL_SERVER='mx.example.com'"
@ -81,8 +82,7 @@ class EmailDeliverTest < ActiveSupport::TestCase
assert_nil(article1.preferences['delivery_status_date']) assert_nil(article1.preferences['delivery_status_date'])
assert_nil(article1.preferences['delivery_status_message']) assert_nil(article1.preferences['delivery_status_message'])
result = Observer::Ticket::Article::CommunicateEmail::BackgroundJob.new(article1.id) TicketArticleCommunicateEmailJob.new.perform(article1.id)
assert(result.perform)
article1_lookup = Ticket::Article.find(article1.id) article1_lookup = Ticket::Article.find(article1.id)
assert_equal(1, article1_lookup.preferences['delivery_retry']) assert_equal(1, article1_lookup.preferences['delivery_retry'])
@ -115,8 +115,7 @@ class EmailDeliverTest < ActiveSupport::TestCase
}, },
) )
assert_raises(RuntimeError) do assert_raises(RuntimeError) do
result = Observer::Ticket::Article::CommunicateEmail::BackgroundJob.new(article1.id) TicketArticleCommunicateEmailJob.new.perform(article1.id)
assert_not(result.perform)
end end
article1_lookup = Ticket::Article.find(article1.id) article1_lookup = Ticket::Article.find(article1.id)
assert_equal(2, article1_lookup.preferences['delivery_retry']) assert_equal(2, article1_lookup.preferences['delivery_retry'])
@ -149,8 +148,7 @@ class EmailDeliverTest < ActiveSupport::TestCase
}, },
) )
result = Observer::Ticket::Article::CommunicateEmail::BackgroundJob.new(article1.id) TicketArticleCommunicateEmailJob.new.perform(article1.id)
assert(result.perform)
article1_lookup = Ticket::Article.find(article1.id) article1_lookup = Ticket::Article.find(article1.id)
assert_equal(3, article1_lookup.preferences['delivery_retry']) assert_equal(3, article1_lookup.preferences['delivery_retry'])
assert_equal('success', article1_lookup.preferences['delivery_status']) assert_equal('success', article1_lookup.preferences['delivery_status'])
@ -205,9 +203,9 @@ class EmailDeliverTest < ActiveSupport::TestCase
ticket1.state = Ticket::State.find_by(name: 'closed') ticket1.state = Ticket::State.find_by(name: 'closed')
ticket1.save ticket1.save
assert_raises(RuntimeError) do assert(Delayed::Job.where(attempts: 1).none?)
Scheduler.worker(true) Scheduler.worker(true)
end assert(Delayed::Job.where(attempts: 1).exists?)
ticket1.reload ticket1.reload
article2_lookup = Ticket::Article.find(article2.id) article2_lookup = Ticket::Article.find(article2.id)
@ -228,10 +226,10 @@ class EmailDeliverTest < ActiveSupport::TestCase
assert(article2_lookup.preferences['delivery_status_message']) assert(article2_lookup.preferences['delivery_status_message'])
assert_equal('closed', ticket1.state.name) assert_equal('closed', ticket1.state.name)
sleep 6 travel 26.seconds
assert_raises(RuntimeError) do assert(Delayed::Job.where(attempts: 2).none?)
Scheduler.worker(true) Scheduler.worker(true)
end assert(Delayed::Job.where(attempts: 2).exists?)
ticket1.reload ticket1.reload
article2_lookup = Ticket::Article.find(article2.id) article2_lookup = Ticket::Article.find(article2.id)
@ -253,10 +251,10 @@ class EmailDeliverTest < ActiveSupport::TestCase
assert(article2_lookup.preferences['delivery_status_message']) assert(article2_lookup.preferences['delivery_status_message'])
assert_equal('closed', ticket1.state.name) assert_equal('closed', ticket1.state.name)
sleep 11 travel 51.seconds
assert_raises(RuntimeError) do assert(Delayed::Job.where(attempts: 3).none?)
Scheduler.worker(true) Scheduler.worker(true)
end assert(Delayed::Job.where(attempts: 3).exists?)
ticket1.reload ticket1.reload
article2_lookup = Ticket::Article.find(article2.id) article2_lookup = Ticket::Article.find(article2.id)
@ -267,10 +265,12 @@ class EmailDeliverTest < ActiveSupport::TestCase
assert(article2_lookup.preferences['delivery_status_message']) assert(article2_lookup.preferences['delivery_status_message'])
assert_equal('closed', ticket1.state.name) assert_equal('closed', ticket1.state.name)
sleep 16 travel 76.seconds
assert(Delayed::Job.where(attempts: 4).none?)
assert_raises(RuntimeError) do assert_raises(RuntimeError) do
Scheduler.worker(true) Scheduler.worker(true)
end end
assert(Delayed::Job.where(attempts: 4).exists?)
ticket1.reload ticket1.reload
article2_lookup = Ticket::Article.find(article2.id) article2_lookup = Ticket::Article.find(article2.id)