| 11 | const logger = createLogger('config-manager'); |
| 12 | |
| 13 | export class ConfigManager { |
| 14 | private watcher: FSWatcher; |
| 15 | |
| 16 | constructor( |
| 17 | private db: PrismaClient, |
| 18 | private connectionManager: ConnectionManager, |
| 19 | configPath: string, |
| 20 | ) { |
| 21 | this.watcher = chokidar.watch(configPath, { |
| 22 | ignoreInitial: true, // Don't fire events for existing files |
| 23 | awaitWriteFinish: { |
| 24 | stabilityThreshold: 100, // File size stable for 100ms |
| 25 | pollInterval: 100 // Check every 100ms |
| 26 | }, |
| 27 | atomic: true // Handle atomic writes (temp file + rename) |
| 28 | }); |
| 29 | |
| 30 | this.watcher.on('change', async () => { |
| 31 | logger.debug(`Config file ${configPath} changed. Syncing config.`); |
| 32 | try { |
| 33 | await this.syncConfig(configPath); |
| 34 | } catch (error) { |
| 35 | logger.error(`Failed to sync config: ${error}`); |
| 36 | } |
| 37 | }); |
| 38 | |
| 39 | this.syncConfig(configPath); |
| 40 | } |
| 41 | |
| 42 | private syncConfig = async (configPath: string) => { |
| 43 | const config = await loadConfig(configPath); |
| 44 | |
| 45 | await this.syncConnections(config.connections); |
| 46 | await syncSearchContexts({ |
| 47 | contexts: config.contexts, |
| 48 | orgId: SINGLE_TENANT_ORG_ID, |
| 49 | db: this.db, |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | private syncConnections = async (connections?: { [key: string]: ConnectionConfig }) => { |
| 54 | if (connections) { |
| 55 | for (const [key, newConnectionConfig] of Object.entries(connections)) { |
| 56 | const existingConnection = await this.db.connection.findUnique({ |
| 57 | where: { |
| 58 | name_orgId: { |
| 59 | name: key, |
| 60 | orgId: SINGLE_TENANT_ORG_ID, |
| 61 | } |
| 62 | } |
| 63 | }); |
| 64 | |
| 65 | |
| 66 | const existingConnectionConfig = existingConnection ? existingConnection.config as unknown as ConnectionConfig : undefined; |
| 67 | const connectionNeedsSyncing = |
| 68 | !existingConnectionConfig || |
| 69 | !isEqual(existingConnectionConfig, newConnectionConfig); |
| 70 |
nothing calls this directly
no test coverage detected