如何从DelayedJob迁移到Sidekiq

时间:2014-01-20 10:34:15

标签: heroku redis unicorn sidekiq

我的应用程序正在使用DelayedJob,但速度很慢所以我决定切换到Sidekiq。

到目前为止,它还没有与Sidekiq一起进行测试或开发。 (当然,我还没有尝试过使用Sidekiq进行制作。)

从DelayedJob搬到Sidekiq时,我开始更新我的Profile

web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
# worker:  bundle exec rake jobs:work
before_fork do |server, worker|
   @sidekiq_pid ||= spawn("bundle exec sidekiq -c 2")
end

我假设这意味着我不必在bundle exec sidekiq之前输入foreman start,对吧?当我运行我的测试时,这应该启动sidekiq,对吗?

但是在我的测试中,我的应用程序中表达为@instance.delay.method的所有内容(与DelayedJob和Sidekiq一起使用)都无效。

因此,为了尝试处理正在发生的事情,我将以下内容添加到routes.rb

Testivate::Application.routes.draw do
  require 'sidekiq/web'
  mount Sidekiq::Web, at: "/sidekiq"

但是,当我重新开始访问localhost:3000/sidekiq时,我只看到:

No route matches [GET] "/sidekiq"
Routes
Routes match in priority from top to bottom
Helper  HTTP Verb   Path    Controller#Action
Path / Url          
sidekiq_web_path         /sidekiq    Sidekiq::Web

BTW,redis似乎在本地自动运行:

$ redis-cli ping
PONG

我的捆绑gemfile包括:

ruby "2.0.0"
gem "rails", "4.0.2"
gem 'sinatra', require: false
gem 'slim'
gem "sidekiq"
gem "autoscaler"

我没有对unicorn.rb进行任何更改:

# config/unicorn.rb
worker_processes 3
timeout 360
preload_app true

before_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|

  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

1 个答案:

答案 0 :(得分:0)

我自己从DJ搬到了Sidekiq。虽然我的配置中没有看到任何明显的东西,但我会分享我的设置。关于如何配置Sidekiq的一些建议。希望这会有所帮助。

对于initializers / redis.rb,支持local和heroku envs:

module MyRedis
  class << self
      def redis
        @redis ||= Redis.new(url: (ENV['REDISTOGO_URL'] || 'redis://127.0.0.1:6379'))
      end

      ##
      # test for avail
      #         
      def sidekiq?
       begin
        redis_info = Sidekiq.redis { |conn| conn.info }
        true
       rescue
        false
       end
      end
   end
end

对于initilizers / sidekiq.rb(考虑参数化size

Sidekiq.configure_server do |config|
  config.redis = { :url => MyRedis.redis[:url], :namespace => 'sidekiq', :size => 4 }
end

Sidekiq.configure_client do |config|
  config.redis = { :url => MyRedis.redis[:url], :namespace => 'sidekiq', :size => 1 }
end

典型的config / sidekiq.yml:

:verbose: false
:concurrency:  25
:queues:
 - [often, 7]
 - [default, 5]
 - [seldom, 3]

然后在你的Procfile中,sidekiq.yml:

web: bundle exec unicorn -p $PORT -E $RAILS_ENV -c ./config/unicorn.rb
worker: bundle exec sidekiq -C config/sidekiq.yml -e $RAILS_ENV

最后,我对config / unicorn.rb所做的唯一更改是在分叉时处理redis连接:

 ...
 before_fork do |server, worker|

   if defined?(MyRedis.redis)
     MyRedis.redis.quit
     Rails.logger.info('Disconnected from Redis')
   end 


 ...
 after_fork do |server, worker|

   if defined?(MyRedis.redis)
     MyRedis.redis.client.reconnect
     Rails.logger.info('Connected to Redis')
   end