From 84ae13eb6b2296c778a80c963da8f095defad936 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 23 Oct 2024 16:57:54 +0200 Subject: [PATCH 1/3] fix(helpscout): don't create conversation with an empty attachment --- app/jobs/helpscout_create_conversation_job.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/jobs/helpscout_create_conversation_job.rb b/app/jobs/helpscout_create_conversation_job.rb index f18781d53..85dda42a0 100644 --- a/app/jobs/helpscout_create_conversation_job.rb +++ b/app/jobs/helpscout_create_conversation_job.rb @@ -49,6 +49,7 @@ class HelpscoutCreateConversationJob < ApplicationJob def safe_blob return if !contact_form.piece_jointe.virus_scanner&.safe? + return if contact_form.piece_jointe.byte_size.zero? # HS don't support empty attachment contact_form.piece_jointe end From 4b48ee02cd0e06267a121dea01b0ee635303fb62 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 23 Oct 2024 16:58:15 +0200 Subject: [PATCH 2/3] fix(helpscout): limit retries to a few hours --- app/jobs/helpscout_create_conversation_job.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/jobs/helpscout_create_conversation_job.rb b/app/jobs/helpscout_create_conversation_job.rb index 85dda42a0..b7f59c043 100644 --- a/app/jobs/helpscout_create_conversation_job.rb +++ b/app/jobs/helpscout_create_conversation_job.rb @@ -2,6 +2,9 @@ class HelpscoutCreateConversationJob < ApplicationJob queue_as :critical # user feedback is critical + + def max_attempts = 15 # ~10h + class FileNotScannedYetError < StandardError end From eaa3350b778a583be4be0692948602a0b72f95e7 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 23 Oct 2024 17:05:27 +0200 Subject: [PATCH 3/3] feat(contact): delete contact forms when max attempts has been reached --- app/jobs/helpscout_create_conversation_job.rb | 4 ++++ .../helpscout_create_conversation_job_spec.rb | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/app/jobs/helpscout_create_conversation_job.rb b/app/jobs/helpscout_create_conversation_job.rb index b7f59c043..8239898c7 100644 --- a/app/jobs/helpscout_create_conversation_job.rb +++ b/app/jobs/helpscout_create_conversation_job.rb @@ -25,6 +25,10 @@ class HelpscoutCreateConversationJob < ApplicationJob create_conversation contact_form.delete + rescue StandardError + contact_form.delete if executions >= max_attempts + + raise end private diff --git a/spec/jobs/helpscout_create_conversation_job_spec.rb b/spec/jobs/helpscout_create_conversation_job_spec.rb index 2ed841cb0..d94609731 100644 --- a/spec/jobs/helpscout_create_conversation_job_spec.rb +++ b/spec/jobs/helpscout_create_conversation_job_spec.rb @@ -110,5 +110,21 @@ RSpec.describe HelpscoutCreateConversationJob, type: :job do end end end + + context 'when max attempts are reached' do + before do + allow(api).to receive(:create_conversation).and_raise(StandardError) + allow_any_instance_of(described_class).to receive(:executions).and_return(described_class.new.max_attempts) + end + + it 'deletes the contact form' do + expect { subject }.to raise_error(StandardError) + expect(contact_form).to be_destroyed + end + + it 'does not enqueue the job again' do + expect { subject rescue nil }.not_to have_enqueued_job(described_class) + end + end end end