Class: OpenC3::DecomMicroservice

Inherits:
Microservice show all
Includes:
InterfaceDecomCommon
Defined in:
lib/openc3/microservices/decom_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 included from InterfaceDecomCommon

#handle_build_cmd, #handle_inject_tlm

Methods inherited from Microservice

#as_json, run, #shutdown

Constructor Details

#initialize(*args) ⇒ DecomMicroservice

Returns a new instance of DecomMicroservice.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/openc3/microservices/decom_microservice.rb', line 32

def initialize(*args)
  super(*args)
  # Should only be one target, but there might be multiple decom microservices for a given target
  # First Decom microservice has no number in the name
  if @name =~ /__DECOM__/
    @topics << "#{@scope}__DECOMINTERFACE__{#{@target_names[0]}}"
  end
  Topic.update_topic_offsets(@topics)
  System.telemetry.limits_change_callback = method(:limits_change_callback)
  LimitsEventTopic.sync_system(scope: @scope)
  @error_count = 0
  @metric.set(name: 'decom_total', value: @count, type: 'counter')
  @metric.set(name: 'decom_error_total', value: @error_count, type: 'counter')
end

Instance Method Details

#decom_packet(topic, msg_id, msg_hash, _redis) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/openc3/microservices/decom_microservice.rb', line 82

def decom_packet(topic, msg_id, msg_hash, _redis)
  OpenC3.in_span("decom_packet") do
    msgid_seconds_from_epoch = msg_id.split('-')[0].to_i / 1000.0
    delta = Time.now.to_f - msgid_seconds_from_epoch
    @metric.set(name: 'decom_topic_delta_seconds', value: delta, type: 'gauge', unit: 'seconds', help: 'Delta time between data written to stream and decom start')

    start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    target_name = msg_hash["target_name"]
    packet_name = msg_hash["packet_name"]

    packet = System.telemetry.packet(target_name, packet_name)
    packet.stored = ConfigParser.handle_true_false(msg_hash["stored"])
    # Note: Packet time will be recalculated as part of decom so not setting
    packet.received_time = Time.from_nsec_from_epoch(msg_hash["received_time"].to_i)
    packet.received_count = msg_hash["received_count"].to_i
    extra = msg_hash["extra"]
    if extra and extra.length > 0
      extra = JSON.parse(extra, allow_nan: true, create_additions: true)
      packet.extra = extra
    end
    packet.buffer = msg_hash["buffer"]
    packet.check_limits(System.limits_set) # Process all the limits and call the limits_change_callback (as necessary)

    TelemetryDecomTopic.write_packet(packet, scope: @scope)
    diff = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start # seconds as a float
    @metric.set(name: 'decom_duration_seconds', value: diff, type: 'gauge', unit: 'seconds')
  end
end

#limits_change_callback(packet, item, old_limits_state, value, log_change) ⇒ Object

Called when an item in any packet changes limits states.

Parameters:

  • packet (Packet)

    Packet which has had an item change limits state

  • item (PacketItem)

    The item which has changed limits state

  • old_limits_state (Symbol)

    The previous state of the item. See PacketItemLimits#state

  • value (Object)

    The current value of the item

  • log_change (Boolean)

    Whether to log this limits change event



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/openc3/microservices/decom_microservice.rb', line 119

def limits_change_callback(packet, item, old_limits_state, value, log_change)
  return if @cancel_thread
  packet_time = packet.packet_time
  if value
    message = "#{packet.target_name} #{packet.packet_name} #{item.name} = #{value} is #{item.limits.state}"
  else
    message = "#{packet.target_name} #{packet.packet_name} #{item.name} is disabled"
  end
  message << " (#{packet.packet_time.sys.formatted})" if packet_time

  time_nsec = packet_time ? packet_time.to_nsec_from_epoch : Time.now.to_nsec_from_epoch
  if log_change
    case item.limits.state
    when :BLUE, :GREEN, :GREEN_LOW, :GREEN_HIGH
      @logger.info message
    when :YELLOW, :YELLOW_LOW, :YELLOW_HIGH
      @logger.warn(message, type: Logger::NOTIFICATION)
    when :RED, :RED_LOW, :RED_HIGH
      @logger.error(message, type: Logger::ALERT)
    end
  end

  # The openc3_limits_events topic can be listened to for all limits events, it is a continuous stream
  event = { type: :LIMITS_CHANGE, target_name: packet.target_name, packet_name: packet.packet_name,
            item_name: item.name, old_limits_state: old_limits_state.to_s, new_limits_state: item.limits.state.to_s,
            time_nsec: time_nsec, message: message.to_s }
  LimitsEventTopic.write(event, scope: @scope)

  if item.limits.response
    begin
      item.limits.response.call(packet, item, old_limits_state)
    rescue Exception => e
      @error = e
      @logger.error "#{packet.target_name} #{packet.packet_name} #{item.name} Limits Response Exception!"
      @logger.error "Called with old_state = #{old_limits_state}, new_state = #{item.limits.state}"
      @logger.error e.formatted
    end
  end
end

#runObject



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
78
79
80
# File 'lib/openc3/microservices/decom_microservice.rb', line 47

def run
  while true
    break if @cancel_thread

    begin
      OpenC3.in_span("read_topics") do
        Topic.read_topics(@topics) do |topic, msg_id, msg_hash, redis|
          break if @cancel_thread

          if topic =~ /__DECOMINTERFACE/
            if msg_hash.key?('inject_tlm')
              handle_inject_tlm(msg_hash['inject_tlm'])
              next
            end
            if msg_hash.key?('build_cmd')
              handle_build_cmd(msg_hash['build_cmd'], msg_id)
              next
            end
          else
            decom_packet(topic, msg_id, msg_hash, redis)
            @metric.set(name: 'decom_total', value: @count, type: 'counter')
          end
          @count += 1
        end
      end
      LimitsEventTopic.sync_system_thread_body(scope: @scope)
    rescue => e
      @error_count += 1
      @metric.set(name: 'decom_error_total', value: @error_count, type: 'counter')
      @error = e
      @logger.error("Decom error: #{e.formatted}")
    end
  end
end