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