ruby
27 lines · 7 steps
Purging stale exports with an Active Job in Rails
A Rails background job batch-deletes old exports and their attachments, with retry policies and a rake task to enqueue it.
Explained by
highlit
1class PurgeStaleExportsJob < ApplicationJob
2 queue_as :maintenance
3
4 retry_on ActiveRecord::Deadlocked, wait: :polynomially_longer, attempts: 5
5 retry_on Aws::S3::Errors::ServiceError, wait: 30.seconds, attempts: 3
6 discard_on ActiveJob::DeserializationError
7
8 RETENTION = 7.days
9
10 def perform(older_than: RETENTION.ago)
11 Export.completed.where(created_at: ..older_than).in_batches(of: 200) do |batch|
12 batch.each do |export|
13 export.file.purge_later if export.file.attached?
14 end
15
16 deleted = batch.delete_all
17 Rails.logger.info("[PurgeStaleExports] removed #{deleted} exports")
18 end
19 end
20end
21
22namespace :exports do
23 desc "Enqueue cleanup of stale exports (run hourly via cron/whenever)"
24 task cleanup: :environment do
25 PurgeStaleExportsJob.perform_later
26 end
27end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Processing records in batches keeps memory bounded and avoids locking huge row sets at once.
- 2Declarative retry_on and discard_on rules let a job tolerate transient failures without custom rescue logic.
- 3Purging attachments separately from deleting rows prevents orphaned files in your storage backend.
Related explainers
python
from fastapi import Depends, FastAPI, HTTPException from sqlalchemy import create_engine from sqlalchemy.orm import Session, sessionmaker
Wiring SQLAlchemy sessions into FastAPI
dependency-injection
orm
sessions
Intermediate
7 steps
ruby
class EmailValidator < ActiveModel::EachValidator EMAIL_FORMAT = /\A[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}\z/i def validate_each(record, attribute, value)
Writing a custom email validator in Rails
validation
regex
custom validators
Intermediate
7 steps
javascript
const { validationResult, matchedData } = require('express-validator'); function validate(schema) { const runners = schema.map((rule) => rule.run.bind(rule));
A reusable validation middleware in Express
middleware
validation
higher-order-functions
Intermediate
8 steps
php
public function importUsers(array $users): int { $inserted = 0;
Bulk-inserting users with batched PDO upserts
batching
bulk-insert
prepared-statements
Intermediate
6 steps
go
func RecoveryHandler(logger *zap.Logger) gin.HandlerFunc { return gin.CustomRecovery(func(c *gin.Context, recovered any) { var brokenPipe bool if ne, ok := recovered.(*net.OpError); ok {
A panic-recovery middleware in Gin
middleware
panic-recovery
structured-logging
Intermediate
6 steps
ruby
class ConfigFlattener def initialize(config) @config = config end
Flattening nested config into dotted keys
recursion
hashes
tree-traversal
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/purging-stale-exports-with-an-active-job-in-rails-explained-ruby-edda/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.