resque-restriction icon indicating copy to clipboard operation
resque-restriction copied to clipboard

When using multiple workers the limits are not accurately observed

Open phunehehe opened this issue 2 years ago • 0 comments

When using resque-restriction with multiple workers, the limits are not accurately observed. I'm not sure if this should be considered a bug or an intended limitation.

It seems like there is no locking mechanism to prevent race condition, so multiple workers may find themselves within the limit and start the jobs, but together they would exceed the limit. https://github.com/flyerhzm/resque-restriction/blob/14f0b40/lib/resque/plugins/restriction.rb#L40-L45

I was able to reliably trigger the problem using this job:

class GuestsCleanupJob < ApplicationJob
  queue_as :default
  self.queue_adapter = :resque

  extend Resque::Plugins::Restriction
  restrict concurrent: 1

  def perform(queued_at)
    started_at = Time.now
    logger.debug "START #{t queued_at} #{t started_at}"
    sleep 5
    logger.debug "END   #{t queued_at} #{t started_at} #{t Time.now}"
  end

  def t(time)
    time.strftime('%M:%S.%2N')
  end
end

Then I start 20 workers:

bundle exec env rake resque:workers QUEUE='*' COUNT='20'

And submit 20 jobs:

20.times { GuestsCleanupJob.perform_later(Time.now) }

Looking at log/development.log I can see lines like this:

[ActiveJob] [GuestsCleanupJob] [16a97cd7-7257-4b99-8c96-5735a3a29dff] START 33:02.68 33:08.00
[ActiveJob] [GuestsCleanupJob] [15462ae1-6890-421c-a45a-bb1c49033e32] START 33:02.68 33:08.00
[ActiveJob] [GuestsCleanupJob] [e20900a5-9d2b-465e-9e2a-6933962f3c9b] START 33:02.68 33:08.00
[ActiveJob] [GuestsCleanupJob] [a12ebba5-8e82-4f3f-8328-816971082fd3] START 33:02.68 33:08.00
[ActiveJob] [GuestsCleanupJob] [31080fe3-0c2d-48d7-8541-c7decf92f116] START 33:02.68 33:08.00

Indicating 5 jobs were started at the same time.

This is in a newly generated application using Rails 7.0.4.3 via rails new blog

phunehehe avatar Mar 29 '23 03:03 phunehehe