Add signal handlers, retry logic, and improved boot script
This commit is contained in:
parent
86a2d896f9
commit
4bf5f13233
Binary file not shown.
BIN
__pycache__/setup.cpython-314.pyc
Normal file
BIN
__pycache__/setup.cpython-314.pyc
Normal file
Binary file not shown.
26
main.py
26
main.py
@ -12,6 +12,7 @@ import logging
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
|
import signal
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@ -92,6 +93,16 @@ class GadgetbridgeMQTT:
|
|||||||
self.mqtt_client = None
|
self.mqtt_client = None
|
||||||
self.last_publish_time = 0
|
self.last_publish_time = 0
|
||||||
self.last_db_mtime = 0
|
self.last_db_mtime = 0
|
||||||
|
self.running = True
|
||||||
|
|
||||||
|
# Register signal handlers for graceful shutdown
|
||||||
|
signal.signal(signal.SIGTERM, self._signal_handler)
|
||||||
|
signal.signal(signal.SIGINT, self._signal_handler)
|
||||||
|
|
||||||
|
def _signal_handler(self, signum, frame):
|
||||||
|
"""Handle shutdown signals gracefully"""
|
||||||
|
logger.info(f"Received signal {signum}. Shutting down...")
|
||||||
|
self.running = False
|
||||||
|
|
||||||
def connect_mqtt(self):
|
def connect_mqtt(self):
|
||||||
"""Connect to MQTT broker"""
|
"""Connect to MQTT broker"""
|
||||||
@ -365,18 +376,23 @@ class GadgetbridgeMQTT:
|
|||||||
discovery_published = False
|
discovery_published = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while self.running:
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
|
|
||||||
# Check if it's time to trigger sync and publish
|
# Check if it's time to trigger sync and publish
|
||||||
if current_time - self.last_publish_time >= interval:
|
if current_time - self.last_publish_time >= interval:
|
||||||
|
try:
|
||||||
logger.info("Triggering Gadgetbridge sync...")
|
logger.info("Triggering Gadgetbridge sync...")
|
||||||
trigger_gadgetbridge_sync()
|
trigger_gadgetbridge_sync()
|
||||||
|
|
||||||
# Wait for export to complete
|
# Wait for export to complete
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
|
|
||||||
# Find latest database
|
# Find latest database with retry
|
||||||
|
self.db_path = find_latest_db(export_dir)
|
||||||
|
if not self.db_path:
|
||||||
|
logger.warning(f"No database found, retrying in 3s...")
|
||||||
|
time.sleep(3)
|
||||||
self.db_path = find_latest_db(export_dir)
|
self.db_path = find_latest_db(export_dir)
|
||||||
|
|
||||||
if self.db_path:
|
if self.db_path:
|
||||||
@ -399,14 +415,18 @@ class GadgetbridgeMQTT:
|
|||||||
else:
|
else:
|
||||||
logger.warning(f"No database found in {export_dir}")
|
logger.warning(f"No database found in {export_dir}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Sync cycle failed: {e}")
|
||||||
|
|
||||||
self.last_publish_time = current_time
|
self.last_publish_time = current_time
|
||||||
logger.info(f"Next publish in {interval}s...")
|
logger.info(f"Next publish in {interval}s...")
|
||||||
|
|
||||||
time.sleep(10) # Check every 10 seconds
|
time.sleep(10) # Check every 10 seconds
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logger.info("Shutting down...")
|
logger.info("Interrupted by user")
|
||||||
finally:
|
finally:
|
||||||
|
logger.info("Cleaning up...")
|
||||||
self.disconnect_mqtt()
|
self.disconnect_mqtt()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
11
setup.py
11
setup.py
@ -121,9 +121,20 @@ termux-wake-lock
|
|||||||
# Wait for system to fully boot
|
# Wait for system to fully boot
|
||||||
sleep 30
|
sleep 30
|
||||||
|
|
||||||
|
# Kill any existing process
|
||||||
|
pkill -f gadgetbridge_mqtt.py 2>/dev/null
|
||||||
|
|
||||||
# Start the MQTT publisher in background
|
# Start the MQTT publisher in background
|
||||||
cd ~
|
cd ~
|
||||||
python ~/scripts/gadgetbridge_mqtt.py >> ~/gb_mqtt.log 2>&1 &
|
python ~/scripts/gadgetbridge_mqtt.py >> ~/gb_mqtt.log 2>&1 &
|
||||||
|
|
||||||
|
# Verify it started
|
||||||
|
sleep 2
|
||||||
|
if pgrep -f gadgetbridge_mqtt.py > /dev/null; then
|
||||||
|
echo "$(date): Gadgetbridge MQTT started successfully" >> ~/gb_mqtt.log
|
||||||
|
else
|
||||||
|
echo "$(date): ERROR - Failed to start" >> ~/gb_mqtt.log
|
||||||
|
fi
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with open(boot_script, "w") as f:
|
with open(boot_script, "w") as f:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user