From 86626495c2f1ea25f5f450595b2ff7eb258c5133 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Thu, 5 Dec 2024 15:52:18 -0700 Subject: [PATCH] make the mqtt feeder a timer since we're getting memory leaks or something --- feeder/mqtt.py | 40 ++++++++-------------- feeder/systemd/enable.sh | 15 -------- feeder/systemd/space-weather-cache.service | 3 +- feeder/systemd/space-weather-mqtt.service | 1 - feeder/systemd/space-weather-mqtt.timer | 9 +++++ 5 files changed, 24 insertions(+), 44 deletions(-) delete mode 100755 feeder/systemd/enable.sh create mode 100644 feeder/systemd/space-weather-mqtt.timer diff --git a/feeder/mqtt.py b/feeder/mqtt.py index 1bc6e97..6929049 100644 --- a/feeder/mqtt.py +++ b/feeder/mqtt.py @@ -1,13 +1,12 @@ import gc import logging +import numpy as np import os +import paho.mqtt.client as mqtt import pickle import sys import time from datetime import datetime, timezone - -import numpy as np -import paho.mqtt.client as mqtt from dateutil.parser import parse from redis import Redis @@ -56,32 +55,21 @@ def publish(topic: str, msg): def main(): redis = Redis(host='localhost', port=6379, db=0) - - while True: + data = redis.get('glotec') + while data is None: + logging.warning('Redis has not been populated yet. Is cache.py running? Sleeping 10s...') + time.sleep(10) data = redis.get('glotec') - while data is None: - logging.warning('Redis has not been populated yet. Is cache.py running? Sleeping 10s...') - time.sleep(10) - data = redis.get('glotec') - geojson = pickle.loads(data) + geojson = pickle.loads(data) - utc_hr = datetime.now(timezone.utc).hour - logging.info(f'Using hour {utc_hr}') + utc_hr = datetime.now(timezone.utc).hour + logging.info(f'Using hour {utc_hr}') - glotec_map_ranged, _ = plot_glotec_map(geojson, [float(LON_RANGE_MIN), float(LON_RANGE_MAX)], [float(LAT_RANGE_MIN), float(LAT_RANGE_MAX)]) - avg_tec = np.mean(glotec_map_ranged) - logging.info(f'Data timestamp: {parse(geojson["time_tag"]).isoformat()}') - latest = round(avg_tec, 1) - publish('glotec', latest) - - del data - del geojson - del glotec_map_ranged - del avg_tec - del latest - gc.collect() - - time.sleep(60) + glotec_map_ranged, _ = plot_glotec_map(geojson, [float(LON_RANGE_MIN), float(LON_RANGE_MAX)], [float(LAT_RANGE_MIN), float(LAT_RANGE_MAX)]) + avg_tec = np.mean(glotec_map_ranged) + logging.info(f'Data timestamp: {parse(geojson["time_tag"]).isoformat()}') + latest = round(avg_tec, 1) + publish('glotec', latest) if __name__ == '__main__': diff --git a/feeder/systemd/enable.sh b/feeder/systemd/enable.sh deleted file mode 100755 index de85e9d..0000000 --- a/feeder/systemd/enable.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -SOURCE=${BASH_SOURCE[0]} -while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink - DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd ) - SOURCE=$(readlink "$SOURCE") - [[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located -done -DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd ) - -cp "$DIR"/*.service /etc/systemd/system -systemctl daemon-reload -systemctl enable --now *.service -systemctl restart *.service -systemctl status *.service \ No newline at end of file diff --git a/feeder/systemd/space-weather-cache.service b/feeder/systemd/space-weather-cache.service index ea821f1..3f70282 100644 --- a/feeder/systemd/space-weather-cache.service +++ b/feeder/systemd/space-weather-cache.service @@ -8,8 +8,7 @@ User=homeassistant EnvironmentFile=/etc/secrets/space-weather ExecStart=/srv/ha-noaa-space-weather/venv/bin/python /srv/ha-noaa-space-weather/feeder/cache.py SyslogIdentifier=space-weather-cache -Restart=on-failure -RestartSec=5s +Restart=always [Install] WantedBy=multi-user.target diff --git a/feeder/systemd/space-weather-mqtt.service b/feeder/systemd/space-weather-mqtt.service index 7e03340..c6b8c3f 100644 --- a/feeder/systemd/space-weather-mqtt.service +++ b/feeder/systemd/space-weather-mqtt.service @@ -9,7 +9,6 @@ EnvironmentFile=/etc/secrets/space-weather ExecStart=/srv/ha-noaa-space-weather/venv/bin/python /srv/ha-noaa-space-weather/feeder/mqtt.py SyslogIdentifier=space-weather-mqtt Restart=on-failure -RestartSec=5s [Install] WantedBy=multi-user.target diff --git a/feeder/systemd/space-weather-mqtt.timer b/feeder/systemd/space-weather-mqtt.timer new file mode 100644 index 0000000..e597e1e --- /dev/null +++ b/feeder/systemd/space-weather-mqtt.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Space Weather MQTT timer + +[Timer] +OnCalendar=hourly +Persistent=true + +[Install] +WantedBy=timers.target \ No newline at end of file