Class: Klenod::Build::AssetGenerationQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/klenod/build/asset_generation_queue.rb

Constant Summary collapse

DEFAULT_CONCURRENCY =
[Etc.nprocessors / 2, 2].max
DEFAULT_DOWNLOAD_CONCURRENCY =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(concurrency: DEFAULT_CONCURRENCY, download_concurrency: DEFAULT_DOWNLOAD_CONCURRENCY) ⇒ AssetGenerationQueue

Returns a new instance of AssetGenerationQueue.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/klenod/build/asset_generation_queue.rb', line 15

def initialize(concurrency: DEFAULT_CONCURRENCY, download_concurrency: DEFAULT_DOWNLOAD_CONCURRENCY)
  @concurrency = Integer(concurrency)
  @download_concurrency = Integer(download_concurrency)
  raise ArgumentError, "concurrency must be positive" unless @concurrency.positive?
  raise ArgumentError, "download_concurrency must be positive" unless @download_concurrency.positive?

  @semaphores = {
    cpu: Async::Semaphore.new(@concurrency),
    io: Async::Semaphore.new(@download_concurrency)
  }
end

Instance Attribute Details

#concurrencyObject (readonly)

Returns the value of attribute concurrency.



13
14
15
# File 'lib/klenod/build/asset_generation_queue.rb', line 13

def concurrency
  @concurrency
end

#download_concurrencyObject (readonly)

Returns the value of attribute download_concurrency.



13
14
15
# File 'lib/klenod/build/asset_generation_queue.rb', line 13

def download_concurrency
  @download_concurrency
end

Instance Method Details

#run(kind: :cpu, &block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/klenod/build/asset_generation_queue.rb', line 27

def run(kind: :cpu, &block)
  semaphore = semaphore_for(kind)
  task = current_async_task
  return semaphore.acquire { block.call } unless task

  semaphore.async(parent: task) { block.call }
end