Skip to content

Instantly share code, notes, and snippets.

@paracycle
Forked from ethnt/Gemfile
Last active December 17, 2015 09:59
Show Gist options
  • Save paracycle/5591185 to your computer and use it in GitHub Desktop.
Save paracycle/5591185 to your computer and use it in GitHub Desktop.
How to use Sidekiq with Padrino on Heroku. This config enables you to run sidekiq inside your "web" dyno (See https://coderwall.com/p/fprnhg for details)
# config/boot.rb
# ...
# Load our dependencies
require 'rubygems' unless defined?(Gem)
require 'bundler/setup'
Bundler.require(:default, PADRINO_ENV)
Padrino.before_load do
# Load worker definitions
require File.join(PADRINO_ROOT, 'config', 'workers.rb')
end
# ...
Padrino.after_load do
# Sidekiq should log to our main log file.
Sidekiq.logger = Padrino.logger
# Make sure that all mailers are loaded. Without this I was having problems delivering emails from workers.
Padrino.require_dependencies("#{Padrino.root}/app/mailers/*.rb")
end
# ...
#!/usr/bin/env rackup
# encoding: utf-8
require File.expand_path("../config/boot.rb", __FILE__)
require 'sidekiq/web'
map('/sidekiq') { run Sidekiq::Web }
run Padrino.application
# ...
gem 'sidekiq'
gem 'slim'
gem 'unicorn'
# ...
# workers/hard_worker.rb
class HardWorker
include Sidekiq::Worker
def perform(name, count)
MyApp::App.deliver(:mailer_name, :email_name, name, count)
end
end
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
# See http://manuel.manuelles.nl/blog/2012/11/13/sidekiq-on-heroku-with-redistogo-nano/
:concurrency: 2
# app/controllers/test.rb
YourApp.controller do
get '/test' do
HardWorker.perform_async('bob', 5)
end
end
require 'sidekiq'
worker_processes 3
before_fork do |server, worker|
# Spawn Sidekiq worker inside the current process.
@worker_pid = spawn('bundle exec sidekiq -C ./config/sidekiq.yml -r ./config/boot.rb')
t = Thread.new {
Process.wait(@worker_pid)
puts "Worker died. Bouncing unicorn."
Process.kill 'QUIT', Process.pid
}
# Just in case
t.abort_on_exception = true
end
after_fork do |server, worker|
Sidekiq.configure_client do |config|
config.redis = { :size => 1 } # Limit the client size, see http://manuel.manuelles.nl/blog/2012/11/13/sidekiq-on-heroku-with-redistogo-nano/
end
Sidekiq.configure_server do |config|
config.redis = { :size => 2 } # Limit the server size, see http://manuel.manuelles.nl/blog/2012/11/13/sidekiq-on-heroku-with-redistogo-nano/
end
end
# config/workers.rb
Dir[File.expand_path('../../workers/*.rb', __FILE__)].each{|file|require file}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment