Files
leadchat/app/jobs/delete_object_job.rb
Muhsin Keloth 20fa5eeaa5 fix: Prevent SLA deletion timeouts by moving to async job (#12944)
This PR fixes the HTTP 500 timeout errors occurring when deleting SLA
policies that have large volumes of historical data.
The fix moves the deletion workflow to asynchronous background
processing using the existing `DeleteObjectJob`.
By offloading heavy cascaded deletions (applied SLAs, SLA events,
conversation nullifications) from the request cycle, the API can now
return immediately while the cleanup continues in the background
avoiding the `Rack::Timeout::RequestTimeoutException`. This ensures that
SLA policies can be deleted reliably, regardless of data size.


### Problem
Deleting an SLA policy via `DELETE
/api/v1/accounts/{account_id}/sla_policies/{id}` fails consistently with
`Rack::Timeout::RequestTimeoutException (15s)` for policies with large
amounts of related data.

Because the current implementation performs all dependent deletions
**synchronously**, Rails processes:

- `has_many :applied_slas, dependent: :destroy` (thousands)
- Each `AppliedSla#destroy` → triggers destruction of many `SlaEvent`
records
- `has_many :conversations, dependent: :nullify` (thousands)

This processing far exceeds the Rack timeout window and consistently
triggers HTTP 500 errors for users.

### Solution

This PR applies the same pattern used successfully in Inbox deletion.

**Move deletion to async background jobs**

- Uses `DeleteObjectJob` for centralized, reliable cleanup.
- Allows the DELETE API call to respond immediately.

**Chunk large datasets**

- Records are processed in **batches of 5,000** to reduce DB load and
avoid job timeouts.
2025-12-10 12:28:47 +05:30

44 lines
1.1 KiB
Ruby

class DeleteObjectJob < ApplicationJob
queue_as :low
BATCH_SIZE = 5_000
def perform(object, user = nil, ip = nil)
# Pre-purge heavy associations for large objects to avoid
# timeouts & race conditions due to destroy_async fan-out.
purge_heavy_associations(object)
object.destroy!
process_post_deletion_tasks(object, user, ip)
end
def process_post_deletion_tasks(object, user, ip); end
private
def heavy_associations
{
Account => %i[conversations contacts inboxes reporting_events],
Inbox => %i[conversations contact_inboxes reporting_events]
}.freeze
end
def purge_heavy_associations(object)
klass = heavy_associations.keys.find { |k| object.is_a?(k) }
return unless klass
heavy_associations[klass].each do |assoc|
next unless object.respond_to?(assoc)
batch_destroy(object.public_send(assoc))
end
end
def batch_destroy(relation)
relation.find_in_batches(batch_size: BATCH_SIZE) do |batch|
batch.each(&:destroy!)
end
end
end
DeleteObjectJob.prepend_mod_with('DeleteObjectJob')