Sidekiq中的queue.size未实时更新

问题描述 投票:0回答:1

我正在使用sidekiq和ActiveJob。我想平衡队列。所以我用这种方式。

while queue.size < 10
  SomeJob.perform_later(some_args) # This should add one job to the queue right away, but it doesn't, it takes some time for the job to enter the queue.
end

这是一个糟糕的方式失败。这将安排50,60或更多的工作。原因是队列没有直接填充作业,而是需要一些时间让作业进入队列。因此,方法queue.size将返回0几秒钟,然后获得真正的队列大小。

更新:我发现了这个问题。事实证明,我用来安排作业的类是一个配置的类,某些时候的配置是SomeJob.set(wait:wait_time),而wait_time是0.活动作业会将作业放入预定的设置一段时间(在进入队列之前不到一秒左右。这就是为什么queue.size没有反映我期望在队列中的内容。

ruby ruby-on-rails-5 sidekiq
1个回答
-1
投票

发生这种情况是因为queue已经初始化,而且每次作业入队时你都不会重新初始化新的queue对象。它不会像你说的那样“实时更新”(类似于你必须在ActiveRecord对象上调用#reload)

比重新初始化更有效,同样的效果:

size = queue.size
max_queue_size = 10
number_of_jobs_to_perform = max_queue_size - size

number_of_jobs_to_perform = 0 if number_of_jobs_to_perform < 0 

number_of_jobs_to_perform.times do
  SomeJob.perform_later(args)
end

编辑:如果你真的必须,请使用proc,例如Proc.new { queue.size }.times do ...

© www.soinside.com 2019 - 2024. All rights reserved.