A light or deep analysis read its frames at the start and, under the sequence lock, recorded AnalyzedLight, the warning count and the advisor's Auto values for them whatever happened meanwhile. Frames added by an upload, an import or a scan's reconcile then kept advice and warnings counted on the old frame count, over a newer light analysis, and scans never queued another one because AnalyzedLight was set again. Under the lock the analysis now compares the row's frames version with the one it read. When it moved and no light analysis is queued for the sequence, it records nothing (a deep pass keeps only analysis.json and its per-frame measurements), and the job logs it and queues a light analysis of the frames as they are. A queued light analysis replaces what the pass records, as for image passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
109 lines
6.1 KiB
C#
109 lines
6.1 KiB
C#
using System.Globalization;
|
|
using Titano.Core.Analysis.Deep;
|
|
using Titano.Core.Archiving;
|
|
using Titano.Core.Localization;
|
|
using Titano.Core.Model;
|
|
using Titano.Data.Jobs;
|
|
|
|
namespace Titano.Data.Processing.Jobs;
|
|
|
|
/// <summary>
|
|
/// <see cref="JobKind.LightAnalyse"/> and <see cref="JobKind.DeepAnalyse"/> over the target sequences, one after the
|
|
/// other. Progress counts sequences (light) or frames (deep). A sequence that cannot be analysed is logged and the
|
|
/// others still run; the job then fails, and a retry analyses only the failed ones. An offline archive stops the job at
|
|
/// once. Checkpoint: the next target and the failed ones (<c>next[;i,j…]</c>). A deep pass interrupted inside a
|
|
/// sequence resumes from that sequence's <c>analysis.partial</c>.
|
|
/// </summary>
|
|
internal sealed class AnalyseJobHandler(TitanoDatabase db, SequenceAnalyser analyser, JobKind kind) : IJobHandler
|
|
{
|
|
public JobKind Kind => kind;
|
|
|
|
public async Task RunAsync(JobContext context)
|
|
{
|
|
var deep = kind == JobKind.DeepAnalyse;
|
|
var targets = context.Job.TargetIds;
|
|
var (next, retry) = Parse(context.Checkpoint, targets.Count);
|
|
// Deep progress is in frames across all targets, so the bar and the ETA cover the whole job.
|
|
var units = new long[targets.Count];
|
|
for (var i = 0; i < targets.Count; i++)
|
|
units[i] = deep ? (await db.Sequences.GetAsync(targets[i], context.Cancellation).ConfigureAwait(false))?.FrameCount ?? 0 : 1;
|
|
var total = units.Sum();
|
|
var done = Enumerable.Range(0, next).Where(i => !retry.Contains(i)).Sum(i => units[i]);
|
|
var failed = new List<int>();
|
|
string? failure = null;
|
|
int[] order = [.. retry, .. Enumerable.Range(next, targets.Count - next)];
|
|
for (var k = 0; k < order.Length; k++)
|
|
{
|
|
context.Cancellation.ThrowIfCancellationRequested();
|
|
var i = order[k];
|
|
var before = done;
|
|
var chain = new ProgressChain(context.ReportAsync);
|
|
var progress = deep ? chain.For<AnalysisProgress>(p => (before + p.Done, total, null)) : null;
|
|
try
|
|
{
|
|
var outcome = await analyser.AnalyseAsync(targets[i], deep, progress, context.Cancellation).ConfigureAwait(false);
|
|
await chain.FlushAsync().ConfigureAwait(false);
|
|
if (outcome is null) context.Log(Loc.L("Archive.Log.SequenceMissing", targets[i]));
|
|
else if (outcome.FramesChanged) await AnalyseAgainAsync(context, outcome.Sequence).ConfigureAwait(false);
|
|
else Report(context, outcome, deep);
|
|
}
|
|
catch (Exception ex) when (ex is not (OperationCanceledException or ArchiveOfflineException))
|
|
{
|
|
await chain.FlushAsync().ConfigureAwait(false);
|
|
Core.Diagnostics.Log.Warning("processing", $"Analysis of sequence {targets[i]} failed", ex);
|
|
failure = Loc.L("Advisor.Log.Failed", await TitleAsync(targets[i], context.Cancellation).ConfigureAwait(false), ex.Message);
|
|
context.Log(failure);
|
|
failed.Add(i);
|
|
}
|
|
done += units[i];
|
|
next = int.Max(next, i + 1);
|
|
// Failed targets of an earlier attempt not retried yet stay listed.
|
|
await context.CheckpointAsync(Format(next, [.. failed, .. retry.Skip(k + 1)]), done, total).ConfigureAwait(false);
|
|
}
|
|
// One failure is the job's error as it was logged; several are counted (the log names each).
|
|
if (failed.Count > 0) throw new InvalidOperationException(failed.Count == 1 ? failure : Loc.L("Errors.Analysis.Failed", failed.Count, targets.Count));
|
|
}
|
|
|
|
/// <summary>
|
|
/// A sequence whose frames changed while it was analysed, with no light analysis queued for it: nothing was recorded
|
|
/// from the frames the analysis read, so a light analysis of the frames as they are is queued (the deep report it saved
|
|
/// counts only for the frames it describes).
|
|
/// </summary>
|
|
private async Task AnalyseAgainAsync(JobContext context, Sequence sequence)
|
|
{
|
|
context.Log(Loc.L("Advisor.Log.FramesChanged", sequence.DisplayTitle));
|
|
await AnalysisFollowUp.QueueAsync(db, context, [sequence.Id], sequence.DisplayTitle).ConfigureAwait(false);
|
|
}
|
|
|
|
private async Task<string> TitleAsync(Guid sequenceId, CancellationToken cancellationToken) =>
|
|
(await db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false))?.DisplayTitle ?? sequenceId.ToString();
|
|
|
|
private static void Report(JobContext context, AnalysisOutcome outcome, bool deep)
|
|
{
|
|
var title = outcome.Sequence.DisplayTitle;
|
|
context.Log(Loc.L(deep ? "Advisor.Log.DeepDone" : "Advisor.Log.LightDone", title, outcome.Frames, outcome.Warnings));
|
|
if (outcome.SettingsChanged) context.Log(Loc.L("Advisor.Log.AutoUpdated", title));
|
|
if (outcome.SettingsUnreadable) context.Log(Loc.L("Advisor.Log.ProjectUnreadable", title));
|
|
if (outcome.Presets.Count > 0) context.Log(Loc.L("Advisor.Log.Presets", string.Join(" · ", outcome.Presets.Select(p => p.DisplayName()))));
|
|
}
|
|
|
|
/// <summary>Next target and the failed targets to retry first; a missing or unreadable checkpoint starts over.</summary>
|
|
internal static (int Next, IReadOnlyList<int> Retry) Parse(string? checkpoint, int count)
|
|
{
|
|
var parts = checkpoint?.Split(';');
|
|
if (parts is not ([_] or [_, _]) || !int.TryParse(parts[0], NumberStyles.None, CultureInfo.InvariantCulture, out var next) || next > count) return (0, []);
|
|
var retry = new List<int>();
|
|
foreach (var item in parts.Length == 2 ? parts[1].Split(',') : [])
|
|
{
|
|
if (!int.TryParse(item, NumberStyles.None, CultureInfo.InvariantCulture, out var index) || index >= next) return (0, []);
|
|
if (!retry.Contains(index)) retry.Add(index);
|
|
}
|
|
return (next, retry);
|
|
}
|
|
|
|
internal static string Format(int next, IReadOnlyCollection<int> failed) =>
|
|
failed.Count == 0
|
|
? next.ToString(CultureInfo.InvariantCulture)
|
|
: string.Create(CultureInfo.InvariantCulture, $"{next};{string.Join(',', failed.Order())}");
|
|
}
|