问题: 假设创建 7 个同样任务,都是 30 秒后执行,但是最终不是全部执行,可能只执行 5 个,不一定。
Debug:
debug 很久都没有结果,请问大家有没有碰到过。
以下是其他信息:
#Gemfile
gem 'sidekiq', '3.4.2', require: ['sidekiq', 'sidekiq/web']
gem 'sidekiq-failures'
gem 'sidetiq'
gem 'redis'
group :development do
gem 'capistrano-sidekiq'
end
group :test do
gem 'rspec-sidekiq'
end
#sidekiq.yml 配置文件
:concurrency: 5
:pidfile: ./tmp/pids/sidekiq.pid
:logfile: ./log/sidekiq.log
:queues:
- default
development:
:verbose: true
:concurrency: 5
staging:
:concurrency: 10
production:
:concurrency: 20
# config/initializers/sidekiq.rb
Sidekiq::Web.use Rack::Session::Cookie, :secret => "SOMETHING SECRET"
Sidekiq::Web.instance_eval { @middleware.reverse! } # Last added, First Run
Sidekiq.configure_server do |config|
config.redis = { url: 'redis://localhost:6379/12' }
end
Sidekiq.configure_client do |config|
config.redis = { url: 'redis://localhost:6379/12' }
end
Sidekiq::Web.use(Rack::Auth::Basic) do |user, password|
[user, password] == ["", ""]
end
#调用代码
def create_qr_codes
logger.info "#{Time.now}, invoke create_qr_codes"
QRCodeWorker.perform_in(30.seconds, object_id)
end
class QRCodeWorker
include Sidekiq::Worker
sidekiq_options :retry => 1
def perform(id)
# do hard work
end
end
解决方法 问题出在了服务器上同时跑了多个 sidekiq 的 instances,但是没有设置 namespace。修改如下:
# config/initializers/sidekiq.rb
# 旧的配置,没有 namespace
Sidekiq.configure_server do |config|
config.redis = { url: 'redis://localhost:6379/12' }
end
Sidekiq.configure_client do |config|
config.redis = { url: 'redis://localhost:6379/12' }
end
# 新的配置,有 namespace
Sidekiq.configure_server do |config|
config.redis = { url: 'redis://localhost:6379/12', namespace: 'your namespace' }
end
Sidekiq.configure_client do |config|
config.redis = { url: 'redis://localhost:6379/12', namespace: 'your namespace' }
end
这样就可以了。吃一堑长一智。