Class: OpenC3::CleanupMicroservice

Inherits:
Microservice show all
Defined in:
lib/openc3/microservices/cleanup_microservice.rb

Direct Known Subclasses

ScopeCleanupMicroservice

Instance Attribute Summary

Attributes inherited from Microservice

#count, #custom, #error, #logger, #microservice_status_thread, #name, #scope, #secrets, #state

Instance Method Summary collapse

Methods inherited from Microservice

#as_json, #microservice_cmd, run, #setup_microservice_topic

Constructor Details

#initialize(*args) ⇒ CleanupMicroservice

Returns a new instance of CleanupMicroservice.



30
31
32
33
34
35
36
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 30

def initialize(*args)
  super(*args)
  @metric.set(name: 'cleanup_total', value: @count, type: 'counter')
  @delete_count = 0
  @metric.set(name: 'cleanup_delete_total', value: @delete_count, type: 'counter')
  @sleeper = Sleeper.new
end

Instance Method Details

#cleanup(areas, bucket) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 38

def cleanup(areas, bucket)
  @state = 'GETTING_OBJECTS'
  start_time = Time.now
  areas.each do |prefix, retain_time|
    next unless retain_time
    time = start_time - retain_time
    oldest_list = BucketUtilities.files_between_time(ENV['OPENC3_LOGS_BUCKET'], prefix, nil, time)
    if oldest_list.length > 0
      @state = 'DELETING_OBJECTS'
      oldest_list.each_slice(1000) do |slice|
        # The delete_objects function utilizes an MD5 hash when verifying the checksums, which is not
        # FIPS compliant (https://github.com/aws/aws-sdk-ruby/issues/2645).
        # delete_object does NOT require an MD5 hash and will work on FIPS compliant systems. It is
        # probably less performant, but we can instead delete each item one at a time.
        # bucket.delete_objects(bucket: ENV['OPENC3_LOGS_BUCKET'], keys: slice)
        slice.each do |item|
          bucket.delete_object(bucket: ENV['OPENC3_LOGS_BUCKET'], key: item)
        end
        @logger.debug("Cleanup deleted #{slice.length} log files")
        @delete_count += slice.length
        @metric.set(name: 'cleanup_delete_total', value: @delete_count, type: 'counter')
      end
    end
  end
end

#get_areas_and_poll_timeObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 64

def get_areas_and_poll_time
  split_name = @name.split("__")
  target_name = split_name[-1]
  target = TargetModel.get_model(name: target_name, scope: @scope)

  areas = [
    ["#{@scope}/raw_logs/cmd/#{target_name}", target.cmd_log_retain_time],
    ["#{@scope}/decom_logs/cmd/#{target_name}", target.cmd_decom_log_retain_time],
    ["#{@scope}/raw_logs/tlm/#{target_name}", target.tlm_log_retain_time],
    ["#{@scope}/decom_logs/tlm/#{target_name}", target.tlm_decom_log_retain_time],
    ["#{@scope}/reduced_minute_logs/tlm/#{target_name}", target.reduced_minute_log_retain_time],
    ["#{@scope}/reduced_hour_logs/tlm/#{target_name}", target.reduced_hour_log_retain_time],
    ["#{@scope}/reduced_day_logs/tlm/#{target_name}", target.reduced_day_log_retain_time],
  ]
  return areas, target.cleanup_poll_time
end

#runObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 81

def run
  bucket = Bucket.getClient()
  while true
    break if @cancel_thread
    areas, poll_time = get_areas_and_poll_time()
    cleanup(areas, bucket)

    @count += 1
    @metric.set(name: 'cleanup_total', value: @count, type: 'counter')

    @state = 'SLEEPING'
    break if @sleeper.sleep(poll_time)
  end
end

#shutdownObject



96
97
98
99
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 96

def shutdown
  @sleeper.cancel
  super()
end