E4: «Salva il fotogramma» ripreso dopo una pausa non salva più l'istante vecchio
Il lavoro registrava lo SHA-256 del file nuovo prima di scriverlo: se il disco se ne andava a metà, alla ripresa il file sembrava non cambiato e si salvava l'istante scelto sul file di prima. L'elemento tiene lo SHA-256 sul quale il suo istante è stato scelto. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cf52e0ea6a
commit
9c2d237ec9
2 files changed
+76
-17
No files matched your search
@@ -591,7 +591,13 @@ public sealed class ExportCopier(ExportEnvironment env)
|
|||||||
chosen = at;
|
chosen = at;
|
||||||
}
|
}
|
||||||
|
|
||||||
batch = batch.Select(i => string.Equals(i.SourceSha256, sha, StringComparison.Ordinal) ? i : i with { SourceSha256 = sha }).ToList();
|
if (mode != FrameMode.Still)
|
||||||
|
{
|
||||||
|
// A «Still» item keeps the SHA-256 its instant was chosen on: a job that pauses once the item is written again
|
||||||
|
// (the disk went in the middle of the file) must still see that the file changed.
|
||||||
|
batch = batch.Select(i => string.Equals(i.SourceSha256, sha, StringComparison.Ordinal) ? i : i with { SourceSha256 = sha }).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
var format = batch[0].Format;
|
var format = batch[0].Format;
|
||||||
var firstId = mode == FrameMode.Count
|
var firstId = mode == FrameMode.Count
|
||||||
? await exports.FirstItemIdAsync(x.Run.Id, library.Media.Id, ExportVariant.Frame, format, ct).ConfigureAwait(false) ?? batch[0].Id
|
? await exports.FirstItemIdAsync(x.Run.Id, library.Media.Id, ExportVariant.Frame, format, ct).ConfigureAwait(false) ?? batch[0].Id
|
||||||
|
|||||||
@@ -346,37 +346,70 @@ public sealed class ExportFramesTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
[DataRow(null)]
|
[DataRow(null, false, false)]
|
||||||
[DataRow(VideoAnalysisOutcome.Moving)]
|
[DataRow(VideoAnalysisOutcome.Moving, false, false)]
|
||||||
[DataRow(VideoAnalysisOutcome.Still)]
|
[DataRow(VideoAnalysisOutcome.Still, false, false)]
|
||||||
public async Task AFrameWaitingForAFileThatChangedIsTheOneOfTheNewAnalysisOrNone(VideoAnalysisOutcome? now)
|
[DataRow(VideoAnalysisOutcome.Still, true, false)]
|
||||||
|
[DataRow(VideoAnalysisOutcome.Still, false, true)]
|
||||||
|
[DataRow(VideoAnalysisOutcome.Still, true, true)]
|
||||||
|
[DataRow(VideoAnalysisOutcome.Moving, true, true)]
|
||||||
|
public async Task AFrameWaitingForAFileThatChangedIsTheOneOfTheNewAnalysisOrNone(VideoAnalysisOutcome? now, bool pausedWhileWriting, bool changedTwice)
|
||||||
{
|
{
|
||||||
// The job waited (disk full) while the file was replaced: the instant was chosen on the old one, so it would save
|
// The job waited (disk full) while the file was replaced: the instant was chosen on the old one, so it would save
|
||||||
// any frame of the new one, whatever it is.
|
// any frame of the new one, whatever it is. Then the disk may go in the middle of the writing, once the item is
|
||||||
|
// recorded again: resumed, the job must still know that the file changed (and it may change once more).
|
||||||
var ct = TestContext.CancellationToken;
|
var ct = TestContext.CancellationToken;
|
||||||
await using var bed = await ExportTestBed.CreateAsync();
|
var files = new AwayWhileHashing();
|
||||||
|
await using var bed = await ExportTestBed.CreateAsync(e => e with { FileSystem = files });
|
||||||
var video = (await SaveVideoAsync(bed, bed.Marta, "984", Mp4, ".mp4")).Media[0];
|
var video = (await SaveVideoAsync(bed, bed.Marta, "984", Mp4, ".mp4")).Media[0];
|
||||||
var usb = await bed.GeneralAsync(ct: ct);
|
var usb = await bed.GeneralAsync(ct: ct);
|
||||||
await bed.Db.VideoAnalyses.SetAsync(new VideoAnalysis(video.Sha256!, 1, VideoAnalysisOutcome.Still, Make.T0, TimeSpan.FromMilliseconds(400)), ct);
|
await bed.Db.VideoAnalyses.SetAsync(new VideoAnalysis(video.Sha256!, 1, VideoAnalysisOutcome.Still, Make.T0, TimeSpan.FromMilliseconds(400)), ct);
|
||||||
bed.Disk.Free = 0;
|
bed.Disk.Free = 0;
|
||||||
var start = await bed.CopyAsync(Frames(video, new ExportFramesRequest(FrameMode.Still, null, null, null, ExportFormat.Png)), ct);
|
var start = await bed.CopyAsync(Frames(video, new ExportFramesRequest(FrameMode.Still, null, null, null, ExportFormat.Png)), ct);
|
||||||
|
|
||||||
byte[] other = [.. Mp4, 0, 0, 0, 8, (byte)'f', (byte)'r', (byte)'e', (byte)'e'];
|
async Task<string> ReplaceAsync(byte[] file, VideoAnalysisOutcome? outcome, TimeSpan instant)
|
||||||
var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(other));
|
|
||||||
await File.WriteAllBytesAsync(bed.Library.Layout.FullPath(bed.Marta, video.RelativePath!), other, ct);
|
|
||||||
await bed.Db.Media.UpdateAsync((await bed.Db.Media.GetAsync(video.Id, ct))! with { Sha256 = sha, Bytes = other.Length }, ct);
|
|
||||||
var at = TimeSpan.FromMilliseconds(100);
|
|
||||||
if (now is { } outcome)
|
|
||||||
{
|
{
|
||||||
await bed.Db.VideoAnalyses.SetAsync(new VideoAnalysis(sha, 1, outcome, Make.T0, outcome == VideoAnalysisOutcome.Still ? at : null), ct);
|
var hash = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(file));
|
||||||
|
await File.WriteAllBytesAsync(bed.Library.Layout.FullPath(bed.Marta, video.RelativePath!), file, ct);
|
||||||
|
await bed.Db.Media.UpdateAsync((await bed.Db.Media.GetAsync(video.Id, ct))! with { Sha256 = hash, Bytes = file.Length }, ct);
|
||||||
|
if (outcome is { } found)
|
||||||
|
{
|
||||||
|
await bed.Db.VideoAnalyses.SetAsync(new VideoAnalysis(hash, 1, found, Make.T0, found == VideoAnalysisOutcome.Still ? instant : null), ct);
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
byte[] other = [.. Mp4, 0, 0, 0, 8, (byte)'f', (byte)'r', (byte)'e', (byte)'e'];
|
||||||
|
var at = TimeSpan.FromMilliseconds(100);
|
||||||
|
var sha = await ReplaceAsync(other, pausedWhileWriting ? VideoAnalysisOutcome.Still : now, at);
|
||||||
bed.Disk.Free = null;
|
bed.Disk.Free = null;
|
||||||
await bed.Service.ResumeAsync([start.Runs.Single().Id], ct);
|
var runId = start.Runs.Single().Id;
|
||||||
|
if (pausedWhileWriting)
|
||||||
|
{
|
||||||
|
files.OnHash = () => Directory.Move(bed.Usb, bed.Usb + "-via");
|
||||||
|
await bed.Service.ResumeAsync([runId], ct);
|
||||||
|
await bed.Service.WaitAsync(ct);
|
||||||
|
files.OnHash = null;
|
||||||
|
Directory.Move(bed.Usb + "-via", bed.Usb);
|
||||||
|
Assert.AreEqual(ExportRunState.Paused, (await bed.RunAsync(runId, ct)).State);
|
||||||
|
var waiting = (await bed.ItemsAsync(runId, ct)).Single();
|
||||||
|
Assert.AreEqual(ExportItemState.Pending, waiting.State);
|
||||||
|
Assert.IsNotNull(waiting.RelativePath, "fermo dopo l'annuncio del file da scrivere");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changedTwice)
|
||||||
|
{
|
||||||
|
other = [.. other, 0, 0, 0, 8, (byte)'f', (byte)'r', (byte)'e', (byte)'e'];
|
||||||
|
at = TimeSpan.FromMilliseconds(200);
|
||||||
|
sha = await ReplaceAsync(other, now, at);
|
||||||
|
}
|
||||||
|
|
||||||
|
await bed.Service.ResumeAsync([runId], ct);
|
||||||
await bed.Service.WaitAsync(ct);
|
await bed.Service.WaitAsync(ct);
|
||||||
var run = await bed.RunAsync(start.Runs.Single().Id, ct);
|
var run = await bed.RunAsync(runId, ct);
|
||||||
Assert.AreEqual(ExportRunState.Completed, run.State, run.LastError);
|
Assert.AreEqual(ExportRunState.Completed, run.State, run.LastError);
|
||||||
var item = (await bed.ItemsAsync(start.Runs.Single().Id, ct)).Single();
|
var item = (await bed.ItemsAsync(runId, ct)).Single();
|
||||||
if (now == VideoAnalysisOutcome.Still)
|
if (now == VideoAnalysisOutcome.Still)
|
||||||
{
|
{
|
||||||
var copy = (await bed.Db.Exports.ListCopiesAsync(usb.Id, ct: ct)).Single();
|
var copy = (await bed.Db.Exports.ListCopiesAsync(usb.Id, ct: ct)).Single();
|
||||||
@@ -500,4 +533,24 @@ public sealed class ExportFramesTests
|
|||||||
|
|
||||||
public bool InSession => Interlocked.Decrement(ref _left) >= 0;
|
public bool InSession => Interlocked.Decrement(ref _left) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>The real files, telling when a file is hashed: the moment the disk of the copies goes.</summary>
|
||||||
|
private sealed class AwayWhileHashing : IExportFileSystem
|
||||||
|
{
|
||||||
|
public Action? OnHash { get; set; }
|
||||||
|
|
||||||
|
public Stream OpenRead(string path) => ExportFileSystem.Instance.OpenRead(path);
|
||||||
|
|
||||||
|
public Stream OpenRandomAccess(string path) => ExportFileSystem.Instance.OpenRandomAccess(path);
|
||||||
|
|
||||||
|
public Task<string> ComputeSha256Async(string path, CancellationToken ct)
|
||||||
|
{
|
||||||
|
OnHash?.Invoke();
|
||||||
|
return ExportFileSystem.Instance.ComputeSha256Async(path, ct);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool FileExists(string path) => ExportFileSystem.Instance.FileExists(path);
|
||||||
|
|
||||||
|
public long? FileLength(string path) => ExportFileSystem.Instance.FileLength(path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user