ruby 30 lines · 2 tabs

ActiveJob for queue adapter abstraction

Alex Kumar Jan 2026
2 tabs
class ProcessUploadJob < ApplicationJob
  queue_as :default

  retry_on StandardError, wait: :exponentially_longer, attempts: 5
  discard_on ActiveJob::DeserializationError

  def perform(upload_id)
    upload = Upload.find(upload_id)

    # Process the upload
    processor = UploadProcessor.new(upload)
    processor.process!

    upload.update!(status: 'completed', processed_at: Time.current)
  rescue StandardError => e
    upload.update!(status: 'failed', error_message: e.message)
    raise e
  end
end
2 files · ruby Explain with highlit

ActiveJob provides a unified interface across different queue backends (Sidekiq, Resque, Delayed Job), making it easier to switch adapters or test jobs. I define jobs by inheriting from ApplicationJob and implementing perform. ActiveJob handles serialization, queue routing, and retry logic automatically. The abstraction layer means tests can use the :inline or :test adapter while production uses Sidekiq. I configure queues with priority in application.rb and route jobs to specific queues based on their characteristics. For simple background work, ActiveJob is sufficient. For advanced features like batches, rate limiting, or complex scheduling, I drop down to Sidekiq-specific APIs.


Related snips

ruby
class CommentsController < ApplicationController
  before_action :set_post

  def create
    @comment = @post.comments.build(comment_params)

System test: asserting Turbo Stream responses

rails hotwire turbo
by codesnips 4 tabs
ruby
class Post < ApplicationRecord
  belongs_to :author, class_name: 'User'
  has_many :comments, dependent: :destroy

  scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.current) }
  scope :draft, -> { where(published_at: nil) }

ActiveRecord scopes for reusable query logic

rails activerecord patterns
by Alex Kumar 1 tab
ruby
module Api
  module V1
    class UsersController < BaseController
      def show
        user = User.includes(:profile).find(params[:id])

ETags for conditional requests and caching

rails caching http-caching
by Alex Kumar 1 tab
ruby
class PostsController < ApplicationController
  def index
    @posts = Post.includes(:author)
                 .order(created_at: :desc)
                 .page(params[:page])
                 .per(10)

Turbo Frames: infinite scroll with lazy-loading frame

rails turbo hotwire
by codesnips 4 tabs
ruby
require "csv"

class PeopleCsvStream
  include Enumerable

  HEADERS = %w[id full_name email signed_up_at plan].freeze

Resilient CSV Export as a Streamed Response

rails performance streaming
by codesnips 3 tabs
erb
<form data-controller="query-sync" data-action="change->query-sync#apply">
  <select name="status" class="rounded border p-2">
    <option value="">Any</option>
    <option value="open">Open</option>
    <option value="closed">Closed</option>
  </select>

Filter UI that syncs query params via Stimulus (no front-end router)

rails hotwire stimulus
by Henry Kim 2 tabs

Share this code

Here's the card — post it anywhere.

ActiveJob for queue adapter abstraction — share card
Link copied