Moved temporary files into a folder of their own below the data directory (#1245)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-29 18:45:34 +01:00
1 parent ddfc632b63
commit 12d3e1ec6c
52 files changed
+1219 -396

No files matched your search

@@ -44,15 +44,24 @@ public static partial class PluginFactory
var downloadUrl = $"{serverUrl}/{configPlugId}.zip";
LOG.LogInformation($"Try to download configuration plugin with ID='{configPlugId}' from server='{configServerUrl}' (GET {downloadUrl})");
var tempDownloadFile = Path.GetTempFileName();
var stagedDirectory = Path.Join(ENTERPRISE_CONFIGURATION_PLUGINS_ROOT, $"{configPlugId}.staging-{Guid.NewGuid():N}");
string? downloadDirectory = null;
string? backupDirectory = null;
var wasSuccessful = false;
try
{
await LockHotReloadAsync();
// In the temporary folder of AI Studio, below the data directory: on the drive of the plugins, so the new configuration can move there:
downloadDirectory = TemporaryFiles.CreateDirectory();
var tempDownloadFile = Path.Join(downloadDirectory, $"{configPlugId}.zip");
var stagedDirectory = Path.Join(downloadDirectory, configPlugId.ToString());
using var httpClient = ExternalHttpClientTimeout.CreateHttpClient(ExternalHttpTrustPolicy.ALLOW_CUSTOM_ROOTS_WHEN_HOST_WHITELISTED);
var response = await httpClient.GetAsync(downloadUrl, cancellationToken);
// Only the headers, so the archive goes to the disk instead of into memory. The time limit of the client
// then covers the headers alone, so the token keeps it for the whole download:
using var downloadTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
downloadTimeout.CancelAfter(httpClient.Timeout);
using var response = await httpClient.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead, downloadTimeout.Token);
if (!response.IsSuccessStatusCode)
{
LOG.LogError($"Failed to download the enterprise configuration plugin. HTTP Status: {response.StatusCode}");
@@ -61,7 +70,7 @@ public static partial class PluginFactory
await using(var tempFileStream = File.Create(tempDownloadFile))
{
await response.Content.CopyToAsync(tempFileStream, cancellationToken);
await response.Content.CopyToAsync(tempFileStream, downloadTimeout.Token);
}
await ExtractConfigPluginArchiveAsync(tempDownloadFile, stagedDirectory, cancellationToken);
@@ -73,7 +82,7 @@ public static partial class PluginFactory
Directory.Move(configDirectory, backupDirectory);
}
Directory.Move(stagedDirectory, configDirectory);
MoveDeploymentIntoPlace(stagedDirectory, configDirectory);
if (!string.IsNullOrWhiteSpace(backupDirectory) && Directory.Exists(backupDirectory))
Directory.Delete(backupDirectory, true);
@@ -99,36 +108,39 @@ public static partial class PluginFactory
}
finally
{
if (Directory.Exists(stagedDirectory))
if (downloadDirectory is not null && Directory.Exists(downloadDirectory))
{
try
{
Directory.Delete(stagedDirectory, true);
Directory.Delete(downloadDirectory, true);
}
catch (Exception e)
{
LOG.LogError(e, "Failed to delete the staged configuration plugin directory.");
LOG.LogError(e, "Failed to delete the downloaded configuration plugin. The next start of AI Studio deletes it.");
}
}
if (File.Exists(tempDownloadFile))
{
try
{
File.Delete(tempDownloadFile);
}
catch (Exception e)
{
LOG.LogError(e, "Failed to delete the temporary download file.");
}
}
UnlockHotReload();
}
return wasSuccessful;
}
/// <summary>
/// Moves a downloaded deployment of an organization from the temporary folder of AI Studio to its place.
/// </summary>
/// <remarks>
/// Unlike unpacking, Directory.Move creates no missing folders above its destination. The folder of the
/// deployments is missing on a new installation until the first deployment arrives, and once somebody deleted it.
/// </remarks>
/// <param name="stagedDirectory">The unpacked deployment.</param>
/// <param name="deploymentDirectory">The directory of the deployment, named after its configuration ID. It must not exist.</param>
internal static void MoveDeploymentIntoPlace(string stagedDirectory, string deploymentDirectory)
{
Directory.CreateDirectory(Path.GetDirectoryName(deploymentDirectory)!);
Directory.Move(stagedDirectory, deploymentDirectory);
}
private static async Task ExtractConfigPluginArchiveAsync(string sourceArchiveFileName, string destinationDirectory, CancellationToken token)
{
await PluginArchive.ExtractAsync(sourceArchiveFileName, destinationDirectory, token);
@@ -29,8 +29,12 @@ public static partial class PluginFactory
{
var directoryName = Path.GetFileName(configurationDirectory);
// A download in flight stages and backs up next to the configuration directories. Those
// directories belong to a running update, not to a withdrawn configuration:
//
// A download in flight backs up the previous configuration next to the configuration
// directories. Such a directory belongs to a running update, not to a withdrawn
// configuration. Earlier versions also staged the new configuration here; what they left
// behind goes the way of every other directory which is no configuration:
//
if (IsTransientDownloadDirectory(directoryName))
continue;
@@ -50,9 +54,7 @@ public static partial class PluginFactory
/// Checks whether a directory below the enterprise configuration directory belongs to a running
/// download instead of to an installed configuration.
/// </summary>
private static bool IsTransientDownloadDirectory(string directoryName) =>
directoryName.Contains(".staging-", StringComparison.OrdinalIgnoreCase) ||
directoryName.Contains(".backup-", StringComparison.OrdinalIgnoreCase);
private static bool IsTransientDownloadDirectory(string directoryName) => directoryName.Contains(".backup-", StringComparison.OrdinalIgnoreCase);
/// <summary>
/// Unloads every plugin stored in the given directory and deletes the directory afterwards.