Here's a simplified project for setting up Active Jobs in Rails with Sidekiq and Redis, using sample models UserDetail and Employee.
rails new active_jobs_demo
cd active_jobs_demo
code .
Add the following to your Gemfile:
gem 'sidekiq'
gem 'sidekiq-scheduler'
gem 'redis'
bundle install
bundle binstubs sidekiq
rails generate model UserDetail name:string profile:text
rails generate model Employee first_name:string last_name:string email:string notes:text
validates :name, presence: true, length: { minimum: 5, maximum: 15 }
validates :profile, presence: true
validates :first_name, :last_name, :email, presence: true
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
rails db:migrate
Update db/seeds.rb with UserDetail and Employee records.
Important: Do not run rails db:seed yet.
rails generate job update_employees
Edit app/jobs/update_employees_job.rb:
class UpdateEmployeesJob < ApplicationJob
queue_as :default
def perform(employee_id, attributes)
employee = Employee.find(employee_id)
employee.update(attributes)
end
endIn app/models/employee.rb, update class:
class Employee < ApplicationRecord
validates :first_name, :last_name, presence: true
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
after_create :update_employee
after_update :update_employee
def update_employee
UpdateEmployeesJob.set(wait: 1.minutes).perform_later(self.id, {notes: "create notes from job"}) if saved_changes.key?(:created_at)
UpdateEmployeesJob.set(queue: 'high_priority').perform_later(self.id, {notes: "update notes from job"}) if saved_changes.key?(:updated_at)
end
endStart Redis:
redis-server
Start Sidekiq:
bundle exec sidekiq
rails db:seed
Sidekiq will process background jobs as data is seeded.
Create a scheduled task with Sidekiq Scheduler:
config/sidekiq.yml
:queues:
- critical
- default
- mailers
tasks: &tasks
UpdateUsersJob:
cron: "*/5 * * * *" # Every 5 minutes
queue: default
description: "Update User profile"
:scheduler:
:schedule:
<<: *tasksconfig/initializers/redis.rb
$redis = Redis.new(url: "redis://localhost:6379/0")
Sidekiq.configure_server do |config|
config.redis = { url: "redis://localhost:6379/0" }
end
Sidekiq.configure_client do |config|
config.redis = { url: "redis://localhost:6379/0" }
endconfig/initializers/sidekiq.rb
require "sidekiq-scheduler"
config/routes.rb
require "sidekiq/web"
Rails.application.routes.draw do
mount Sidekiq::Web => '/sidekiq'
...
endbin/importmap pin local-time
Then in config/importmap.rb:
pin "local-time"
And in app/javascript/application.js:
import LocalTime from "local-time"
LocalTime.start()
app/jobs/update_users_job.rb
class UpdateUsersJob < ApplicationJob
queue_as :default
def perform
timestamp = Time.now.strftime('%B %e, %Y %l:%M %P')
UserDetail.update_all(profile: "profile from job #{timestamp}")
end
endThis job updates all UserDetail records every 5 minutes via Sidekiq Scheduler.
✅ Final Setup Summary
Models: UserDetail & Employee
Redis & Sidekiq: Fully configured
Job 1: UpdateEmployeesJob, triggered automatically on create/update
Job 2: UpdateUsersJob, runs every 5 minutes
🧹 How to Zero Sidekiq
To reset Sidekiq and clear job queues:
Option A - Clear All Redis Data
redis-cli flushall
Option B - Clear Only Current DB
redis-cli flushdb
Use this if you're not using the default Redis DB or sharing Redis across environments.
All done! We're here to bring you the best — every time! 🎉👏