From 117ab2b45f8a0a2dfb051db9b6a37df5759dde2f Mon Sep 17 00:00:00 2001 From: "sentry[bot]" <39604003+sentry[bot]@users.noreply.github.com> Date: Tue, 7 Apr 2026 15:53:25 +0200 Subject: [PATCH] refactor(accounts): Improve destroy_later atomicity and add controller error handling (#1395) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor(accounts): Improve destroy_later atomicity and add controller error handling * Address PR comment --------- Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com> Co-authored-by: Juan José Mata --- app/controllers/accounts_controller.rb | 9 +++++++-- app/jobs/destroy_job.rb | 1 + app/models/account.rb | 6 ++++-- config/locales/views/accounts/en.yml | 1 + 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index af46e970a..9ae5767e5 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -113,8 +113,13 @@ class AccountsController < ApplicationController if @account.linked? redirect_to account_path(@account), alert: t("accounts.destroy.cannot_delete_linked") else - @account.destroy_later - redirect_to accounts_path, notice: t("accounts.destroy.success", type: @account.accountable_type) + begin + @account.destroy_later + redirect_to accounts_path, notice: t("accounts.destroy.success", type: @account.accountable_type) + rescue => e + Rails.logger.error "Failed to schedule account #{@account.id} for deletion: #{e.message}" + redirect_to accounts_path, alert: t("accounts.destroy.failed") + end end end diff --git a/app/jobs/destroy_job.rb b/app/jobs/destroy_job.rb index 692585de6..8b5622423 100644 --- a/app/jobs/destroy_job.rb +++ b/app/jobs/destroy_job.rb @@ -1,5 +1,6 @@ class DestroyJob < ApplicationJob queue_as :low_priority + self.enqueue_after_transaction_commit = :never def perform(model) model.destroy diff --git a/app/models/account.rb b/app/models/account.rb index 79876c1ec..8a31a8d5e 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -311,8 +311,10 @@ class Account < ApplicationRecord end def destroy_later - mark_for_deletion! - DestroyJob.perform_later(self) + transaction do + mark_for_deletion! + DestroyJob.perform_later(self) + end end # Override destroy to handle error recovery for accounts diff --git a/config/locales/views/accounts/en.yml b/config/locales/views/accounts/en.yml index e64f84972..23af6cfab 100644 --- a/config/locales/views/accounts/en.yml +++ b/config/locales/views/accounts/en.yml @@ -24,6 +24,7 @@ en: destroy: success: "%{type} account scheduled for deletion" cannot_delete_linked: "Cannot delete a linked account. Please unlink it first." + failed: "Resource deletion failed. Try again later." empty: empty_message: Add an account either via connection, importing or entering manually. new_account: New account