Class: Tebako::FinalizedRuntimeCache

Inherits:
Object
  • Object
show all
Defined in:
lib/tebako/finalized_runtime_cache.rb

Overview

Stores verified, application-independent runtime executables by identity.

Constant Summary collapse

SCHEMA_VERSION =
1
MANIFEST =
"manifest.json"
RUNTIME =
"runtime"

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir:, descriptor:, exe_suffix: "") ⇒ FinalizedRuntimeCache

Returns a new instance of FinalizedRuntimeCache.



24
25
26
27
28
# File 'lib/tebako/finalized_runtime_cache.rb', line 24

def initialize(cache_dir:, descriptor:, exe_suffix: "")
  @cache_dir = File.expand_path(cache_dir)
  @descriptor = descriptor
  @exe_suffix = exe_suffix
end

Instance Method Details

#fetch(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tebako/finalized_runtime_cache.rb', line 30

def fetch(&block)
  FileUtils.mkdir_p(@cache_dir)
  path = File.join(@cache_dir, @descriptor.identity)
  started_at = monotonic_time
  with_lock("#{path}.lock") do
    if valid?(path)
      FileUtils.touch(path)
      report("reused", "finalized runtime identity is unchanged", started_at)
      return runtime_path(path)
    end

    publish(path, &block)
    report("rebuilt", "no matching finalized runtime executable exists", started_at)
    runtime_path(path)
  end
end

#import(runtime) ⇒ Object

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tebako/finalized_runtime_cache.rb', line 47

def import(runtime)
  source = File.expand_path(runtime)
  descriptor_source = Tebako::RuntimeDescriptor.path_for(source)
  raise Tebako::Error.new("Finalized runtime does not exist: #{source}", 120) unless File.file?(source)
  unless File.file?(descriptor_source)
    raise Tebako::Error.new("Finalized runtime descriptor is missing: #{descriptor_source}", 120)
  end

  imported_descriptor = Tebako::RuntimeDescriptor.load(descriptor_source)
  unless imported_descriptor.data == @descriptor.data
    raise Tebako::Error.new("Finalized runtime descriptor is inconsistent", 120)
  end

  fetch do |target_base|
    target = "#{target_base}#{@exe_suffix}"
    FileUtils.cp(source, target, preserve: true)
    FileUtils.cp(descriptor_source, Tebako::RuntimeDescriptor.path_for(target), preserve: true)
  end
end