Class: OpenC3::PeriodicMicroservice

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

Constant Summary collapse

STARTUP_DELAY_SECONDS =

Two Minutes

2 * 60
SLEEP_PERIOD_SECONDS =

Run once per day

24 * 60 * 60

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) ⇒ PeriodicMicroservice

Returns a new instance of PeriodicMicroservice.



27
28
29
30
# File 'lib/openc3/microservices/periodic_microservice.rb', line 27

def initialize(*args)
  super(*args)
  @metric.set(name: 'periodic_total', value: @count, type: 'counter')
end

Instance Method Details

#runObject



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

def run
  @run_sleeper = Sleeper.new
  return if @run_sleeper.sleep(STARTUP_DELAY_SECONDS)
  while true
    models = OfflineAccessModel.get_all_models(scope: @scope)
    models.each do |name, model|
      if model.offline_access_token
        auth = OpenC3KeycloakAuthentication.new(ENV['OPENC3_KEYCLOAK_URL'])
        valid_token = auth.get_token_from_refresh_token(model.offline_access_token)
        if valid_token
          @logger.info("Refreshed offline access token for #{name}")
          model.offline_access_token = auth.refresh_token
        else
          @logger.error("Unable to refresh offline access token for #{name}")
          model.offline_access_token = nil
        end
        model.update
      end
    end
    @count += 1
    @metric.set(name: 'periodic_total', value: @count, type: 'counter')
    break if @cancel_thread
    break if @run_sleeper.sleep(SLEEP_PERIOD_SECONDS)
  end
end

#shutdownObject



58
59
60
61
# File 'lib/openc3/microservices/periodic_microservice.rb', line 58

def shutdown
  @run_sleeper.cancel if @run_sleeper
  super()
end