Class: S3File
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#local_path ⇒ Object
readonly
Returns the value of attribute local_path.
-
#priority ⇒ Object
Returns the value of attribute priority.
-
#reservation_count ⇒ Object
readonly
Returns the value of attribute reservation_count.
-
#s3_path ⇒ Object
readonly
Returns the value of attribute s3_path.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#delete ⇒ Object
private.
-
#initialize(s3_path, size = 0, priority = 0) ⇒ S3File
constructor
A new instance of S3File.
- #reserve ⇒ Object
- #retrieve ⇒ Object
- #unreserve ⇒ Object
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
#error ⇒ Object (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_path ⇒ Object (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 |
#priority ⇒ Object
Returns the value of attribute priority.
30 31 32 |
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 30 def priority @priority end |
#reservation_count ⇒ Object (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_path ⇒ Object (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 |
#size ⇒ Object (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
#delete ⇒ Object
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 |
#reserve ⇒ Object
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 |
#retrieve ⇒ Object
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 |
#unreserve ⇒ Object
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 |