|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using Microsoft.Extensions.Hosting; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | +using OpenDeepWiki.Chat.Providers; |
| 5 | +using OpenDeepWiki.Chat.Routing; |
| 6 | + |
| 7 | +namespace OpenDeepWiki.Chat.Config; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Bridges database configuration to live provider instances. |
| 11 | +/// On startup, loads DB config for all registered IConfigurableProvider providers. |
| 12 | +/// At runtime, subscribes to IConfigChangeNotifier to apply config changes immediately. |
| 13 | +/// </summary> |
| 14 | +public class ProviderConfigApplicator : IHostedService, IDisposable |
| 15 | +{ |
| 16 | + private readonly IMessageRouter _router; |
| 17 | + private readonly IConfigChangeNotifier _changeNotifier; |
| 18 | + private readonly IServiceScopeFactory _scopeFactory; |
| 19 | + private readonly ILogger<ProviderConfigApplicator> _logger; |
| 20 | + private IDisposable? _subscription; |
| 21 | + |
| 22 | + public ProviderConfigApplicator( |
| 23 | + IMessageRouter router, |
| 24 | + IConfigChangeNotifier changeNotifier, |
| 25 | + IServiceScopeFactory scopeFactory, |
| 26 | + ILogger<ProviderConfigApplicator> logger) |
| 27 | + { |
| 28 | + _router = router; |
| 29 | + _changeNotifier = changeNotifier; |
| 30 | + _scopeFactory = scopeFactory; |
| 31 | + _logger = logger; |
| 32 | + } |
| 33 | + |
| 34 | + public async Task StartAsync(CancellationToken cancellationToken) |
| 35 | + { |
| 36 | + _logger.LogInformation("ProviderConfigApplicator starting: applying DB configs to registered providers"); |
| 37 | + |
| 38 | + await ApplyAllDbConfigsAsync(cancellationToken); |
| 39 | + |
| 40 | + // Subscribe to all platform changes for runtime hot-reload |
| 41 | + _subscription = _changeNotifier.Subscribe(null, OnConfigChanged); |
| 42 | + |
| 43 | + _logger.LogInformation("ProviderConfigApplicator started: subscribed to config change notifications"); |
| 44 | + } |
| 45 | + |
| 46 | + public Task StopAsync(CancellationToken cancellationToken) |
| 47 | + { |
| 48 | + _subscription?.Dispose(); |
| 49 | + _subscription = null; |
| 50 | + _logger.LogInformation("ProviderConfigApplicator stopped"); |
| 51 | + return Task.CompletedTask; |
| 52 | + } |
| 53 | + |
| 54 | + public void Dispose() |
| 55 | + { |
| 56 | + _subscription?.Dispose(); |
| 57 | + } |
| 58 | + |
| 59 | + private async Task ApplyAllDbConfigsAsync(CancellationToken cancellationToken) |
| 60 | + { |
| 61 | + using var scope = _scopeFactory.CreateScope(); |
| 62 | + var configService = scope.ServiceProvider.GetRequiredService<IChatConfigService>(); |
| 63 | + |
| 64 | + foreach (var provider in _router.GetAllProviders()) |
| 65 | + { |
| 66 | + if (provider is not IConfigurableProvider configurable) |
| 67 | + continue; |
| 68 | + |
| 69 | + try |
| 70 | + { |
| 71 | + var dbConfig = await configService.GetConfigAsync(provider.PlatformId, cancellationToken); |
| 72 | + if (dbConfig != null) |
| 73 | + { |
| 74 | + configurable.ApplyConfig(dbConfig); |
| 75 | + _logger.LogInformation( |
| 76 | + "Applied DB config to provider {Platform} at startup", provider.PlatformId); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + _logger.LogDebug( |
| 81 | + "No DB config for provider {Platform}, using environment variable defaults", |
| 82 | + provider.PlatformId); |
| 83 | + } |
| 84 | + } |
| 85 | + catch (Exception ex) |
| 86 | + { |
| 87 | + _logger.LogError(ex, |
| 88 | + "Failed to apply DB config to provider {Platform} at startup, falling back to env vars", |
| 89 | + provider.PlatformId); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void OnConfigChanged(ConfigChangeEvent evt) |
| 95 | + { |
| 96 | + // Fire-and-forget with error handling (notification callback is synchronous) |
| 97 | + _ = Task.Run(async () => |
| 98 | + { |
| 99 | + try |
| 100 | + { |
| 101 | + await ApplyConfigForPlatformAsync(evt.Platform, evt.ChangeType); |
| 102 | + } |
| 103 | + catch (Exception ex) |
| 104 | + { |
| 105 | + _logger.LogError(ex, "Failed to apply config change for platform {Platform}", evt.Platform); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + private async Task ApplyConfigForPlatformAsync(string platform, ConfigChangeType changeType) |
| 111 | + { |
| 112 | + var provider = _router.GetProvider(platform); |
| 113 | + if (provider is not IConfigurableProvider configurable) |
| 114 | + { |
| 115 | + _logger.LogDebug("Provider {Platform} does not support runtime config updates", platform); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + if (changeType == ConfigChangeType.Deleted) |
| 120 | + { |
| 121 | + configurable.ResetToDefaults(); |
| 122 | + _logger.LogInformation("Reset provider {Platform} to env var defaults (DB config deleted)", platform); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + using var scope = _scopeFactory.CreateScope(); |
| 127 | + var configService = scope.ServiceProvider.GetRequiredService<IChatConfigService>(); |
| 128 | + var dbConfig = await configService.GetConfigAsync(platform); |
| 129 | + |
| 130 | + if (dbConfig != null) |
| 131 | + { |
| 132 | + configurable.ApplyConfig(dbConfig); |
| 133 | + _logger.LogInformation("Applied updated DB config to provider {Platform} (change: {ChangeType})", |
| 134 | + platform, changeType); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + configurable.ResetToDefaults(); |
| 139 | + _logger.LogInformation("Reset provider {Platform} to env var defaults (DB config not found)", platform); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments