feat(tradein): systemd timer scheduler — trigger script + units + SCHEDULER_ENABLE flag (#581) #914
No reviewers
Labels
No labels
admin
analytics
auth
automation
bug
business
chore
ci
compliance
data
data-moat
docs
duplicate
dx
enhancement
Fable 5 ревью
feedback/max
generative
GG-форсайт
needs-discussion
needs-human
observability
pause-bots
performance
priority/p0
priority/p1
priority/p2
priority/p3
scope/backend
scope/db
scope/devops
scope/frontend
scope/qa
scrapers
security
site-finder
stage/1
stage/2
status/blocked
status/done
status/needs-analysis
status/needs-fix
status/qa
status/ready
status/review
status/wip
tech-debt
tradein
ux
week ревью 1
wontfix
вторичка
ИРД
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: lekss361/gendesign#914
Loading…
Add table
Reference in a new issue
No description provided.
Delete branch "feat/581-systemd-scheduler"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
tradein-mvp/scripts/trigger-schedules.py: standalone script called by systemd every 60s. Queriesscrape_schedules WHERE enabled AND next_run_at <= NOW(), POSTs to the correct admin endpoint per source, staggers requests withrandom.uniform(1,4)s, retries onConnectErrorwith exponential backoff (3 attempts), usesflockon/var/run/tradein-trigger.lockto prevent overlapping runs.tradein-scrape-trigger.service(oneshot,TimeoutStartSec=55) andtradein-scrape-trigger.timer(OnUnitActiveSec=60s,AccuracySec=5s), plusinstall.shandREADME.mdwith diagnostics.scheduler_enable: boolfield (SCHEDULER_ENABLEenv, defaulttrue) toSettingsinconfig.py.scheduler_loop()start inmain.pylifespan withif settings.scheduler_enable:so the in-app asyncio loop can be disabled when systemd drives scheduling.Auth note
The trigger script uses
X-Authenticated-User: {ADMIN_TOKEN}(matching the RBAC guard inmain.py— admin-role username fromroles.yaml).Sources without admin endpoint
Sources
rosreestr_dkp_import,listing_source_snapshot,asking_to_sold_ratio_refresh,refresh_search_matview,deactivate_stale_avito,sber_index_pull,rosreestr_quarter_pollhave no dedicated admin POST endpoint — trigger logs warning and skips. They continue via in-app scheduler (SCHEDULER_ENABLE=true).Test plan
python3 -m py_compile tradein-mvp/scripts/trigger-schedules.pypassesruff check tradein-mvp/scripts/trigger-schedules.pypasses (pre-commit verified)bash tradein-mvp/ops/systemd/install.shinstalls timer,systemctl list-timersshows itjournalctl -u tradein-scrape-trigger -fshows trigger log entries every 60sSCHEDULER_ENABLE=falsein.env.runtime+ recreate backend container → confirm "in-app scheduler disabled" logsystemctl start tradein-scrape-trigger.servicetriggers a one-shot run, logs show POST calls to admin endpointsCloses #581
✅ APPROVE — implementation matches vault ADR (Option C: systemd timer on VPS).
Checked:
psycopgv3 (not v2),_normalize_db_urlstrips SQLAlchemy prefix correctlyflock(LOCK_EX|LOCK_NB)+ exit 0 on busy — correct, systemd won't log spurious failuresConnectErroronly (not 4xx/5xx) — correct scopeX-Authenticated-Userover localhost-only — acceptable security posturescheduler_enable=Truedefault preserves existing prod behaviourscheduler_task is not Noneguard in shutdown — cleanAdvisory (no merge block):
get_due_schedules()runs a secondLIMIT 0query to discover column names — could hardcode["id", "source", "schedule_type"]to save a round-trip. Not worth a follow-up issue.