在 rails 中限制 Sidekiq 作业

Throttle Sidekiq jobs in rails

我需要限制我的 Sidekiq 作业,以免出现此故障:

2020-04-23T12:36:24.424Z pid=1 tid=osrlydn8x WARN: ActiveJob::DeserializationError: Error while trying to deserialize arguments: could not obtain a connection from the pool within 5.000 seconds (waited 5.000 seconds); all pooled connections were in use

基本上,我要做的是向 Sidekiq 发送许多 IP 地址,其中每个 IP 都是它自己的工作:

ip_array.each do |ip|
  ScanJob.perform_later(@scan, ip, ports)
end

ScanJob 然后对每个传入的 IP 地址执行它的操作,这可能需要一些时间,有效地消耗 Sidekiq worker 'pool',一旦消耗,任何剩余的作业都会失败,因为所有池连接都在使用中,因为错误状态。

例如,我需要对 50 个地址进行 运行 ScanJob。有没有办法将 Sidekiq 池大小一次限制为 运行 5,并将剩余的 45 个排队。当这 5 个完成后,接下来的 5 运行(剩余 40 个排队作业)等等?

试试这个:bundle exec sidekiq -c 5