From 6e7a79952de5101683fddfc1d9cf8e54daf4e52a Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 6 Sep 2026 10:44:00 +0900 Subject: [PATCH] =?UTF-8?q?YMM4=E3=81=AE=E3=83=A6=E3=83=BC=E3=82=B6?= =?UTF-8?q?=E3=83=BC=E9=A0=98=E5=9F=9F=E3=81=AB=E7=BD=AE=E3=81=8F=E9=80=81?= =?UTF-8?q?=E4=BF=A1=E5=BE=85=E3=81=A1=E3=81=AE=E9=80=80=E9=81=BF=E9=A0=98?= =?UTF-8?q?=E5=9F=9F=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Telemetry/TelemetrySpool.cs | 127 ++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Telemetry/TelemetrySpool.cs diff --git a/Telemetry/TelemetrySpool.cs b/Telemetry/TelemetrySpool.cs new file mode 100644 index 0000000..db15346 --- /dev/null +++ b/Telemetry/TelemetrySpool.cs @@ -0,0 +1,127 @@ +using System.IO; +using YukkuriMovieMaker.Commons; + +namespace Telemetry; + +internal sealed class TelemetrySpool +{ + private const string DirectoryName = "telemetry"; + private const string FileExtension = ".envelope"; + private const string TemporaryExtension = ".tmp"; + private const string SearchPattern = "*" + FileExtension; + public const int MaxFiles = 50; + private const long MaxTotalBytes = 2L * 1024L * 1024L; + + private readonly string _directory; + + public TelemetrySpool(string directory) + { + ArgumentException.ThrowIfNullOrWhiteSpace(directory); + _directory = directory; + } + + public static TelemetrySpool Default { get; } = new(Path.Combine(AppDirectories.UserDirectory, DirectoryName)); + + public string? TryWrite(string eventIdentifier, byte[] envelope) + { + ArgumentException.ThrowIfNullOrWhiteSpace(eventIdentifier); + ArgumentNullException.ThrowIfNull(envelope); + + try + { + Directory.CreateDirectory(_directory); + + var path = Path.Combine(_directory, eventIdentifier + FileExtension); + var temporary = path + TemporaryExtension; + + File.WriteAllBytes(temporary, envelope); + File.Move(temporary, path, true); + + Trim(); + return path; + } + catch (Exception) + { + return null; + } + } + + public IReadOnlyList List() + { + try + { + if (!Directory.Exists(_directory)) + { + return []; + } + + return [.. Directory.EnumerateFiles(_directory, SearchPattern) + .Select(static path => new FileInfo(path)) + .OrderBy(static file => file.CreationTimeUtc) + .Select(static file => file.FullName)]; + } + catch (Exception) + { + return []; + } + } + + public byte[]? TryRead(string path) + { + ArgumentException.ThrowIfNullOrWhiteSpace(path); + + try + { + return File.ReadAllBytes(path); + } + catch (Exception) + { + return null; + } + } + + public void TryDelete(string path) + { + ArgumentException.ThrowIfNullOrWhiteSpace(path); + + try + { + File.Delete(path); + } + catch (Exception) + { + } + } + + public void Clear() + { + foreach (var path in List()) + { + TryDelete(path); + } + } + + private void Trim() + { + try + { + var files = Directory.EnumerateFiles(_directory, SearchPattern) + .Select(static path => new FileInfo(path)) + .OrderBy(static file => file.CreationTimeUtc) + .ToList(); + + var total = files.Sum(static file => file.Length); + var index = 0; + + while (index < files.Count && (files.Count - index > MaxFiles || total > MaxTotalBytes)) + { + total -= files[index].Length; + TryDelete(files[index].FullName); + index++; + } + } + catch (Exception) + { + } + } +}