class RSpecTracer::RemoteCache::Cache

Public Class Methods

new() click to toggle source
# File lib/rspec_tracer/remote_cache/cache.rb, line 12
def initialize
  @aws = RSpecTracer::RemoteCache::Aws.new
  @repo = RSpecTracer::RemoteCache::Repo.new(@aws)
end

Public Instance Methods

download() click to toggle source
# File lib/rspec_tracer/remote_cache/cache.rb, line 17
def download
  return unless cache_ref?

  @aws.download_file(@cache_sha, 'last_run.json')
  @aws.download_dir(@cache_sha, last_run_id)
rescue StandardError => e
  puts "Error: #{e.message}"
  puts e.backtrace.first(5).join("\n")
end
upload() click to toggle source
# File lib/rspec_tracer/remote_cache/cache.rb, line 27
def upload
  @aws.upload_file(@repo.branch_ref, 'last_run.json')
  @aws.upload_dir(@repo.branch_ref, last_run_id)

  file_name = File.join(RSpecTracer.cache_path, 'branch_refs.json')

  write_branch_refs(file_name)
  @aws.upload_branch_refs(@repo.branch_name, file_name)
rescue StandardError => e
  puts "Error: #{e.message}"
  puts e.backtrace.first(5).join("\n")
end

Private Instance Methods

cache_ref?() click to toggle source
# File lib/rspec_tracer/remote_cache/cache.rb, line 42
def cache_ref?
  cache_validator = RSpecTracer::RemoteCache::Validator.new

  @cache_sha = @repo.cache_refs.each_key.detect do |ref|
    puts "Validating ref #{ref}"

    cache_validator.valid?(ref, @aws.cache_files_list(ref))
  end

  if @cache_sha.nil?
    puts 'Could not find a suitable cache sha to download'

    return false
  end

  true
end
last_run_id() click to toggle source
# File lib/rspec_tracer/remote_cache/cache.rb, line 70
def last_run_id
  file_name = File.join(RSpecTracer.cache_path, 'last_run.json')

  raise CacheError, 'Could not find any local cache to upload' unless File.file?(file_name)

  JSON.parse(File.read(file_name))['run_id']
end
write_branch_refs(file_name) click to toggle source
# File lib/rspec_tracer/remote_cache/cache.rb, line 60
def write_branch_refs(file_name)
  branch_ref_time = `git show --no-patch --format="%ct" #{@repo.branch_ref}`.chomp

  puts "Failed to find object #{@repo.branch_ref} commit timestamp" unless $CHILD_STATUS.success?

  ref_list = @repo.branch_refs.merge(@repo.branch_ref => branch_ref_time.to_i)

  File.write(file_name, JSON.pretty_generate(ref_list))
end