Handle errors on the gRPC connection

Also, actually do the thing I said I was doing in the prior checkin by writing state as a string instead of integer.

And a bit more cleanup.
This commit is contained in:
sparky8512 2020-12-30 10:17:02 -08:00
parent 61e9abf7d4
commit e1a4c473c8
2 changed files with 64 additions and 27 deletions

View file

@ -10,6 +10,8 @@ import grpc
import spacex.api.device.device_pb2 import spacex.api.device.device_pb2
import spacex.api.device.device_pb2_grpc import spacex.api.device.device_pb2_grpc
# Note that if you remove the 'with' clause here, you need to separately
# call channel.close() when you're done with the gRPC connection.
with grpc.insecure_channel('192.168.100.1:9200') as channel: with grpc.insecure_channel('192.168.100.1:9200') as channel:
stub = spacex.api.device.device_pb2_grpc.DeviceStub(channel) stub = spacex.api.device.device_pb2_grpc.DeviceStub(channel)
response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={})) response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={}))

View file

@ -17,6 +17,9 @@ import spacex.api.device.device_pb2_grpc
import time import time
fVerbose = True
sleepTime = 30
class DeviceStatusSeries(SeriesHelper): class DeviceStatusSeries(SeriesHelper):
class Meta: class Meta:
series_name = "spacex.starlink.user_terminal.status" series_name = "spacex.starlink.user_terminal.status"
@ -38,46 +41,78 @@ class DeviceStatusSeries(SeriesHelper):
"fraction_obstructed"] "fraction_obstructed"]
tags = ["id"] tags = ["id"]
influxClient = InfluxDBClient("localhost", 8086, "script-user", "password", "dishstats", ssl=False, retries=1, timeout=15) influxClient = InfluxDBClient(host="localhost", port=8086, username="script-user", password="password", database="dishstats", ssl=False, retries=1, timeout=15)
try: try:
dishChannel = grpc.insecure_channel("192.168.100.1:9200") dishChannel = None
lastId = None
fLastFailed = False
pending = 0 pending = 0
count = 0 count = 0
while True: while True:
stub = spacex.api.device.device_pb2_grpc.DeviceStub(dishChannel) try:
response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={})) if dishChannel is None:
status = response.dish_get_status dishChannel = grpc.insecure_channel("192.168.100.1:9200")
DeviceStatusSeries( stub = spacex.api.device.device_pb2_grpc.DeviceStub(dishChannel)
id=status.device_info.id, response = stub.Handle(spacex.api.device.device_pb2.Request(get_status={}))
hardware_version=status.device_info.hardware_version, status = response.dish_get_status
software_version=status.device_info.software_version, DeviceStatusSeries(
state=status.state, id=status.device_info.id,
alert_motors_stuck=status.alerts.motors_stuck, hardware_version=status.device_info.hardware_version,
alert_thermal_throttle=status.alerts.thermal_throttle, software_version=status.device_info.software_version,
alert_thermal_shutdown=status.alerts.thermal_shutdown, state=spacex.api.device.dish_pb2.DishState.Name(status.state),
alert_unexpected_location=status.alerts.unexpected_location, alert_motors_stuck=status.alerts.motors_stuck,
snr=status.snr, alert_thermal_throttle=status.alerts.thermal_throttle,
seconds_to_first_nonempty_slot=status.seconds_to_first_nonempty_slot, alert_thermal_shutdown=status.alerts.thermal_shutdown,
pop_ping_drop_rate=status.pop_ping_drop_rate, alert_unexpected_location=status.alerts.unexpected_location,
downlink_throughput_bps=status.downlink_throughput_bps, snr=status.snr,
uplink_throughput_bps=status.uplink_throughput_bps, seconds_to_first_nonempty_slot=status.seconds_to_first_nonempty_slot,
pop_ping_latency_ms=status.pop_ping_latency_ms, pop_ping_drop_rate=status.pop_ping_drop_rate,
currently_obstructed=status.obstruction_stats.currently_obstructed, downlink_throughput_bps=status.downlink_throughput_bps,
fraction_obstructed=status.obstruction_stats.fraction_obstructed) uplink_throughput_bps=status.uplink_throughput_bps,
pop_ping_latency_ms=status.pop_ping_latency_ms,
currently_obstructed=status.obstruction_stats.currently_obstructed,
fraction_obstructed=status.obstruction_stats.fraction_obstructed)
lastId = status.device_info.id
fLastFailed = False
except Exception as e:
if not dishChannel is None:
dishChannel.close()
dishChannel = None
if fLastFailed:
if not lastId is None:
DeviceStatusSeries(id=lastId, state="DISH_UNREACHABLE")
else:
# Retry once, because the connection may have been lost while
# we were sleeping
fLastFailed = True
continue
pending = pending + 1 pending = pending + 1
print("Samples: " + str(pending)) if fVerbose:
print("Samples: " + str(pending))
count = count + 1
if count > 5: if count > 5:
try: try:
DeviceStatusSeries.commit(influxClient) DeviceStatusSeries.commit(influxClient)
print("Wrote " + str(pending)) if fVerbose:
print("Wrote " + str(pending))
pending = 0 pending = 0
except Exception as e: except Exception as e:
print("Failed to write: " + str(e)) print("Failed to write: " + str(e))
count = 0 count = 0
count = count + 1 if sleepTime > 0:
time.sleep(5) time.sleep(sleepTime)
else:
break
finally: finally:
# Flush on error/exit # Flush on error/exit
DeviceStatusSeries.commit(influxClient) try:
DeviceStatusSeries.commit(influxClient)
if fVerbose:
print("Wrote " + str(pending))
except Exception as e:
print("Failed to write: " + str(e))
influxClient.close()
if not dishChannel is None:
dishChannel.close()