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.



25
26
27
28
29
30
31
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 25

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 33

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



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 59

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}/raw_logs/tlm/#{target_name}", target.tlm_log_retain_time],
  ]
  return areas, target.cleanup_poll_time
end

#runObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 71

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



86
87
88
89
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 86

def shutdown
  @sleeper.cancel
  super()
end