From 94bd93a18a4206ce38dc0a0adcdff3c2ae350e26 Mon Sep 17 00:00:00 2001 From: OliverGiertz Date: Wed, 8 Apr 2026 09:34:27 +0000 Subject: [PATCH] fix(scheduler): fill schedule gaps instead of always appending to end MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously the scheduler started searching from the last scheduled post date, skipping all free slots in between (e.g. a free slot on Apr 20 would be ignored if the last post was on May 18). Now starts scanning from tomorrow, finding the first available slot regardless of whether earlier dates have gaps — fills the calendar naturally. Also extended lookahead from 30 to 60 days. Co-Authored-By: Claude Sonnet 4.6 --- backend/app/scheduler.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/backend/app/scheduler.py b/backend/app/scheduler.py index a92ba08..a1028fc 100644 --- a/backend/app/scheduler.py +++ b/backend/app/scheduler.py @@ -146,17 +146,18 @@ def _format_slot(d: date, hour: int) -> str: def _find_next_free_slot( - wp_occupied: set[tuple[str, int]], lookahead_days: int = 30 + wp_occupied: set[tuple[str, int]], lookahead_days: int = 60 ) -> tuple[date, int] | None: - """Find the next free (date, hour) slot, anchored after the last scheduled article.""" + """Find the next free (date, hour) slot. + + Starts from tomorrow and scans forward, filling any gaps in the schedule + rather than always appending after the last existing post. + """ today = _today_cet() tomorrow = today + timedelta(days=1) - last_date = _get_last_future_scheduled_date(wp_occupied) - start_date = last_date if (last_date and last_date >= tomorrow) else tomorrow - for offset in range(0, lookahead_days + 1): - candidate = start_date + timedelta(days=offset) + candidate = tomorrow + timedelta(days=offset) hour = _next_free_hour(candidate, wp_occupied) if hour is not None: return candidate, hour