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:
@@ -230,6 +230,23 @@ every_log = " ".join(cap.lines)
|
||||
chk("no log line holds the API token", TOKEN not in every_log)
|
||||
chk("no log line holds the client secret", CLIENT_SECRET not in every_log)
|
||||
|
||||
# ── 8. the whole fetch is time-bounded (T13.4 relies on it) ──────────────────
|
||||
print("\n8. the whole fetch is time-bounded")
|
||||
reset_env()
|
||||
saved = okta_sync.TOTAL_DEADLINE_SECONDS
|
||||
okta_sync.TOTAL_DEADLINE_SECONDS = 0.0
|
||||
|
||||
|
||||
def slow_pages(req):
|
||||
return httpx.Response(200, json=[user("alice")],
|
||||
headers={"Link": f'<{ORG}/api/v1/users?after=x>; rel="next"'})
|
||||
|
||||
|
||||
raises(slow_pages, "a fetch that runs past the total deadline")
|
||||
okta_sync.TOTAL_DEADLINE_SECONDS = saved
|
||||
chk("the real deadline is under the shortest allowed interval",
|
||||
okta_sync.TOTAL_DEADLINE_SECONDS < okta_sync.INTERVAL_MIN_SECONDS)
|
||||
|
||||
print()
|
||||
print("ALL PASSED" if not fails else f"{fails} FAILED")
|
||||
sys.exit(0 if not fails else 1)
|
||||
|
||||
Reference in New Issue
Block a user