fix: stable event hashing, HEAD fallback, caldav str/bytes compat

This commit is contained in:
2026-06-11 23:06:47 +02:00
parent 0f390ff1e1
commit d628f03a82
3 changed files with 52 additions and 9 deletions
+28 -3
View File
@@ -56,9 +56,14 @@ def sync_once(state: SyncState, health: HealthServer, config: Config) -> bool:
logger.info("Starting sync cycle...")
try:
r = requests.head(config.ics_url, headers=HEADERS, timeout=30, allow_redirects=True)
r.raise_for_status()
remote_etag = r.headers.get("ETag")
remote_etag = None
try:
r = requests.head(config.ics_url, headers=HEADERS, timeout=15, allow_redirects=True)
if r.status_code < 400:
remote_etag = r.headers.get("ETag")
except Exception:
pass
cached_hash, cached_etag, _ = state.get_ics_cache()
if remote_etag and cached_etag == remote_etag:
@@ -91,6 +96,8 @@ def sync_once(state: SyncState, health: HealthServer, config: Config) -> bool:
to_update = deltas["to_update"]
to_delete = deltas["to_delete"]
is_first_run = not state.get_event_uids() and not cached_hash
if not to_add and not to_update and not to_delete:
logger.info("Calendar is already in sync.")
duration = time.time() - start_time
@@ -100,6 +107,24 @@ def sync_once(state: SyncState, health: HealthServer, config: Config) -> bool:
True,
len(ics_uids),
)
for uid, h in ics_uids.items():
state.upsert_event(uid, h)
return True
if is_first_run and not to_delete:
logger.info(
"First run detected: %d events in ICS. Registering state without re-adding to calendar.",
len(to_add),
)
for uid, h in ics_uids.items():
state.upsert_event(uid, h)
duration = time.time() - start_time
health.update_status(
datetime.now(timezone.utc),
duration,
True,
len(ics_uids),
)
return True
logger.info(