2025-06-01 19:14:21 +00:00
using AIStudio.Tools.PluginSystem ;
namespace AIStudio.Tools.Services ;
2025-06-02 18:08:25 +00:00
public sealed class EnterpriseEnvironmentService ( ILogger < EnterpriseEnvironmentService > logger , RustService rustService ) : BackgroundService
2025-06-01 19:14:21 +00:00
{
public static EnterpriseEnvironment CURRENT_ENVIRONMENT ;
2025-06-02 18:08:25 +00:00
#if DEBUG
private static readonly TimeSpan CHECK_INTERVAL = TimeSpan . FromMinutes ( 6 ) ;
#else
2025-06-01 19:14:21 +00:00
private static readonly TimeSpan CHECK_INTERVAL = TimeSpan . FromMinutes ( 16 ) ;
2025-06-02 18:08:25 +00:00
#endif
2025-06-01 19:14:21 +00:00
#region Overrides of BackgroundService
protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
{
logger . LogInformation ( "The enterprise environment service was initialized." ) ;
2025-06-02 18:08:25 +00:00
await this . StartUpdating ( isFirstRun : true ) ;
2025-06-01 19:14:21 +00:00
while ( ! stoppingToken . IsCancellationRequested )
{
await Task . Delay ( CHECK_INTERVAL , stoppingToken ) ;
await this . StartUpdating ( ) ;
}
}
#endregion
2025-06-02 18:08:25 +00:00
private async Task StartUpdating ( bool isFirstRun = false )
2025-06-01 19:14:21 +00:00
{
try
{
2025-06-27 18:52:33 +00:00
logger . LogInformation ( "Start updating of the enterprise environment." ) ;
2025-06-01 19:14:21 +00:00
2026-01-24 19:17:35 +00:00
Guid enterpriseRemoveConfigId ;
try
{
enterpriseRemoveConfigId = await rustService . EnterpriseEnvRemoveConfigId ( ) ;
}
catch ( Exception e )
{
logger . LogError ( e , "Failed to fetch the enterprise remove configuration ID from the Rust service." ) ;
return ;
}
2025-06-01 19:14:21 +00:00
var isPlugin2RemoveInUse = PluginFactory . AvailablePlugins . Any ( plugin = > plugin . Id = = enterpriseRemoveConfigId ) ;
if ( enterpriseRemoveConfigId ! = Guid . Empty & & isPlugin2RemoveInUse )
{
2026-01-24 19:17:35 +00:00
logger . LogWarning ( "The enterprise environment configuration ID '{EnterpriseRemoveConfigId}' must be removed." , enterpriseRemoveConfigId ) ;
2025-06-01 19:14:21 +00:00
PluginFactory . RemovePluginAsync ( enterpriseRemoveConfigId ) ;
}
2026-01-24 19:17:35 +00:00
string? enterpriseConfigServerUrl ;
try
{
enterpriseConfigServerUrl = await rustService . EnterpriseEnvConfigServerUrl ( ) ;
}
catch ( Exception e )
{
logger . LogError ( e , "Failed to fetch the enterprise configuration server URL from the Rust service." ) ;
return ;
}
Guid enterpriseConfigId ;
try
{
enterpriseConfigId = await rustService . EnterpriseEnvConfigId ( ) ;
}
catch ( Exception e )
{
logger . LogError ( e , "Failed to fetch the enterprise configuration ID from the Rust service." ) ;
return ;
}
2025-06-02 18:08:25 +00:00
var etag = await PluginFactory . DetermineConfigPluginETagAsync ( enterpriseConfigId , enterpriseConfigServerUrl ) ;
var nextEnterpriseEnvironment = new EnterpriseEnvironment ( enterpriseConfigServerUrl , enterpriseConfigId , etag ) ;
2025-06-01 19:14:21 +00:00
if ( CURRENT_ENVIRONMENT ! = nextEnterpriseEnvironment )
{
logger . LogInformation ( "The enterprise environment has changed. Updating the current environment." ) ;
CURRENT_ENVIRONMENT = nextEnterpriseEnvironment ;
switch ( enterpriseConfigServerUrl )
{
case null when enterpriseConfigId = = Guid . Empty :
2025-06-02 18:22:15 +00:00
case not null when string . IsNullOrWhiteSpace ( enterpriseConfigServerUrl ) & & enterpriseConfigId = = Guid . Empty :
2025-06-01 19:14:21 +00:00
logger . LogInformation ( "AI Studio runs without an enterprise configuration." ) ;
break ;
case null :
2026-01-24 19:17:35 +00:00
logger . LogWarning ( "AI Studio runs with an enterprise configuration id ('{EnterpriseConfigId}'), but the configuration server URL is not set." , enterpriseConfigId ) ;
2025-06-01 19:14:21 +00:00
break ;
2025-06-02 18:22:15 +00:00
case not null when ! string . IsNullOrWhiteSpace ( enterpriseConfigServerUrl ) & & enterpriseConfigId = = Guid . Empty :
2026-01-24 19:17:35 +00:00
logger . LogWarning ( "AI Studio runs with an enterprise configuration server URL ('{EnterpriseConfigServerUrl}'), but the configuration ID is not set." , enterpriseConfigServerUrl ) ;
2025-06-01 19:14:21 +00:00
break ;
default :
2026-01-24 19:17:35 +00:00
logger . LogInformation ( "AI Studio runs with an enterprise configuration id ('{EnterpriseConfigId}') and configuration server URL ('{EnterpriseConfigServerUrl}')." , enterpriseConfigId , enterpriseConfigServerUrl ) ;
2025-06-02 18:08:25 +00:00
if ( isFirstRun )
MessageBus . INSTANCE . DeferMessage ( null , Event . STARTUP_ENTERPRISE_ENVIRONMENT , new EnterpriseEnvironment ( enterpriseConfigServerUrl , enterpriseConfigId , etag ) ) ;
else
await PluginFactory . TryDownloadingConfigPluginAsync ( enterpriseConfigId , enterpriseConfigServerUrl ) ;
2025-06-01 19:14:21 +00:00
break ;
}
}
2025-06-02 18:08:25 +00:00
else
logger . LogInformation ( "The enterprise environment has not changed. No update required." ) ;
2025-06-01 19:14:21 +00:00
}
catch ( Exception e )
{
logger . LogError ( e , "An error occurred while updating the enterprise environment." ) ;
}
}
}