Data Sync & Cache¶
Overview¶
Data synchronisation is handled by sync_worker.py. It runs as a scheduled Windows task (nightly) and builds the JSON cache that the FastAPI backend uses to answer questions.
SQL Server master DB (Clubs table)
│
├── boolChatbot = 1 → Club included in sync
│
▼
Per club: query 44 allowed tables
│
▼
cache/{clubFolder}_context.json
sync_worker.py – Detailed Flow¶
- Connects to the master database with pyodbc parameters from
.env - Reads the
Clubstable – fetches all rows whereboolChatbot = 1 - For each club:
- Connects to the club's own database
- Queries 44 pre-approved tables (news, activities, course info, price lists, etc.)
- Aggregates the results into a single Python dict
- Writes
cache/{clubFolder}_context.json
Allowed Tables (44 total)¶
The sync worker uses an explicit allowlist (ALLOWED_TABLES) to prevent unintended data from being exposed to the AI model. Examples:
| Table | Content |
|---|---|
tblNews |
News articles |
tblActivities |
Activities and events |
tblCourseInfo |
Course information |
tblPrices |
Price list |
tblContacts |
Contact information |
tblOpeningHours |
Opening hours |
AI_Chatbot_Settings |
Per-club chatbot settings |
Cache Format¶
The file cache/{clubFolder}_context.json has the following top-level structure:
{
"club_id": "SKI",
"synced_at": "2026-05-02T03:00:00Z",
"tables": {
"tblNews": [
{"id": 1, "title": "Summer open!", "body": "..."},
...
],
"tblActivities": [...],
...
}
}
Scheduled Execution (Windows Task Scheduler)¶
| Parameter | Value |
|---|---|
| Script | API\chatbot\sync_worker.py |
| Executor | Python venv: API\python\venv\Scripts\python.exe |
| Trigger | Daily at 03:00 |
| Working directory | C:\ftp\sites\clubsite-4\API\chatbot |
Run manually:
Adding a New Tenant to Sync¶
- Set
boolChatbot = 1for the club in theClubstable in the master database - Verify the
clubFolderfield (used as the cache filename) - Run
sync_worker.pymanually - Confirm that
cache/{clubFolder}_context.jsonhas been created - Update
ALLOWED_TENANT_IDinapp.py(temporary – removed in v2.1)
Future improvement
The v2.1 plan removes the ALLOWED_TENANT_ID restriction. The API will then automatically load the cache for any tenant with a valid cache file.