Periodic Tasks with sidekiq-cron

Episode #61 by Teacher's Avatar David Kimura

Summary

Using a third-party add-on, sidekiq-cron, we can create periodic tasks to extend the functionality of sidekiq.
rails background processing 4:59

Resources

Summary

# Gemfile
gem 'sidekiq-cron'

# config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
  config.redis = { url: 'redis://localhost:6379/0'  }
  schedule_file = "config/schedule.yml"
  if File.exists?(schedule_file)
    Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
  end
end

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

# config/schedule.yml
generate_random_user:
  cron: "* * * * *"
  class: "GenerateRandomUserJob"
  queue: default

# routes.rb
Rails.application.routes.draw do
  ...
  require 'sidekiq/web'
  require 'sidekiq/cron/web'
  mount Sidekiq::Web => '/sidekiq'
  ...
end