Class: S3FileCache
Constant Summary collapse
- MAX_DISK_USAGE =
20 GB
20_000_000_000
- TIMESTAMP_FORMAT =
TODO: get from different class?
"%Y%m%d%H%M%S%N"
- @@instance =
nil
- @@mutex =
Mutex.new
Instance Attribute Summary collapse
-
#cache_dir ⇒ Object
readonly
Returns the value of attribute cache_dir.
Class Method Summary collapse
Instance Method Summary collapse
-
#file_in_time_range(s3_path, start_time_nsec, end_time_nsec) ⇒ Object
private.
-
#initialize(name = 'default', max_disk_usage = MAX_DISK_USAGE) ⇒ S3FileCache
constructor
A new instance of S3FileCache.
- #reserve_file(cmd_or_tlm, target_name, packet_name, start_time_nsec, end_time_nsec, type = :DECOM, timeout = 60, scope:) ⇒ Object
- #unreserve_file(filename) ⇒ Object
Constructor Details
#initialize(name = 'default', max_disk_usage = MAX_DISK_USAGE) ⇒ S3FileCache
Returns a new instance of S3FileCache.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 157 def initialize(name = 'default', max_disk_usage = MAX_DISK_USAGE) @max_disk_usage = max_disk_usage @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 # Create local file cache location @cache_dir = File.join(Dir.tmpdir, 'cosmos', 'file_cache', name) FileUtils.mkdir_p(@cache_dir) # Clear out local file cache FileUtils.rm_f Dir.glob("#{@cache_dir}/*") @cached_files = S3FileCollection.new @thread = Thread.new do while true file = @cached_files.get_next_to_retrieve # Cosmos::Logger.debug "Next file: #{file}" if file and (file.size + @cached_files.current_disk_usage()) <= @max_disk_usage file.retrieve else sleep(1) end end rescue => err Cosmos::Logger.error "S3FileCache thread unexpectedly died\n#{err.formatted}" end end |
Instance Attribute Details
#cache_dir ⇒ Object (readonly)
Returns the value of attribute cache_dir.
144 145 146 |
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 144 def cache_dir @cache_dir end |
Class Method Details
.instance ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 149 def self.instance return @@instance if @@instance @@mutex.synchronize do @@instance ||= S3FileCache.new end @@instance end |
Instance Method Details
#file_in_time_range(s3_path, start_time_nsec, end_time_nsec) ⇒ Object
private
257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 257 def file_in_time_range(s3_path, start_time_nsec, end_time_nsec) basename = File.basename(s3_path) , , other = basename.split("__") file_start_time_nsec = DateTime.strptime(, TIMESTAMP_FORMAT).to_f * Time::NSEC_PER_SECOND file_end_time_nsec = DateTime.strptime(, TIMESTAMP_FORMAT).to_f * Time::NSEC_PER_SECOND if (start_time_nsec < file_end_time_nsec) and (end_time_nsec >= file_start_time_nsec) return true else return false end end |
#reserve_file(cmd_or_tlm, target_name, packet_name, start_time_nsec, end_time_nsec, type = :DECOM, timeout = 60, scope:) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 191 def reserve_file(cmd_or_tlm, target_name, packet_name, start_time_nsec, end_time_nsec, type = :DECOM, timeout = 60, scope:) # Cosmos::Logger.debug "reserve_file #{cmd_or_tlm}:#{target_name}:#{packet_name} start:#{start_time_nsec / 1_000_000_000} end:#{end_time_nsec / 1_000_000_000} type:#{type} timeout:#{timeout}" # Get List of Files from S3 total_resp = [] token = nil dates = [] cur_date = Time.at(start_time_nsec / Time::NSEC_PER_SECOND).beginning_of_day end_date = Time.at(end_time_nsec / Time::NSEC_PER_SECOND).beginning_of_day cur_date -= 1.day # start looking in the folder for the day before because log files can span across midnight while cur_date <= end_date dates << cur_date.strftime("%Y%m%d") cur_date += 1.day end prefixes = [] dates.each do |date| while true prefixes << "#{scope}/#{type.to_s.downcase}_logs/#{cmd_or_tlm.to_s.downcase}/#{target_name}/#{packet_name}/#{date}" resp = @rubys3_client.list_objects_v2({ bucket: "logs", max_keys: 1000, prefix: prefixes[-1], continuation_token: token }) total_resp.concat(resp.contents) break unless resp.is_truncated token = resp.next_continuation_token end end # Add to needed files files = [] total_resp.each_with_index do |item, index| s3_path = item.key if file_in_time_range(s3_path, start_time_nsec, end_time_nsec) file = @cached_files.add(s3_path, item.size, index) files << file end end # Wait for first file retrieval if files.length > 0 wait_start = Time.now file = files[0] file.reserve while (Time.now - wait_start) < timeout return file.local_path if file.local_path sleep(1) end # Remove reservations if we timeout file.unreserve else Cosmos::Logger.info "No files found for #{prefixes}" end return nil end |
#unreserve_file(filename) ⇒ Object
248 249 250 251 252 253 |
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 248 def unreserve_file(filename) @@mutex.synchronize do file = @cached_files.get(filename) file.unreserve if file end end |