Slow Sidekiq 仅在开发中

Slow Sidekiq in development only

运行 Sidekiq 6.1.0 on Rails 5.0.7.2,Sidekiq 并发设置为 12,Redis server 4.0.14Redis gem 4.2.1.

queueing/executing 个作业之间似乎有一些延迟(秒),我们在 Rails 4.2.9 中没有看到。请注意,此行为仅发生在开发中,生产似乎做得很好。

示例工人:

class SidekiqTestWorker
  include Sidekiq::Worker

  sidekiq_options(
    queue: "default",
  )

  def perform
    puts "Hello from Sidekiq!"
  end
end

运行 1000.times { SidekiqTestWorker.perform_async } 在 Rails 控制台中使用 Rails 4.2.9 执行所有作业大约需要一秒钟,但使用 Rails 5.0.7.2 则需要几分钟完全的。值得一提的是,我们尝试了 运行 相同的 Sidekiq 版本 (5.2.8),只有 Rails 不同的尝试具有相同的结果。

Sidekiq 工作日志中的一个片段显示了该行为(注意时间戳):

16:52:00 sidekiq_worker.1 | Hello from Sidekiq!
16:52:00 sidekiq_worker.1 | Hello from Sidekiq!
16:52:00 sidekiq_worker.1 | Hello from Sidekiq!
16:52:00 sidekiq_worker.1 | Hello from Sidekiq!
16:52:00 sidekiq_worker.1 | Hello from Sidekiq!
16:52:00 sidekiq_worker.1 | Hello from Sidekiq!
16:52:00 sidekiq_worker.1 | Hello from Sidekiq!
16:52:00 sidekiq_worker.1 | Hello from Sidekiq!
16:52:00 sidekiq_worker.1 | Hello from Sidekiq!
16:52:00 sidekiq_worker.1 | Hello from Sidekiq!
16:52:00 sidekiq_worker.1 | Hello from Sidekiq!
16:52:01 sidekiq_worker.1 | Hello from Sidekiq!
16:52:04 sidekiq_worker.1 | Hello from Sidekiq!
16:52:04 sidekiq_worker.1 | Hello from Sidekiq!
16:52:04 sidekiq_worker.1 | Hello from Sidekiq!
16:52:04 sidekiq_worker.1 | Hello from Sidekiq!
16:52:04 sidekiq_worker.1 | Hello from Sidekiq!
16:52:04 sidekiq_worker.1 | Hello from Sidekiq!
16:52:04 sidekiq_worker.1 | Hello from Sidekiq!
16:52:04 sidekiq_worker.1 | Hello from Sidekiq!
16:52:04 sidekiq_worker.1 | Hello from Sidekiq!
16:52:04 sidekiq_worker.1 | Hello from Sidekiq!
16:52:04 sidekiq_worker.1 | Hello from Sidekiq!
16:52:05 sidekiq_worker.1 | Hello from Sidekiq!
16:52:05 sidekiq_worker.1 | Hello from Sidekiq!
16:52:05 sidekiq_worker.1 | Hello from Sidekiq!

有什么办法可以解决这个问题吗?

在 Sidekiq 5.0 发行说明中:

Sidekiq 5.0 contains a reworked job dispatch and execution core to integrate better with the new Rails 5.0 Executor.

Rails 执行器在开发模式下是单线程的,因此它可以热重载作业代码更改。 Sidekiq 一次只能执行一个作业。

解决此问题的唯一方法是在 config/environment/development.rb 中启用预先加载,但这也会禁用代码重新加载。