Sidekiq allows Rails to launch any task in the background. Let's see how, from zero to production.
- Ruby version
ruby -v
ruby 3.1.1p18 // at least version 3
- npm version
npm -v
8.12.1 // at least version 7.1
- Yarn version
yarn -v
1.22.15 // at least 1.22.10
- PostgreSQL version
psql --version
psql (PostgreSQL) 14.2
-
Redis
Run to determine if redis is running
redis-cli ping
- Foreman
foreman -v
0.87.2
mkdir sidekiqrails && cd sidekiqrails
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '7.0.1'" >> Gemfile
bundle install
bundle exec rails new . --force -d=postgresql --minimal
- Create a default controller
echo "class WelcomeController < ApplicationController" > app/controllers/welcome_controller.rb
echo "end" >> app/controllers/welcome_controller.rb
- Create a default route
echo "Rails.application.routes.draw do" > config/routes.rb
echo ' get "welcome/index"' >> config/routes.rb
echo ' root to: "welcome#index"' >> config/routes.rb
echo 'end' >> config/routes.rb
- Create a default view
mkdir app/views/welcome
echo '<h1>This is h1 title</h1>' > app/views/welcome/index.html.erb
- Create database and schema.rb
bin/rails db:create
bin/rails db:migrate
- Inside config/application.rb
require_relative "boot"
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie" # <== Uncomment
# ... everything else remains the same
- Create a jobs directory inside app
mkdir app/jobs && cd app/jobs
- Create a application_job.rb file, then paste this code
# inside app/jobs/application_job.rb
class ApplicationJob < ActiveJob::Base
end
- Open your Gemfile, and add at the very bottom
gem "redis"
gem "sidekiq"
- Run in your terminal
bundle install
- Then add the following line inside config/application.rb
# ...
class Application < Rails::Application
config.active_job.queue_adapter = :sidekiq
# ...
- Create a new file under app/jobs/example_job.rb
class ExampleJob < ApplicationJob
queue_as :default
def perform(*args)
# Simulates a long, time-consuming task
sleep 5
# Will display current time, milliseconds included
p "hello from ExampleJob #{Time.now().strftime('%F - %H:%M:%S.%L')}"
end
end
© 2022 Don Forrest Usbal (Don-PhDev)
Licensed under the MIT License