Skip to content

Instantly share code, notes, and snippets.

@frank-who
Created July 19, 2018 09:44
Show Gist options
  • Save frank-who/cedc9c81559b67944a1dced889516d91 to your computer and use it in GitHub Desktop.
Save frank-who/cedc9c81559b67944a1dced889516d91 to your computer and use it in GitHub Desktop.
Basic Sidekiq config
# config/application.rb
# ...
module FooApp
class Application < Rails::Application
# ...
config.active_job.queue_adapter = :sidekiq
# ...
end
end
# app/jobs/application_job.rb
class ApplicationJob < ActiveJob::Base
end
# config/initializers/sidekiq.rb
require 'sidekiq'
Sidekiq.configure_server do |config|
config.redis = { url: "redis://#{ENV.fetch('DATA_REDIS_HOST') { 'localhost' }}:6379/1" }
end
Sidekiq.configure_client do |config|
config.redis = { url: "redis://#{ENV.fetch('DATA_REDIS_HOST') { 'localhost' }}:6379/1" }
end
if Rails.env.development?
require 'sidekiq/testing'
Sidekiq::Testing.inline!
end
# config/sidekiq.yml
:concurrency: <%= ENV.fetch('SIDEKIQ_CONNECTIONS') { 25 } %>
:queues:
- [critical, 2]
- mailers
- default
# app/jobs/test_job.rb
class TestJob < ApplicationJob
queue_as :critical
def perform(user_id:)
user = User.find(user_id)
user.do_something if user
end
end

Starting Sidekiq

bundle exec sidekiq -C config/sidekiq.yml

Perform Job Immediately

TestJob.perform_now(user_id: current_user.id)

Enqueue Jobs

TestJob.perform_later(user_id: current_user.id)

Enqueue a Job to Run 1 Week From Now

TestJob.set(wait: 1.week).perform_later(user_id: current_user.id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment