Owner review 1, phase 4 (P5). A render job now carries the output size its settings give (RenderSize, proportions win) and the settings format it needs, resolved on the server before the job is split, so only nodes that read that format and whose encoder takes the frame claim it, and a job no online node can read no longer waits for one. Nodes and the server announce the format they read; settings of a newer Chronos fail saying so. Encoder size limits are read as long edge x short edge: Media Foundation's software H.264 encoder, announced as 4096x2304, accepts portrait 2160x3840 (probed on this PC), so EncoderSelector and the provider take either orientation and cap a portrait frame against the turned limit. On the desktop, Sync and Send-and-render are disabled with the NeedsNewerServer tooltip while the server reads an older format than the working settings need. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
246 lines
14 KiB
C#
246 lines
14 KiB
C#
using Chronos.Core.Archiving;
|
||
using Chronos.Core.Model;
|
||
using Chronos.Core.Project;
|
||
using Chronos.Data.Archiving;
|
||
|
||
namespace Chronos.Data.Projects;
|
||
|
||
/// <summary>A project's home cannot be reached now (its archive offline or busy, its server out of reach): try again later.</summary>
|
||
public sealed class HomeUnavailableException(string message, Exception? inner = null) : IOException(message, inner);
|
||
|
||
/// <summary>The sequence a home keeps projects for, as a workspace's project file names it.</summary>
|
||
public sealed record HomeSequence(BundleArchive Archive, BundleSequence Sequence, BundleHome Home);
|
||
|
||
/// <summary>
|
||
/// Where a sequence's projects live (owner review 2, R8): the side a workspace pushes to and pulls from, "like git". This
|
||
/// PC's archive is one (<see cref="ArchiveProjectHome"/>); a server is the other (<see cref="ServerProjectHome"/>, phase
|
||
/// 5b). Every call may throw <see cref="HomeUnavailableException"/> when the home cannot be reached, and
|
||
/// <see cref="ProjectException"/> when it refuses.
|
||
/// </summary>
|
||
public interface IProjectHome
|
||
{
|
||
/// <summary>Gives a project another name (1–60 characters, unique among the sequence's live projects whatever the case).</summary>
|
||
/// <exception cref="ProjectException">Unknown project, or the name is invalid or taken.</exception>
|
||
Task<SequenceProject> RenameAsync(Guid projectId, string name, CancellationToken cancellationToken) =>
|
||
throw new NotSupportedException("This home does not rename projects.");
|
||
|
||
/// <summary>Deletes a project (never Main) into the home's project trash, with all its versions, which stay readable there.</summary>
|
||
/// <exception cref="ProjectException">Unknown project, or the Main project.</exception>
|
||
Task DeleteAsync(Guid projectId, CancellationToken cancellationToken) => throw new NotSupportedException("This home does not delete projects.");
|
||
|
||
/// <summary>The sequence and where it is.</summary>
|
||
Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken);
|
||
|
||
/// <summary>The sequence's live projects, Main first (made when the sequence had none).</summary>
|
||
Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken);
|
||
|
||
/// <summary>A new project named <paramref name="name"/> from <paramref name="fromVersionId"/> (a version of any project of the sequence), or from Main's head.</summary>
|
||
Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken);
|
||
|
||
/// <summary>
|
||
/// The project's newest version; a project deleted at the home has none (<see cref="ProjectException"/>,
|
||
/// <see cref="ProjectError.NotFound"/>), so a workspace's copy of it reads as gone, never as in step.
|
||
/// </summary>
|
||
Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken);
|
||
|
||
/// <summary>A version of the project by id; null when the home has none.</summary>
|
||
Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken);
|
||
|
||
/// <summary>
|
||
/// Whether the home holds <paramref name="version"/> durably, read back from where it keeps it and matching its content
|
||
/// hash: what a workspace asks before it drops its own copy. A home that keeps no other copy answers from
|
||
/// <see cref="GetVersionAsync"/>.
|
||
/// </summary>
|
||
async Task<bool> HoldsAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken)
|
||
{
|
||
ArgumentNullException.ThrowIfNull(version);
|
||
return await GetVersionAsync(projectId, version.Id, cancellationToken).ConfigureAwait(false) is { } held
|
||
&& string.Equals(held.ContentHash, version.ContentHash, StringComparison.OrdinalIgnoreCase) && ProjectContent.Verifies(held.SettingsJson, held.ContentHash);
|
||
}
|
||
|
||
/// <summary>The versions numbered above <paramref name="afterNumber"/>, oldest first, at most <paramref name="limit"/>.</summary>
|
||
Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken);
|
||
|
||
/// <summary>One page of the history, newest first: the versions numbered below <paramref name="beforeNumber"/> (from the head when null).</summary>
|
||
Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken);
|
||
|
||
/// <summary>Stores a version made from the head (<see cref="ProjectService.PushAsync"/>).</summary>
|
||
Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken);
|
||
|
||
/// <summary>The sequence's frames as the home holds them now.</summary>
|
||
Task<HomeFrames> FramesAsync(CancellationToken cancellationToken);
|
||
|
||
/// <summary>
|
||
/// A cheap stamp of what <see cref="FramesAsync"/> answers: another value whenever the frames, how their photos are
|
||
/// named (a hash learnt) or the encodings conversions joined may have changed, so a workspace reads the frames again only
|
||
/// then. Null from a home that keeps none: the frames are read every time.
|
||
/// </summary>
|
||
Task<string?> FramesStampAsync(CancellationToken cancellationToken) => Task.FromResult<string?>(null);
|
||
|
||
/// <summary>
|
||
/// The highest project settings format the home stores and renders (owner review 1, phase 4); a version of a higher one
|
||
/// is not pushed to it (an older server refuses it, 422 <c>newer_format</c>). This PC's archive reads what this Chronos does.
|
||
/// </summary>
|
||
int SettingsFormat => ProjectSettings.CurrentVersion;
|
||
}
|
||
|
||
/// <summary>A sequence of this PC's archive as its projects' home: the archive's <see cref="ProjectService"/>.</summary>
|
||
public sealed class ArchiveProjectHome(ChronosDatabase db, ProjectService projects, Guid sequenceId) : IProjectHome
|
||
{
|
||
public Guid SequenceId => sequenceId;
|
||
|
||
public async Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken)
|
||
{
|
||
var sequence = await db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false)
|
||
?? throw new ProjectException(ProjectError.SequenceNotFound, $"Sequence {sequenceId} is not in the index.");
|
||
var archive = await db.Archives.GetAsync(sequence.ArchiveId, cancellationToken).ConfigureAwait(false);
|
||
return new HomeSequence(new BundleArchive(sequence.ArchiveId, archive?.Name ?? string.Empty, archive?.RootPath),
|
||
new BundleSequence(sequence.Id, sequence.Folder, sequence.DisplayTitle, sequence.FrameCount),
|
||
new BundleHome(BundleHome.ArchiveKind, sequence.Id, sequence.ArchiveId));
|
||
}
|
||
|
||
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) =>
|
||
Reach(async () =>
|
||
{
|
||
// Made at once when the sequence has none, so every list has a Main.
|
||
await projects.MainAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
||
return await projects.ListAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
||
});
|
||
|
||
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
|
||
Reach(() => projects.CreateAsync(sequenceId, name, fromVersionId, author, cancellationToken));
|
||
|
||
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) =>
|
||
Reach(async () =>
|
||
{
|
||
await Own(projectId, cancellationToken).ConfigureAwait(false);
|
||
return await projects.HeadAsync(projectId, cancellationToken).ConfigureAwait(false);
|
||
});
|
||
|
||
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
|
||
Reach(async () =>
|
||
{
|
||
if (await projects.GetVersionAsync(versionId, cancellationToken).ConfigureAwait(false) is not { } version)
|
||
{
|
||
// A version the index does not know yet may be in the sequence's sidecars (see Own): they are read first.
|
||
await projects.ListAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
||
version = await projects.GetVersionAsync(versionId, cancellationToken).ConfigureAwait(false);
|
||
}
|
||
return version is not null && version.ProjectId == projectId ? version : null;
|
||
});
|
||
|
||
/// <summary>Read back from the version's file in the archive, never from the index's row (<see cref="ProjectService.StoredAsync"/>).</summary>
|
||
public Task<bool> HoldsAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) =>
|
||
Reach(async () =>
|
||
{
|
||
ArgumentNullException.ThrowIfNull(version);
|
||
var project = await Own(projectId, cancellationToken).ConfigureAwait(false);
|
||
return project.DeletedAt is null && ProjectContent.Verifies(version.SettingsJson, version.ContentHash)
|
||
&& await projects.StoredAsync(projectId, version, cancellationToken).ConfigureAwait(false);
|
||
});
|
||
|
||
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
|
||
Reach(async () =>
|
||
{
|
||
await Own(projectId, cancellationToken).ConfigureAwait(false);
|
||
return await projects.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken).ConfigureAwait(false);
|
||
});
|
||
|
||
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
|
||
Reach(async () =>
|
||
{
|
||
await Own(projectId, cancellationToken).ConfigureAwait(false);
|
||
return await projects.HistoryAsync(projectId, beforeNumber, limit, cancellationToken).ConfigureAwait(false);
|
||
});
|
||
|
||
public Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) =>
|
||
Reach(async () =>
|
||
{
|
||
await Own(projectId, cancellationToken).ConfigureAwait(false);
|
||
return await projects.PushAsync(projectId, version, cancellationToken).ConfigureAwait(false);
|
||
});
|
||
|
||
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken) => projects.FramesAsync(sequenceId, cancellationToken);
|
||
|
||
public Task<SequenceProject> RenameAsync(Guid projectId, string name, CancellationToken cancellationToken) =>
|
||
Reach(async () =>
|
||
{
|
||
await Own(projectId, cancellationToken).ConfigureAwait(false);
|
||
return await projects.RenameAsync(projectId, name, cancellationToken).ConfigureAwait(false);
|
||
});
|
||
|
||
public Task DeleteAsync(Guid projectId, CancellationToken cancellationToken) =>
|
||
Reach(async () =>
|
||
{
|
||
await Own(projectId, cancellationToken).ConfigureAwait(false);
|
||
await projects.DeleteAsync(projectId, cancellationToken).ConfigureAwait(false);
|
||
return true;
|
||
});
|
||
|
||
/// <summary>
|
||
/// The sequence's frames version (frames added, removed, converted or renamed), with how many of its photos the index has
|
||
/// hashed and a checksum of those hashes (a Verify hashes them, or finds other bytes, without another frames version)
|
||
/// and how many removals it keeps (a conversion's links).
|
||
/// </summary>
|
||
public async Task<string?> FramesStampAsync(CancellationToken cancellationToken) =>
|
||
await StampOfAsync(db, sequenceId, cancellationToken).ConfigureAwait(false);
|
||
|
||
/// <summary>
|
||
/// The stamp of a sequence's frames as an index holds them (<see cref="FramesStampAsync"/>): what a server answers for
|
||
/// its sequences too (<c>GET …/projects/frames/stamp</c>).
|
||
/// </summary>
|
||
/// <exception cref="ProjectException">The sequence is not in the index.</exception>
|
||
public static async Task<string> StampOfAsync(ChronosDatabase db, Guid sequenceId, CancellationToken cancellationToken)
|
||
{
|
||
ArgumentNullException.ThrowIfNull(db);
|
||
var row = await db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false)
|
||
?? throw new ProjectException(ProjectError.SequenceNotFound, $"Sequence {sequenceId} is not in the index.");
|
||
var (hashed, checksum, removals) = await db.Frames.CountNamedAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
||
return string.Create(System.Globalization.CultureInfo.InvariantCulture, $"{row.FramesVersion}:{hashed}:{checksum}:{removals}");
|
||
}
|
||
|
||
/// <summary>
|
||
/// The project is one of this sequence's. One the index does not know yet (an index taken back from an older backup, a
|
||
/// sequence back from the trash, a project another program sharing the archive made) is looked for in the sequence's
|
||
/// sidecars first, which every listing brings into the index.
|
||
/// </summary>
|
||
private async Task<SequenceProject> Own(Guid projectId, CancellationToken cancellationToken)
|
||
{
|
||
if (await projects.GetAsync(projectId, cancellationToken).ConfigureAwait(false) is not { } project)
|
||
{
|
||
await projects.ListAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
||
project = await projects.GetAsync(projectId, cancellationToken).ConfigureAwait(false);
|
||
}
|
||
return project is not null && project.SequenceId == sequenceId
|
||
? project
|
||
: throw new ProjectException(ProjectError.NotFound, $"Project {projectId} is not a project of sequence {sequenceId}.");
|
||
}
|
||
|
||
/// <summary>An archive offline, read-only or busy is a home out of reach for now.</summary>
|
||
private static async Task<T> Reach<T>(Func<Task<T>> call)
|
||
{
|
||
try
|
||
{
|
||
return await call().ConfigureAwait(false);
|
||
}
|
||
catch (ArchiveOfflineException ex)
|
||
{
|
||
throw new HomeUnavailableException(ex.Message, ex);
|
||
}
|
||
catch (LeaseUnavailableException ex)
|
||
{
|
||
throw new HomeUnavailableException(ex.Message, ex);
|
||
}
|
||
catch (SequenceFolderMovedException ex)
|
||
{
|
||
// Another index of the archive moved the sequence: this one finds it at its next scan.
|
||
throw new HomeUnavailableException(ex.Message, ex);
|
||
}
|
||
catch (ProjectsUnreadableException ex)
|
||
{
|
||
// A record or a version this Chronos cannot read now (damaged, held open, of a newer Chronos): nothing was written.
|
||
throw new HomeUnavailableException(ex.Message, ex);
|
||
}
|
||
}
|
||
}
|