Skip to content

Commit

Permalink
fix parellelizer off-by-one issue
Browse files Browse the repository at this point in the history
  • Loading branch information
codez committed Apr 16, 2018
1 parent 92e8f96 commit ba4c071
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/services/parallelizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ def initialize(payloads)
end

def run(&block)
workers = (0..thread_count).map do
Thread.new do
process_payloads(&block)
end
if thread_count <= 1
process_payloads(&block)
else
run_parallel(&block)
end
workers.each(&:join)
end

private

def run_parallel(&block)
workers = Array.new(thread_count) do
Thread.new { process_payloads(&block) }
end
workers.each(&:join)
end

def init_queue(payloads)
Queue.new.tap do |q|
payloads.each { |p| q.push(p) }
Expand All @@ -39,7 +45,7 @@ def next_payload
end

def default_thread_count
Rails.application.secrets.parallel_transcodings.to_i || 1
Rails.application.secrets.parallel_transcodings.to_i
end

end

0 comments on commit ba4c071

Please sign in to comment.