Skip to content

Instantly share code, notes, and snippets.

@bernstein7
Last active March 1, 2017 09:52
Show Gist options
  • Save bernstein7/8e0ae7b33e50d3d58a6ff393f9f3241f to your computer and use it in GitHub Desktop.
Save bernstein7/8e0ae7b33e50d3d58a6ff393f9f3241f to your computer and use it in GitHub Desktop.
Module that helps to spread collection job to all available workers
# frozen_string_literal: true
module WorkersBalancer
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def balanced_perform_later(collection_ids)
collection_ids.in_groups(workers_count).each do |ids_group|
perform_later(ids_group)
end
end
def workers_count
Resque.workers.any? ? Resque.workers.count : 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment