Add daily cleanup of old notifications in EventsWorker: Implement a new method to delete notifications older than 30 days, enhancing database management and reducing clutter. Update run loop to trigger this cleanup daily, ensuring timely maintenance of notification records.
This commit is contained in:
@@ -402,6 +402,7 @@ impl EventsWorker {
|
||||
|
||||
fn run_loop(pool: ConnectionPool, broker: MessageBroker, state: Arc<WorkerState>) {
|
||||
let mut last_event_check = None;
|
||||
let mut last_notification_cleanup: Option<Instant> = None;
|
||||
let mut rng = rand::thread_rng();
|
||||
let events = Self::initialize_events();
|
||||
eprintln!("[EventsWorker] Worker-Thread gestartet");
|
||||
@@ -414,7 +415,6 @@ impl EventsWorker {
|
||||
let now = Instant::now();
|
||||
|
||||
// Heartbeat im Run-Loop (unabhängig davon, ob Events triggern)
|
||||
// Damit man sofort sieht, ob der Worker überhaupt läuft.
|
||||
static LAST_RUNLOOP_HEARTBEAT: Mutex<Option<Instant>> = Mutex::new(None);
|
||||
{
|
||||
let mut last = LAST_RUNLOOP_HEARTBEAT.lock().unwrap();
|
||||
@@ -441,8 +441,13 @@ impl EventsWorker {
|
||||
last_event_check = Some(now);
|
||||
}
|
||||
|
||||
// 10-Sekunden-Wartezeit in kurze Scheiben aufteilen, damit ein Shutdown
|
||||
// (running_worker = false) schnell greift.
|
||||
// Alte Notifications einmal täglich aufräumen (alle 24 Stunden)
|
||||
if should_run_interval(last_notification_cleanup, now, Duration::from_secs(86400)) {
|
||||
Self::cleanup_old_notifications(&pool);
|
||||
last_notification_cleanup = Some(now);
|
||||
}
|
||||
|
||||
// 10-Sekunden-Wartezeit in kurze Scheiben aufteilen
|
||||
const SLICE_MS: u64 = 500;
|
||||
let total_ms = 10_000;
|
||||
let mut slept = 0;
|
||||
@@ -458,6 +463,25 @@ impl EventsWorker {
|
||||
}
|
||||
}
|
||||
|
||||
fn cleanup_old_notifications(pool: &ConnectionPool) {
|
||||
use crate::worker::sql::QUERY_DELETE_OLD_NOTIFICATIONS;
|
||||
|
||||
let result = (|| -> Result<(), DbError> {
|
||||
let mut conn = pool
|
||||
.get()
|
||||
.map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
|
||||
|
||||
conn.prepare("delete_old_notifications", QUERY_DELETE_OLD_NOTIFICATIONS)?;
|
||||
conn.execute("delete_old_notifications", &[])?;
|
||||
Ok(())
|
||||
})();
|
||||
|
||||
match result {
|
||||
Ok(()) => eprintln!("[EventsWorker] Alte Notifications (>30 Tage) aufgeräumt"),
|
||||
Err(err) => eprintln!("[EventsWorker] Fehler beim Aufräumen alter Notifications: {err}"),
|
||||
}
|
||||
}
|
||||
|
||||
// Globaler Skalierungsfaktor für Ereignisfrequenz.
|
||||
// Default: 1.0 (unverändert). Optional per ENV `EVENT_RATE_SCALE` konfigurierbar.
|
||||
fn event_rate_scale() -> f64 {
|
||||
|
||||
Reference in New Issue
Block a user