Commit dc2f3b6

mo khan <mo.khan@gmail.com>
2020-05-15 23:25:12
Shutdown threadpool after scan
1 parent 34ec279
Changed files (5)
lib
spec
integration
lib/spandx/cli/commands/scan.rb
@@ -17,13 +17,15 @@ module Spandx
         end
 
         def execute(output: $stdout)
-          report = ::Spandx::Core::Report.new
-          each_file do |file|
-            each_dependency_from(file) do |dependency|
-              report.add(dependency)
+          Spandx::Core::ThreadPool.open do |pool|
+            report = ::Spandx::Core::Report.new
+            each_file do |file|
+              each_dependency_from(file, pool) do |dependency|
+                report.add(dependency)
+              end
             end
+            output.puts(format(report.to(@options[:format])))
           end
-          output.puts(format(report.to(@options[:format])))
         end
 
         private
@@ -34,11 +36,11 @@ module Spandx
             .each { |file| yield file }
         end
 
-        def each_dependency_from(file)
+        def each_dependency_from(file, pool)
           dependencies = ::Spandx::Core::Parser.for(file).parse(file)
           with_progress(title_for(file), dependencies.size) do |bar|
             ::Spandx::Core::Concurrent
-              .map(dependencies) { |dependency| enhance(dependency) }
+              .map(dependencies, pool: pool) { |dependency| enhance(dependency) }
               .each do |dependency|
               bar.advance(1)
               yield dependency
lib/spandx/core/concurrent.rb
@@ -5,7 +5,7 @@ module Spandx
     class Concurrent
       include Enumerable
 
-      def self.map(items, pool: Spandx.thread_pool, &block)
+      def self.map(items, pool:, &block)
         queue = Queue.new
 
         items.each do |item|
@@ -25,9 +25,7 @@ module Spandx
       end
 
       def each
-        size.times do |_n|
-          yield queue.deq
-        end
+        size.times { yield queue.deq }
       end
 
       def to_enum
lib/spandx/core/thread_pool.rb
@@ -25,6 +25,13 @@ module Spandx
         @pool.map(&:join)
       end
 
+      def self.open
+        pool = new
+        yield pool
+      ensure
+        pool.shutdown
+      end
+
       private
 
       def start_worker_thread
lib/spandx.rb
@@ -41,10 +41,6 @@ module Spandx
       @logger ||= Logger.new('/dev/null')
     end
 
-    def thread_pool
-      @thread_pool ||= ::Spandx::Core::ThreadPool.new
-    end
-
     def git
       @git ||= {
         cache: ::Spandx::Core::Git.new(url: 'https://github.com/spandx/cache.git'),
spec/integration/cli/scan_spec.rb
@@ -28,7 +28,7 @@ RSpec.describe '`spandx scan` command', type: :cli do
 
   it 'executes `spandx scan Gemfile.lock`' do
     gemfile_lock = fixture_file('bundler/Gemfile.lock')
-    output = `spandx scan #{gemfile_lock} --format=json`
+    output = `spandx scan #{gemfile_lock} --format=json --no-show-progress`
     expected_output = <<~OUT
       {
         "version": "1.0",
@@ -48,7 +48,7 @@ RSpec.describe '`spandx scan` command', type: :cli do
 
   it 'executes `spandx scan gems.lock' do
     gemfile_lock = fixture_file('bundler/gems.lock')
-    output = `spandx scan #{gemfile_lock} --format=json`
+    output = `spandx scan #{gemfile_lock} --format=json --no-show-progress`
     expected_output = <<~OUT
       {
         "version": "1.0",
@@ -68,7 +68,7 @@ RSpec.describe '`spandx scan` command', type: :cli do
 
   it 'executes `spandx scan Pipfile.lock`' do
     lockfile = fixture_file('pip/Pipfile.lock')
-    output = `spandx scan #{lockfile} --format=json`
+    output = `spandx scan #{lockfile} --format=json --no-show-progress`
     expected_output = <<~OUT
       {
         "version": "1.0",
@@ -87,13 +87,14 @@ RSpec.describe '`spandx scan` command', type: :cli do
   end
 
   xit 'executes `spandx scan yarnfile.lock`' do
-    output = `spandx scan #{fixture_file('js/yarn.lock')}`
+    lockfile = fixture_file('js/yarn.lock')
+    output = `spandx scan #{lockfile} --no-show-progress`
     expect(output).to eq(fixture_file_content('js/yarn.lock.expected'))
   end
 
   it 'executes `spandx scan composer.lock`' do
     lockfile = fixture_file('composer/composer.lock')
-    output = `spandx scan #{lockfile}`
+    output = `spandx scan #{lockfile} --no-show-progress`
 
     expect(output).to eq(fixture_file_content('composer/composer.lock.expected'))
   end