Prerequisites:
You need redis (memory data store) for sidekiq to save your background jobs init.
Redis Installation(ubuntu):
Run below commad on ubuntu terminal to insatll redis-server on local machine or on aws server
sudo apt update
sudo apt install redis-server
you can start redis server using the below command on terminal
redis-server
here is how it will look like when you start reids server
you can open redis console using the below command but you need to start redis server first
redis-cli
here is redis commad line
Installation:
Step 1:
run the below command on terminal to add sidekiq to your gem file
bundle add sidekiq
or add sidekiq gem in gem file and then run “rails bundle” command
# Background processing
gem 'sidekiq', "~> 6.4"
Step 2:
add queue adapter in config/application.rb
config.active_job.queue_adapter = :sidekiq
Step 3:
create sidekiq.rb file in config/initializers/sidekiq.rb and add redis server port number init
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' }
end
Step 4:
create sidekiq.yml file in config/sidekiq.yml to tell about the concurrency and queues
:logfile: ./log/sidekiq.log
:concurrency: 2
staging:
:concurrency: 2
develop:
:concurrency: 2
:queues:
- critical
- default
- mailers
- low
Step 5:
here is how you can check if sidekiq is successfully configure or not by running the below command
bundle exec sidekiq
Step 6:
Now you need to create active job using the below command
rails generate job download_and_attach_receipt
it will create “download_and_attach_receipt.rb” file in app/jobs/download_and_attach_receipt_job.rb
class DownloadAndAttachReceiptJob < ApplicationJob
queue_as :default
def perform(*guests)
# Do something later
end
end
in my case to download and save pdf to active storage :receipt object
class DownloadAndAttachReceiptJob < ApplicationJob
queue_as :default
def perform(charge_id, stripe_receipt_url)
receipt_url = "#{stripe_receipt_url.split('?').first}/pdf"
pdf_content = URI.open(receipt_url).read
temp_file = Tempfile.new(['receipt', '.pdf'])
temp_file.binmode
temp_file.write(pdf_content)
temp_file.rewind
payment = Payment.find_by(stripe_charge_id: charge_id)
payment.receipt.attach(io: temp_file, filename: "receipt-#{payment.created_at.strftime("%d %b %Y")}.pdf")
end
end
Step 7:
here is how you can call the job from rails console. you can use perform_now or perform_later
DownloadAndAttachReceiptJob.perform_now("ch_3OhpngHZwOGN0GX70mgLNj60", "https://pay.stripe.com/receipts/invoices/CAcaFwoVYWNjdF8xT0NjdXBIWndPR04wR1g3KPeuqK4GMgaqwpsIwmE6LBZLF6SYaHlSBs9iuNaIW
wcXkSKv21_zDC1WF25rER8HXiEk-cHeECNQbi?s=ap")
you can also see your running jobs in sidekiq server terminal
you can close it using “ctrl+c”
if you want to test the job and you need ruby terminal inside the job you can add it like using the “binding.irb”
and you can exit it using the “exit” command it will take you back to your rails console.
Sidekiq dashboard:
add below gem in gemfile
gem 'sidekiq-scheduler'
update the route file like this by adding the below line init
require ‘sidekiq/web’
mount Sidekiq::Web => ‘/sidekiq’
require 'sidekiq/web'
Rails.application.routes.draw do
devise_for :users
root "users#index"
resources :users, only: [:index, :edit, :update]
mount Sidekiq::Web => '/sidekiq'
end
after this you can access the sidekiq dashboard on web using the route
http://localhost:3000/sidekiq/
here is how you can restrict the user to access this sidekiq dashboard in your route file
authenticate :user, ->(user) { user.admin? } do
mount Sidekiq::Web => '/sidekiq'
end