Skip to content

Commit

Permalink
Additional logging surrounding config file writes
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyberboss committed Oct 27, 2024
1 parent a858225 commit 17b2b9a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build/Version.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- Integration tests will ensure they match across the board -->
<Import Project="WebpanelVersion.props" />
<PropertyGroup>
<TgsCoreVersion>6.11.1</TgsCoreVersion>
<TgsCoreVersion>6.11.2</TgsCoreVersion>
<TgsConfigVersion>5.3.0</TgsConfigVersion>
<TgsRestVersion>10.10.0</TgsRestVersion>
<TgsGraphQLVersion>0.2.0</TgsGraphQLVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ await ValueTaskExtensions.WhenAll(entries.Select<string, ValueTask>(async file =
await EnsureDirectories(cancellationToken);
var path = ValidateConfigRelativePath(configurationRelativePath);

logger.LogTrace("Starting write to {path}", path);

ConfigurationFileResponse? result = null;

void WriteImpl()
Expand All @@ -476,14 +478,21 @@ async Task UploadHandler()
await using (fileTicket)
{
var fileHash = previousHash;
logger.LogTrace("Write to {path} waiting for upload stream", path);
var uploadStream = await fileTicket.GetResult(uploadCancellationToken);
if (uploadStream == null)
{
logger.LogTrace("Write to {path} expired", path);
return; // expired
}

logger.LogTrace("Write to {path} received stream of length {length}...", path, uploadStream.Length);
bool success = false;
void WriteCallback()
{
logger.LogTrace("Running synchronous write...");
success = synchronousIOManager.WriteFileChecked(path, uploadStream, ref fileHash, cancellationToken);
logger.LogTrace("Finished write {un}successfully!", success ? String.Empty : "un");
}

if (fileTicket == null)
Expand Down
30 changes: 30 additions & 0 deletions src/Tgstation.Server.Host/IO/SynchronousIOManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@
using System.Security.Cryptography;
using System.Threading;

using Microsoft.Extensions.Logging;

namespace Tgstation.Server.Host.IO
{
/// <inheritdoc />
sealed class SynchronousIOManager : ISynchronousIOManager
{
/// <summary>
/// The <see cref="ILogger"/> for the <see cref="SynchronousIOManager"/>.
/// </summary>
readonly ILogger<SynchronousIOManager> logger;

/// <summary>
/// Initializes a new instance of the <see cref="SynchronousIOManager"/> class.
/// </summary>
/// <param name="logger">The value of <see cref="logger"/>.</param>
public SynchronousIOManager(ILogger<SynchronousIOManager> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

/// <inheritdoc />
public bool CreateDirectory(string path, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -85,13 +101,18 @@ public bool WriteFileChecked(string path, Stream data, ref string? sha1InOut, Ca

cancellationToken.ThrowIfCancellationRequested();

logger.LogTrace("Starting checked write to {path} ({fileType} file)", path, newFile ? "New" : "Pre-existing");

using (var file = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
cancellationToken.ThrowIfCancellationRequested();

// no sha1? no write
if (file.Length != 0 && sha1InOut == null)
{
logger.LogDebug("Aborting checked write due to missing SHA!");
return false;
}

// suppressed due to only using for consistency checks
using (var sha1 = SHA1.Create())
Expand All @@ -109,6 +130,10 @@ public bool WriteFileChecked(string path, Stream data, ref string? sha1InOut, Ca
}

var originalSha1 = GetSha1(file);

if (!newFile)
logger.LogTrace("Existing SHA calculated to be {sha}", originalSha1);

if (originalSha1 != sha1InOut && !(newFile && sha1InOut == null))
{
sha1InOut = originalSha1;
Expand All @@ -122,6 +147,7 @@ public bool WriteFileChecked(string path, Stream data, ref string? sha1InOut, Ca

if (data.Length != 0)
{
logger.LogDebug("Writing file of length {size}", data.Length);
file.Seek(0, SeekOrigin.Begin);
data.Seek(0, SeekOrigin.Begin);

Expand All @@ -132,7 +158,11 @@ public bool WriteFileChecked(string path, Stream data, ref string? sha1InOut, Ca
}

if (data.Length == 0)
{
logger.LogDebug("Stream is empty, deleting file");
File.Delete(path);
}

return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task TestListOrdering()

var configuration = new Configuration(
ioManager,
new SynchronousIOManager(),
new SynchronousIOManager(loggerFactory.CreateLogger<SynchronousIOManager>()),
Mock.Of<IFilesystemLinkFactory>(),
Mock.Of<IProcessExecutor>(),
Mock.Of<IPostWriteHandler>(),
Expand Down

0 comments on commit 17b2b9a

Please sign in to comment.