T13.4: run the Okta sync on a schedule (D18)

A daemon timer thread starts from a new lifespan hook in app.py and calls the
T13.3 sync every OKTA_SYNC_INTERVAL_SECONDS (default 900, minimum 300). With no
OKTA_API_TOKEN it starts nothing and the startup log says so.

The api runs 2 gunicorn workers, so each starts its own timer. A single atomic
conditional UPDATE on one app_settings row (key okta_sync) is the gate: exactly
one worker wins each cycle, on SQLite and Postgres, with no lock held. The row
survives a restart, so a restart inside the interval starts no second run. A
fetch is now capped at 120 s in total, so a run is always shorter than the
minimum interval and cannot overlap the next. No migration needed.

docker-compose.yml passes OKTA_API_TOKEN, OKTA_ORG_URL,
OKTA_SYNC_PROFILE_ATTRIBUTE and OKTA_SYNC_INTERVAL_SECONDS to the api service.

tests/okta_sync_schedule_check.py: 38 checks including an 8-worker race on a
file database (exactly one winner, 16 rounds) and fail-closed through the
scheduled path. okta_auth_check.py still 22/22.
This commit is contained in:
2026-10-16 11:34:13 -07:00
parent 103e4d40ee
commit f30989d4b8
7 changed files with 427 additions and 6 deletions

View File

@@ -202,14 +202,28 @@ is sufficient or Okta needs a distinct allowance).
**Done when:**
- [ ] the job runs automatically on the configured interval without manual
- [x] the job runs automatically on the configured interval without manual
invocation
- [ ] interval is env-configurable with a sane default
- [ ] a container restart does not produce a duplicate/overlapping run, and a
*(2026-10-16: a daemon timer thread starts from the app's lifespan hook (`server/app.py`), checks once a minute, and ran the sync on its own in `tests/okta_sync_schedule_check.py`. With `OKTA_API_TOKEN` unset it starts nothing and the startup log says `Okta sync DISABLED`. Booted the real app both ways; the thread starts and ends with the app.)*
- [x] interval is env-configurable with a sane default
*(2026-10-16: `OKTA_SYNC_INTERVAL_SECONDS`, default 900. A non-number uses the default. A value under 300 is raised to 300. Passed through `docker-compose.yml`.)*
- [x] a container restart does not produce a duplicate/overlapping run, and a
slow cycle does not stack with the next one
*(2026-10-16: the api runs 2 gunicorn workers, so each starts its own timer. One row in `app_settings` (key `okta_sync`) is the gate: a worker claims a cycle with one atomic conditional UPDATE, so exactly one wins. Checked with 8 racing workers on a file database, 15 rounds plus the first-insert race: exactly 1 winner every time. The row outlives a restart, so a restart inside the interval starts nothing. A slow run cannot overlap the next: a fetch is capped at 120 s in total and the interval cannot go below 300 s. Inside one process the thread runs one cycle at a time. This needed no migration: `app_settings` already exists.)*
---
**Decided 2026-10-16 (task's call, per this task's "Do"):** in-process timer in the
`api` container, as D18 proposed, not a sidecar. The `backup` pattern was the
alternative; it would need its own image and would not remove the 2-worker
duplicate question, because the sync reads the application database anyway.
**Network check, as the task asked:** the `outbound` network the `api` service
already joins is an ordinary bridge with a gateway, so Okta is reachable with no
network change. The host firewall must allow https to the Okta org.
**One change to T13.2's code:** `fetch_users()` now also stops after 120 seconds in
total (`TOTAL_DEADLINE_SECONDS`), which is what lets the scheduler promise no
overlap. Tested in `tests/okta_sync_check.py` section 8.
### T13.5 — D18: admin visibility
- **Items:** `D18`