Last active
March 1, 2017 09:52
-
-
Save bernstein7/8e0ae7b33e50d3d58a6ff393f9f3241f to your computer and use it in GitHub Desktop.
Module that helps to spread collection job to all available workers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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