Class: S3File

Inherits:
Object show all
Defined in:
lib/cosmos/utilities/s3_file_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s3_path, size = 0, priority = 0) ⇒ S3File

Returns a new instance of S3File.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 32

def initialize(s3_path, size = 0, priority = 0)
  @rubys3_client = Aws::S3::Client.new
  begin
    @rubys3_client.head_bucket(bucket: 'logs')
  rescue Aws::S3::Errors::NotFound
    @rubys3_client.create_bucket(bucket: 'logs')
  end

  @s3_path = s3_path
  @local_path = nil
  @reservation_count = 0
  @size = size
  @priority = priority
  @error = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



29
30
31
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 29

def error
  @error
end

#local_pathObject (readonly)

Returns the value of attribute local_path.



26
27
28
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 26

def local_path
  @local_path
end

#priorityObject

Returns the value of attribute priority.



30
31
32
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 30

def priority
  @priority
end

#reservation_countObject (readonly)

Returns the value of attribute reservation_count.



27
28
29
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 27

def reservation_count
  @reservation_count
end

#s3_pathObject (readonly)

Returns the value of attribute s3_path.



25
26
27
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 25

def s3_path
  @s3_path
end

#sizeObject (readonly)

Returns the value of attribute size.



28
29
30
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 28

def size
  @size
end

Instance Method Details

#deleteObject

private



77
78
79
80
81
82
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 77

def delete
  if @local_path and File.exist?(local_path)
    File.delete(@local_path)
    @local_path = nil
  end
end

#reserveObject



62
63
64
65
66
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 62

def reserve
  @mutex.synchronize do
    @reservation_count += 1
  end
end

#retrieveObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 49

def retrieve
  local_path = "#{S3FileCache.instance.cache_dir}/#{File.basename(@s3_path)}"
  Cosmos::Logger.info "Retrieving #{@s3_path} from logs bucket"
  @rubys3_client.get_object(bucket: "logs", key: @s3_path, response_target: local_path)
  if File.exist?(local_path)
    @size = File.size(local_path)
    @local_path = local_path
  end
rescue => err
  @error = err
  Cosmos::Logger.error "Failed to retrieve #{@s3_path}\n#{err.formatted}"
end

#unreserveObject



68
69
70
71
72
73
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 68

def unreserve
  @mutex.synchronize do
    @reservation_count -= 1
    delete() if @reservation_count <= 0
  end
end