The personal finance app for everyone (by everyone)
Ruby
refactor(accounts): Improve destroy_later atomicity and add controller error handling (#1395)
sentry[bot]117ab2b45f8a
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rbindex af46e970ae..9ae5767e53 100644--- a/app/controllers/accounts_controller.rb+++ b/app/controllers/accounts_controller.rb@@ -113,8 +113,13 @@ class AccountsController < ApplicationControllerif @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")+ endendenddiff --git a/app/jobs/destroy_job.rb b/app/jobs/destroy_job.rbindex 692585de64..8b5622423b 100644--- a/app/jobs/destroy_job.rb+++ b/app/jobs/destroy_job.rb@@ -1,5 +1,6 @@class DestroyJob < ApplicationJobqueue_as :low_priority+ self.enqueue_after_transaction_commit = :neverdef perform(model)model.destroydiff --git a/app/models/account.rb b/app/models/account.rbindex 79876c1ece..8a31a8d5e5 100644--- a/app/models/account.rb+++ b/app/models/account.rb@@ -311,8 +311,10 @@ class Account < ApplicationRecordenddef destroy_later- mark_for_deletion!- DestroyJob.perform_later(self)+ transaction do+ mark_for_deletion!+ DestroyJob.perform_later(self)+ endend# Override destroy to handle error recovery for accountsdiff --git a/config/locales/views/accounts/en.yml b/config/locales/views/accounts/en.ymlindex e64f849728..23af6cfabc 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