Class: ShopifyCLI::ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify_cli/thread_pool.rb,
lib/shopify_cli/thread_pool/job.rb

Defined Under Namespace

Classes: Job

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool_size: 10) ⇒ ThreadPool

Returns a new instance of ThreadPool.



7
8
9
10
# File 'lib/shopify_cli/thread_pool.rb', line 7

def initialize(pool_size: 10)
  @jobs = Queue.new
  @pool = Array.new(pool_size) { spawn_thread }
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/shopify_cli/thread_pool.rb', line 5

def errors
  @errors
end

Instance Method Details

#schedule(job) ⇒ Object



12
13
14
# File 'lib/shopify_cli/thread_pool.rb', line 12

def schedule(job)
  @jobs << job
end

#shutdownObject



16
17
18
19
20
21
22
23
# File 'lib/shopify_cli/thread_pool.rb', line 16

def shutdown
  @pool.size.times do
    schedule(-> { throw(:stop_thread) })
  end
  @pool.map(&:join)
ensure
  @jobs.close
end