impress/lib/dti_requests.rb
Emi Matchu 1d4771ecc5 Create new DTIRequests.load_many helper, to make parallel requests
Just a wrapper for the barrier/semaphore thing we're doing constantly!

I only applied it in the importer rake tasks for now. There's other
call sites to move over to it, too!
2024-12-16 13:16:03 -08:00

19 lines
416 B
Ruby

require "async"
require "async/barrier"
module DTIRequests
class << self
def load_many(max_at_once: 10)
barrier = Async::Barrier.new
semaphore = Async::Semaphore.new(max_at_once, parent: barrier)
Sync do
block_return_value = yield semaphore
barrier.wait # Load all the subtasks.
block_return_value
ensure
barrier.stop # If any subtasks failed, cancel the rest.
end
end
end
end