Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thoughts on unique working jobs? #44

Open
jhaber1 opened this issue Oct 15, 2014 · 2 comments
Open

Thoughts on unique working jobs? #44

jhaber1 opened this issue Oct 15, 2014 · 2 comments

Comments

@jhaber1
Copy link

jhaber1 commented Oct 15, 2014

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.

@lukkry
Copy link

lukkry commented Apr 2, 2015

@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.

@jhaber1
Copy link
Author

jhaber1 commented Apr 2, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants