Avvisa se server backup non raggiungibile
This commit is contained in:
@@ -4,6 +4,7 @@ import argparse
|
||||
import json
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
import tkinter as tk
|
||||
from tkinter import messagebox
|
||||
from pathlib import Path
|
||||
@@ -23,6 +24,8 @@ class BakRestTrayApp:
|
||||
self.config = config
|
||||
self.logger = logger
|
||||
self.registry = ChangeRegistry(config.storage.change_registry)
|
||||
self.stop_event = threading.Event()
|
||||
self.last_alert_reachable: bool | None = None
|
||||
self.icon = pystray.Icon(
|
||||
"BakRestTray",
|
||||
_create_icon_image(),
|
||||
@@ -32,6 +35,7 @@ class BakRestTrayApp:
|
||||
|
||||
def run(self) -> None:
|
||||
self.logger.info("BakRestTray started")
|
||||
threading.Thread(target=self._server_status_monitor, daemon=True).start()
|
||||
self.icon.run()
|
||||
|
||||
def _build_menu(self) -> pystray.Menu:
|
||||
@@ -49,14 +53,19 @@ class BakRestTrayApp:
|
||||
return f"Server: {server} | File in attesa: {pending}"
|
||||
|
||||
def _read_server_status(self) -> str:
|
||||
status = self._read_server_status_data()
|
||||
if status is None:
|
||||
return "sconosciuto"
|
||||
return "raggiungibile" if status.get("reachable") else "non raggiungibile"
|
||||
|
||||
def _read_server_status_data(self) -> dict[str, object] | None:
|
||||
path = self.config.storage.status_file
|
||||
if not path.exists():
|
||||
return "sconosciuto"
|
||||
return None
|
||||
try:
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
return json.loads(path.read_text(encoding="utf-8"))
|
||||
except (OSError, json.JSONDecodeError):
|
||||
return "errore stato"
|
||||
return "raggiungibile" if data.get("reachable") else "non raggiungibile"
|
||||
return None
|
||||
|
||||
def _backup_and_shutdown(self, icon: pystray.Icon, item: pystray.MenuItem) -> None:
|
||||
threading.Thread(target=self._backup_and_shutdown_worker, daemon=True).start()
|
||||
@@ -81,8 +90,29 @@ class BakRestTrayApp:
|
||||
|
||||
def _quit(self, icon: pystray.Icon, item: pystray.MenuItem) -> None:
|
||||
self.logger.info("BakRestTray stopped")
|
||||
self.stop_event.set()
|
||||
self.icon.stop()
|
||||
|
||||
def _server_status_monitor(self) -> None:
|
||||
interval = min(max(self.config.backup.server_check.interval_seconds, 60), 600)
|
||||
while not self.stop_event.wait(interval):
|
||||
status = self._read_server_status_data()
|
||||
if status is None:
|
||||
continue
|
||||
|
||||
reachable = bool(status.get("reachable"))
|
||||
if reachable:
|
||||
self.last_alert_reachable = True
|
||||
continue
|
||||
|
||||
if self.last_alert_reachable is not False:
|
||||
self.last_alert_reachable = False
|
||||
self.logger.warning("Showing backup server unreachable alert")
|
||||
_show_error_message(
|
||||
"Bak&Rest",
|
||||
"Accendi il server di backup o verifica che sia connesso alla rete",
|
||||
)
|
||||
|
||||
|
||||
def _create_icon_image() -> Image.Image:
|
||||
image = Image.new("RGBA", (64, 64), (26, 38, 52, 255))
|
||||
|
||||
Reference in New Issue
Block a user