LEAD-ENGINE-001.6 · Incremental recurring scheduler
Статус: COMPLETED
Структурированная сводка для этой задачи не записана. Показан сохранённый результат без изменений.
Итог
I built the periodic scheduler for campaign research runs. It is off by default and installs no recurring job of any kind: no cron, systemd, thread or loop. All 18 tests in python3 -m unittest tests.test_scheduler -v pass, and the full suite (144 tests) passes too.
Files
app/scheduler.py: the scheduler and a command-line tool for the owner controls.tests/test_scheduler.py: the acceptance tests. They use synthetic data and temporary databases only.docs/scheduler.md: a short reference for the next tasks, including how to turn it off.app/research_workflow.py: one small change. I added two optional settings so discovery can skip companies we already know. Existing callers behave exactly as before.
How it stays controlled
- Off unless deliberately enabled:
- A global switch is off by default. The database won't accept turning it on without a named person and a reference to the accepted end-to-end acceptance task.
- The
LEADGEN_SCHEDULER_KILL environment variable stops everything immediately. - A tick does nothing unless both allow it, and it never runs on a database that hasn't been set up for the scheduler.
- Runs only when called: each call to
tick() does at most one bounded run by default, then returns. The command-line tool has no "run" command, only status, plan, pause, resume, enable and disable. - Per-campaign pause:
- A campaign always starts paused, including after its config changes.
- Only a named person can resume it.
- After 2 failures in a row (configurable), it pauses itself and stays paused until someone resumes it.
- Budgets: each campaign has limits over a rolling time window on runs, tokens, executor calls and cost. A run starts only if the usage so far plus that run's worst case fits within those limits. Crashed runs count at their full worst case. Scheduled campaigns must have a finite per-run cost limit.
- Locks and no overlap:
- Locks expire on their own, so a dead worker can't block things forever.
- One lock allows only one tick at a time, and another allows one worker per campaign.
- A campaign never starts a run while it already has one running, including Factory one-shot runs.
- The next due time is set when a run starts, so a crash or failure can't cause rapid repeated retries.
- Recovery after a crash:
- If research finished but the import didn't, the next tick imports the same saved, checksummed file without repeating the research.
- If the crash happened during research, the run is marked failed as interrupted.
- Runs that still have a live worker are never touched.
- Repeat runs and duplicates:
- Discovery receives the time of the last successful run and the companies seen recently. Those companies are dropped before enrichment, so no budget is spent on them.
- New companies get a lead whose ID stays the same across runs.
- Companies seen again don't create a duplicate lead. The new card is added to a history table that can only grow, and the lead's last-seen date is updated.
- Everything imported stays UNVERIFIED, and nothing allows outreach.
- Logs: every step is written to a log table that can't be edited or deleted, and also to the Python logger.
State of the live system
data/leads.db was not modified and has none of the scheduler tables; it still holds 8 leads.- The user's crontab is still empty.
- No campaign is registered, and the pilot confi
[truncated]
Решение
Ожидает вашего решения: принять, вернуть на доработку или отложить.