diff --git a/.env-example b/.env-example index e6aa060..16a6d9d 100644 --- a/.env-example +++ b/.env-example @@ -4,3 +4,4 @@ WEBLOGIN_SECRET=your-weblogin-secret WEBLOGIN_ID=your-weblogin-id' WEBLOGIN_ON=true CATALOG_SOLR=http://url-to-solr/solr/biblio +MAX_THREADS=5 diff --git a/Gemfile.lock b/Gemfile.lock index 5426f6a..4c6b55c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -17,6 +17,8 @@ GEM byebug (11.1.3) coderay (1.1.3) concurrent-ruby (1.1.10) + concurrent-ruby-ext (1.1.10) + concurrent-ruby (= 1.1.10) crack (0.4.5) rexml diff-lcs (1.5.0) @@ -167,6 +169,8 @@ PLATFORMS DEPENDENCIES byebug + concurrent-ruby + concurrent-ruby-ext faraday faraday-retry net-smtp diff --git a/lib/report_generator.rb b/lib/report_generator.rb index 7a36f5d..e3a27ba 100644 --- a/lib/report_generator.rb +++ b/lib/report_generator.rb @@ -2,22 +2,31 @@ class ReportGenerator def initialize(barcodes: []) @barcodes = barcodes @umich_catalog_items = UmichCatalogItems.for(barcodes: @barcodes) + Logger.new($stdout).info("fetched umich catalog items") end def run queue = Queue.new queue.push(header_row) - @barcodes.each do |barcode| - umich_item = @umich_catalog_items.item_for_barcode(barcode) - next if umich_item.nil? - worldcat_summary = WorldCatSummary.for(umich_item.oclc || []) + barcode_queue = @barcodes.inject(Queue.new, :push) - queue.push( - format_line(umich_item: umich_item, worldcat_summary: worldcat_summary) - ) + threads = Array.new(ENV.fetch(MAX_THREADS)) do + Thread.new do + until barcode_queue.empty? + barcode = barcode_queue.shift + umich_item = @umich_catalog_items.item_for_barcode(barcode) + next if umich_item.nil? + worldcat_summary = WorldCatSummary.for(umich_item.oclc || []) + + queue.push( + format_line(umich_item: umich_item, worldcat_summary: worldcat_summary) + ) + end + end end + threads.each(&:join) print(queue) end