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

Thoughts on unique working jobs?

Open jhaber1 opened this issue 10 years ago • 2 comments

Great gem, does exactly what we need, but we discovered for our application that we also need to ensure a job remains unique while also being processed. I added a small helper in our application not to enqueue jobs whose class/payload exists in any of the workers in Resque::Worker.working mainly because I wasn't quite sure where it would go in relation to this gem (if at all). Anyone have any thoughts on this? Would it make sense to include in this gem? Thanks.

jhaber1 avatar Oct 15 '14 13:10 jhaber1

@singjsong can you share that code?

After reading README, my assumption was that this is the default behaviour. imo it'd be good to either add this functionality or update README to make it crystal clear that your job might be executed in parallel by different workers.

lukkry avatar Apr 02 '15 13:04 lukkry

Sure:

  # Checks if any workers are currently working on a given job from a given queue.
  #
  # @param [Symbol] queue
  # @param [Class] klass
  # @param [Array] args
  # @return [Boolean] whether the job is being worked on or not
  def self.already_working?(queue, klass, *args)
    queue_workers = Resque::Worker.working.select { |worker| worker.job['queue'] == queue.to_s }

    workers = queue_workers.select do |worker|
      payload = worker.job.try(:[], 'payload')

      if payload.present?
        payload.try(:[], 'class') == klass.to_s && payload.try(:[] , 'args') == args
      else
        false
      end
    end

    workers.size > 1
  end

Could be refactored a bit, but it does what's needed.

jhaber1 avatar Apr 02 '15 19:04 jhaber1