From 5d2b9c3ffb0d49010c4586d2aa92bbecb6e8b7d3 Mon Sep 17 00:00:00 2001 From: Thomas Oettli Date: Sun, 1 Feb 2026 04:57:22 +0100 Subject: [PATCH] Cleanup: use max of last update and start time to determine expiry time --- src/ddns_service/cleanup.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ddns_service/cleanup.py b/src/ddns_service/cleanup.py index c1177d5..3f7e5bc 100644 --- a/src/ddns_service/cleanup.py +++ b/src/ddns_service/cleanup.py @@ -8,12 +8,13 @@ from .models import Hostname, User from datetime import timedelta -def cleanup_expired(app): +def cleanup_expired(app, start_time=None): """ Clean up expired hostnames and return count of cleaned entries. Args: app: Application instance with dns_service and email_service. + start_time: Timezone aware datetime object containg the start time of the cleanup thread. Returns: Number of expired hostnames processed. @@ -30,12 +31,14 @@ def cleanup_expired(app): ipv6_expired = False if hostname.last_ipv4: - expiry_time = hostname.last_ipv4_update + timedelta(seconds=hostname.expiry_ttl) + last_update = max(hostname.last_ipv4_update, start_time) + expiry_time = last_update + timedelta(seconds=hostname.expiry_ttl) if now > expiry_time: ipv4_expired = True if hostname.last_ipv6: - expiry_time = hostname.last_ipv6_update + timedelta(seconds=hostname.expiry_ttl) + last_update = max(hostname.last_ipv6_update, start_time) + expiry_time = last_update + timedelta(seconds=hostname.expiry_ttl) if now > expiry_time: ipv6_expired = True @@ -117,10 +120,11 @@ class ExpiredRecordsCleanupThread(threading.Thread): def run(self): """Run the cleanup loop.""" logging.info(f"Expired records cleanup thread started: interval={self.interval}s") + start_time = now_utc() while not self.stop_event.wait(self.interval): try: - count = cleanup_expired(self.app) + count = cleanup_expired(self.app, start_time) if count > 0: logging.info(f"Expired records cleanup completed: count={count}") except Exception as e: