Basic familiarity with Redis and having it already installed and running.
Getting started
We will use Rails to initialize the project demo-hello-sidekiq:
# Create a new rails project
$ rails new demo-rails-with-react-frontend -j esbuild
$ cd demo-rails-with-react-frontend
# Create some required files
$ touch Procfile.dev config/initializers/sidekiq.rb bin/dev
# Add permissions to run bin/dev
$ chmod u+x bin/dev
# Add required gems
$ bundler add sidekiq
# Generate a controller route for our demo
$ bin/rails g controller jobs create
create app/controllers/jobs_controller.rb
route get 'jobs/create'
invoke erb
create app/views/jobs
create app/views/jobs/create.html.erb
invoke test_unit
create test/controllers/jobs_controller_test.rb
invoke helper
create app/helpers/jobs_helper.rb
invoke test_unit
# Generate a basic job
$ bin/rails g sidekiq:job hello
create app/sidekiq/hello_job.rb
create test/sidekiq/hello_job_test.rb
With Sidekiq, we have some helpers to help generate our jobs with bin/rails g sidekiq:job <job_name>.
At this stage, we are ready to start updating and editing our code.
Setting up our development environment
Inside of Procfile.dev:
web: bin/rails s
worker: bundle exec sidekiq
Inside of bin/dev:
#!/usr/bin/env bash
if ! gem list --silent --installed foreman
then
echo "Installing foreman..."
gem install foreman
fi
foreman start -f Procfile.dev "$@"
This will enable us to run bin/dev to start both our Rails server and our Sidekiq process.
Updating our routes
Rails.application.routes.draw do
resources :jobs, only: [:create]
end
Updating our configuration file
We are going to turn off our default_protect_from_forgery in development so we can demonstrate creating the job with our CLI tools.
Update our config/application.rb to the following:
require_relative "boot"
require "rails/all"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module DemoHelloSidekiq
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0
# Enable us to send requests without auth token
config.action_controller.default_protect_from_forgery = false if ENV['RAILS_ENV'] == 'development'
end
end
Configuring our Sidekiq initializer file
Let's update our config/initializers/sidekiq.rb file to set up our server configuration. Note that this will only be for our local server URL.
# inside config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
config.redis = { url: ENV.fetch('REDIS_URL', 'redis://localhost:6379/1') }
end
Sidekiq.configure_client do |config|
config.redis = { url: ENV.fetch('REDIS_URL', 'redis://localhost:6379/1') }
end
Writing our first job
Update what is in app/sidekiq/hello_job.rb:
class HelloJob
include Sidekiq::Job
def perform(*_args)
# Do something
p "HelloJob started with args #{_args}"
# Sleep to simulate a time-consuming task
sleep 5
# Will display current time, milliseconds included
p "HelloJob #{Time.now.strftime('%F - %H:%M:%S.%L')}"
end
end
In our logs now we can see the 3 different jobs run at the expected intervals over the period of the 20 seconds that we expected them to be scheduled for.
The expected schedule is something you should keep in mind when writing your tests.
Summary
Today's post demonstrated how to run jobs in a Rails application with Sidekiq.
This was a simple overview to see a first job in action.
Moving forward, you should look into testing, distributed Redis configuration and more complex jobs that are not so contrived for the purpose of demonstration!
Jobs can be great for background processing such as sending emails, compressing images, or performing long-running tasks.