# TradeIn systemd scrape trigger

Replaces the in-app asyncio scheduler with a systemd timer that calls the admin API every 60s.

## Install

Run as root on the VPS after deploy:

```bash
cd /opt/gendesign/tradein-mvp/ops/systemd
bash install.sh
```

`install.sh` copies the unit files to `/etc/systemd/system/`, reloads systemd, and
enables + starts the timer.

## Environment variables

Create `/etc/tradein/trigger.env` before enabling the timer:

```ini
# psycopg v3 connection string (postgresql://... or postgresql+psycopg://...)
TRADEIN_DATABASE_URL=postgresql://tradein:<password>@localhost:5432/tradein

# Admin username from roles.yaml with role=admin — passed as X-Authenticated-User header
TRADEIN_ADMIN_TOKEN=admin

# Backend base URL (no trailing slash). Default: http://localhost:8000
TRADEIN_API_BASE=http://localhost:8000
```

## Diagnostic commands

```bash
# Follow live timer logs
journalctl -u tradein-scrape-trigger -f

# List all active timers and next trigger times
systemctl list-timers

# Check timer status
systemctl status tradein-scrape-trigger.timer

# Check last service run
systemctl status tradein-scrape-trigger.service

# Manual one-shot trigger (for testing)
systemctl start tradein-scrape-trigger.service
```

## Notes

- The script uses `flock` on `/var/run/tradein-trigger.lock` to prevent overlapping
  runs if the previous invocation is still running.
- Sources without a dedicated admin endpoint (e.g. `rosreestr_dkp_import`,
  `listing_source_snapshot`) are logged as warnings and skipped. Set
  `SCHEDULER_ENABLE=true` in the backend env to handle them via the in-app scheduler.
