Class: OpenC3::CleanupMicroservice

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

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, run

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

#runObject



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 38

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

  bucket = Bucket.getClient()
  while true
    break if @cancel_thread

    @state = 'GETTING_OBJECTS'
    start_time = Time.now
    [
     ["#{@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],
    ].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|
          bucket.delete_objects(bucket: ENV['OPENC3_LOGS_BUCKET'], keys: slice)
          @logger.debug("Deleted #{slice.length} #{target_name} log files")
          @delete_count += slice.length
          @metric.set(name: 'cleanup_delete_total', value: @delete_count, type: 'counter')
        end
      end
    end

    @count += 1
    @metric.set(name: 'cleanup_total', value: @count, type: 'counter')
    @state = 'SLEEPING'
    break if @sleeper.sleep(target.cleanup_poll_time)
  end
end

#shutdownObject



79
80
81
82
# File 'lib/openc3/microservices/cleanup_microservice.rb', line 79

def shutdown
  @sleeper.cancel
  super()
end