😈 Sidekiq

Sidekiq


Simple, efficient background processing for Ruby. 
Sidekiq uses threads to handle many jobs at the same time in the same process. 
It does not require Rails but will integrate tightly with Rails to make background processing dead simple.
Sidekiq…
001.2 first job
# work.rb

reiqure ‘sidekiq'

Sidekiq.configure_client do |config|
  config.redis = {db: 1}
end

Sidekiq.configure_servier do |config|
  config.redis = {db: 1}
end

class OurWorker
  incldue Sidekiq::Worker
  
  def perform(complexity)
    case complexity
    when “super_hand"
      sleep 20
      puts “Really took quite a bit of effort"
    end
    when “ard"
      sleep 10
      puts “That was a bit of work"
    else 
     sleep 1
     puts “That wasn’t a lot of effort"
  end
end
# work.rb…
# Gemfile

gem “sidekiq"

bundle
# Gemfile…
bundle exec sidekiq -r ./worker.rb
bundle exec irb -r ./worker.rb

OurWorker.perform_async(“easy”)
OurWorker.perform_async(“hanrd”)
OurWorker.perform_in(5, “easy”)
bundle exec sidekiq -r ./worker.rb…
001.4 Error handling
#  work.rb
reiqure ‘sidekiq'

Sidekiq.configure_client do |config|
  config.redis = {db: 1}
end

Sidekiq.configure_servier do |config|
  config.redis = {db: 1}
end

class OurWorker
  incldue Sidekiq::Worker
  sidekiq_options retry: 0
  
  def perform(complexity)
    case complexity
    when “super_hand"
      puts “charging a credit card…“
      raise “Woops stuff got bad"
      puts “Really took quite a bit of effort"
    end
    when “ard"
      sleep 10
      puts “That was a bit of work"
    else 
     sleep 1
     puts “That wasn’t a lot of effort"
  end
end
#  work.rb…
# Gemfile

gem “sidekiq"
gem “rack"
gem “sinatra"

buhndle
# Gemfile…
bundle exec sidekiq -r ./worker.rb
bundle exec irb -r ./worker.rb

OurWorker.perform_async(“easy”)
OurWorker.perform_async(“hanrd”)
OurWorker.perform_in(5, “easy”)
bundle exec sidekiq -r ./worker.rb…
# config.ru

reiqure ‘sidekiq'
Sidekiq.configure_client do |config|
  config.redis = { db: 1 }
end

reiqure ‘sidekiq/web'
run Sidekiq::Web

rackup
# config.ru…
Viewer does not support full SVG 1.1

Zane Zheng
Zane Zheng
Full Stack (Ruby on Rails) / Devops

An Full Stack / Devops with JS, Ruby, Linux, JAMStack, MicroServices.

Related