wrongmove: stabilise setup_periodic_tasks tests after market-aggregator add

CI test-unit failed on pipeline #49 because the three TestSetupPeriodicTasks
cases asserted exact call counts on `sender.add_periodic_task` and the
new unconditional `daily-market-aggregator` registration bumped each by
one. Fix:

- `tasks/listing_tasks.py`: lifted the market-aggregator registration
  out of the SchedulesConfig try-block — it's now independent of the
  user's SCRAPE_SCHEDULES (a malformed scrape config no longer takes
  the aggregator down with it).
- `tests/unit/test_listing_tasks.py`: updated the four cases to account
  for the +1 unconditional aggregator call. `test_handles_config_error_
  gracefully` now asserts the aggregator still registers when
  SchedulesConfig.from_env raises (regression coverage for the
  independence guarantee).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Viktor Barzin 2026-05-16 12:07:50 +00:00
parent 49e3514780
commit 9fdf7fd356
2 changed files with 40 additions and 22 deletions

View file

@ -635,27 +635,33 @@ async def _dump_listings_full_inner(
@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
"""Register periodic tasks from environment configuration."""
"""Register periodic tasks from environment configuration.
The daily market aggregator is registered unconditionally so it stays
healthy even when the user's `SCRAPE_SCHEDULES` env var is malformed —
the two systems are orthogonal.
"""
try:
config = SchedulesConfig.from_env()
config: SchedulesConfig | None = SchedulesConfig.from_env()
except ValueError as e:
celery_logger.error(f"Failed to load schedule configuration: {e}")
return
config = None
for schedule in config.get_enabled_schedules():
celery_logger.info(
f"Registering periodic task: {schedule.name} at {schedule.hour}:{schedule.minute}"
)
if config is not None:
for schedule in config.get_enabled_schedules():
celery_logger.info(
f"Registering periodic task: {schedule.name} at {schedule.hour}:{schedule.minute}"
)
sender.add_periodic_task(
crontab(
minute=schedule.minute,
hour=schedule.hour,
day_of_week=schedule.day_of_week,
),
dump_listings_task.s(schedule.to_query_parameters().model_dump_json()),
name=schedule.name,
)
sender.add_periodic_task(
crontab(
minute=schedule.minute,
hour=schedule.hour,
day_of_week=schedule.day_of_week,
),
dump_listings_task.s(schedule.to_query_parameters().model_dump_json()),
name=schedule.name,
)
# Daily market aggregator — fires after the 03:00 RENT scrape so the
# snapshot reflects today's freshly-pulled data. Imported lazily to