background-jobs

ruby
class DailySignupRollup < ApplicationRecord
  validates :day, presence: true, uniqueness: true
  validates :total, :verified, numericality: { greater_than_or_equal_to: 0 }

  scope :for_range, ->(from, to) do
    where(day: from.to_date..to.to_date).order(:day)

Nightly Signup Rollups in Rails with a Scheduled Job and Upsert

rails postgres background-jobs
by codesnips 3 tabs
yaml
cleanup_expired_sessions:
  cron: '0 2 * * *'  # Daily at 2 AM
  class: CleanupExpiredSessionsWorker
  queue: low
  description: Remove expired sessions from Redis

Background job scheduling with sidekiq-scheduler

rails sidekiq background-jobs
by Alex Kumar 2 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Polymorphic Media Attachments in Laravel With a Queued Image Resize Job

laravel polymorphism eloquent
by codesnips 4 tabs
ruby
class LeaderboardCache
  TOP_KEY = "leaderboard:top".freeze
  STATS_KEY = "leaderboard:stats".freeze

  def top_players
    Rails.cache.fetch(TOP_KEY, expires_in: 5.minutes, race_condition_ttl: 15.seconds) do

Cache Stampede Protection with race_condition_ttl

rails caching performance
by codesnips 3 tabs
ruby
class CreateWebhookEvents < ActiveRecord::Migration[7.1]
  def change
    create_table :webhook_events do |t|
      t.string :event_id, null: false
      t.string :source, null: false, default: "stripe"
      t.string :event_type, null: false

Idempotent Stripe Webhook Processing with a Unique Event Key in Rails

rails postgres webhooks
by codesnips 4 tabs
ruby
class CreateOutboxEvents < ActiveRecord::Migration[7.1]
  def change
    create_table :outbox_events do |t|
      t.string :event_type, null: false
      t.string :aggregate_type, null: false
      t.string :aggregate_id, null: false

Transactional Outbox for Reliable Event Publishing

rails background-jobs reliability
by codesnips 4 tabs
ruby
class SyncContactJob < ApplicationJob
  queue_as :external

  BACKOFF = ->(executions) do
    (2**executions) + rand(0.0..1.0) # exponential + jitter, in seconds
  end

Exponential Backoff with Jitter for Flaky External API Calls in ActiveJob

rails activejob background-jobs
by codesnips 3 tabs
ruby
class User < ApplicationRecord
  has_one_attached :avatar do |attachable|
    attachable.variant :thumb,
      resize_to_limit: [256, 256],
      convert: :webp,
      saver: { quality: 80 }

Attach and Resize an Avatar with an Active Storage Variant in Rails

rails active-storage image-processing
by codesnips 3 tabs
ruby
class ProcessPaymentWorker
  include Sidekiq::Worker

  sidekiq_options queue: :critical, retry: 10

  sidekiq_retry_in do |count, exception|

Background job retry strategies

rails sidekiq background-jobs
by Alex Kumar 1 tab
ruby
class ImportResult
  RowError = Struct.new(:line, :messages)

  attr_reader :imported, :row_errors

  def initialize

Streaming CSV Product Import With Per-Row Validation and Error Reporting in Rails

rails csv service-object
by codesnips 3 tabs
ruby
class LastSeenTracker
  THROTTLE = 5.minutes
  PENDING_KEY = "pending:last_seen".freeze

  class << self
    def touch(user_id, at: Time.current)

Database “Last Seen” without Hot Row Updates

rails performance redis
by codesnips 3 tabs
python
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver

from .models import EmailOutbox

Sending Welcome Emails via a Django post_save Signal and Outbox Worker

django signals email
by codesnips 4 tabs