Sidekiq + whenever

I found in Sidekiq an awesome way to get rid of my old cron jobs! 🙂

First install, Sidekiq! My Gemfile :

gem 'whenever' #cronjobs
gem 'sidekiq'
gem 'sidekiq-client-cli' #to run sidekiq with whenever
gem 'slim', ">= 1.3.0" #pseudo language
gem 'sinatra', '>= 1.3.0', :require => nil #interface
gem 'sidekiq-middleware' #handle locks

Then configure it! My sidekiq.yml

:concurrency: 2
:logfile: ./log/sidekiq.log
:verbose: true
:queues:
  - [queue_for_critical_stuff, 4]
  - [queue2_for_important_stuff, 3]
  - [queue3_for_other_stuff, 2]
  - [default, 1]

My initializer: config/initializers/sidekiq.rb

Sidekiq.configure_server do |config|
  config.redis = { :url => 'redis://localhost:6379'}
end
Sidekiq.configure_client do |config|
  config.redis = { :url => 'redis://localhost:6379' }
end

Don’t forget to start Sidekiq after deploying your app! With capistrano in my config/deploy.rb

...
require 'sidekiq/capistrano'
set :sidekiq_role, :sidekiq
after "sidekiq:start"
namespace :sidekiq do
  task :start do
    sudo "service sidekiq start"
  end
  task :stop do
    sudo "service sidekiq stop"
  end
end...

Write your worker in workers folder of your Rails app, ex my workers/cache/live_worker.rb

class Cache::LiveWorker
  include Sidekiq::Worker
  sidekiq_options :queue => :queue1, :retry => false, :backtrace => true, unique: :all, manual: true #really useful for unicity!!

  def self.lock(type, id)
    "locks:unique:#{type}-#{id}"
  end

  # Implement method to handle lock removing manually
  def self.unlock!(type, id)
    lock = self.lock(type, id)
    Sidekiq.redis { |conn| conn.del(lock) }
  end

  def perform(type, id)
    #do what you want!!!
    self.class.unlock!(type, id)
  end

Then in your Rails console, execute!

Cache::LiveWorker.new.perform('blabla',1)

Or in your code:

Cache::LiveWorker.perform_async('blabla',1)

Or execute with whenever as a cronjob, my config/schedule.rb:

job_type :sidekiq, "export PATH=your_path:$PATH; cd /var/www/unicorn/current && RAILS_ENV=#{environment} bundle exec sidekiq-client :task :output"
every 3.minutes, :roles => [:app] do
  sidekiq "-q queue1 push Cache::LiveWorker"
end

Isn’t it awesome??!

One thought on “Sidekiq + whenever

  1. Thanks for your short but useful article. However, I have a problem after crontab updated, the task doesn’t work, because the command run under /bin/bash, look like: `/bin/bash -l -c ‘export PATH=/Volumes/DATA/code/rails/aa:$PATH; cd /Volumes/DATA/code/rails/aaa && RAILS_ENV=development bundle exec sidekiq-client -q default push TestWorker’`
    So it said that I’m missing some gems. How to fix this issue?

Leave a comment