feat(workspace): send local renders home with their project and version
Phase 5c P3. A render made in a workspace must reach its home with the project and version it was rendered from, and transfers with a server must respect a metered connection, downloads into the workspace included. - WorkspaceSendJobHandler: per unsent ledger entry, hash the file again (missing or changed: marked failed, file left in place), push the named version first when the home lacks it (failure fails the job, entry pending), then file it in this PC's archive (staged copy read back against its SHA-256, project and version checked against the sequence) or upload it to the server with ProjectId/ProjectVersionId; mark it sent. Idempotent by render id. A send to a server waits while HoldTransfers says so. - SyncTransfers: extract FileRenderAsync, the lease-held filing of a staged render into a sequence's exports, shared by GetExportsAsync and the send. - ISyncConnector.ProjectHome (default null; SyncHost already implements it) so the send can push a version to a server home. - Server: RenderUploads checks the project fields at start and completion (422 project_mismatch) and records them; RenderDto carries them. - UploadHold also pauses WorkspaceCopy and WorkspaceSend jobs naming a server; Server screen checkbox reads "Pause metered transfers". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2e1c67f531
commit
00b8dcd249
12 files changed
+894
-53
No files matched your search
@@ -236,7 +236,7 @@ public sealed class AppSettings
|
||||
/// <summary>Send and take project settings changed on one side without asking (Server.html).</summary>
|
||||
public bool SyncSettingsAutomatically { get; set; } = true;
|
||||
|
||||
/// <summary>Pause upload jobs while Windows reports the connection as metered (Server.html).</summary>
|
||||
/// <summary>Pause transfers with a server (uploads, workspace copies and renders sent) while Windows reports the connection as metered (Server.html).</summary>
|
||||
public bool PauseUploadsOnMetered { get; set; }
|
||||
|
||||
// ---- Jobs ----
|
||||
|
||||
@@ -160,7 +160,7 @@
|
||||
</StackPanel>
|
||||
<CheckBox Content="{l:Loc Server.KeepProxies}" IsChecked="{Binding KeepProxiesOffline}" ToolTip="{l:Loc Tooltips.Server.KeepProxies}" Margin="0,0,22,0" />
|
||||
<CheckBox Content="{l:Loc Server.AutoSync}" IsChecked="{Binding SyncSettingsAutomatically}" ToolTip="{l:Loc Tooltips.Server.AutoSync}" Margin="0,0,22,0" />
|
||||
<CheckBox Content="{l:Loc Server.PauseMetered}" IsChecked="{Binding PauseUploadsOnMetered}" ToolTip="{l:Loc Tooltips.Server.PauseMetered}" Margin="0,0,22,0" />
|
||||
<CheckBox Content="{l:Loc Server.PauseMeteredTransfers}" IsChecked="{Binding PauseUploadsOnMetered}" ToolTip="{l:Loc Tooltips.Server.PauseMeteredTransfers}" Margin="0,0,22,0" />
|
||||
</WrapPanel>
|
||||
</Border>
|
||||
</Grid>
|
||||
|
||||
@@ -4,14 +4,17 @@ using Chronos.App.Infrastructure;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Workspaces;
|
||||
|
||||
namespace Chronos.App.Server;
|
||||
|
||||
/// <summary>
|
||||
/// "Pause uploads on metered networks" (Server.html): while <see cref="AppSettings.PauseUploadsOnMetered"/> is on and
|
||||
/// Windows reports the connection as metered, upload jobs are paused (a running one stops after its current chunk and
|
||||
/// keeps its checkpoint); they are resumed when the connection is no longer metered or the option goes off. Only the
|
||||
/// uploads it paused are resumed. Checked when the network or an upload job changes, and every minute.
|
||||
/// "Pause metered transfers" (Server.html): while <see cref="AppSettings.PauseUploadsOnMetered"/> is on and Windows reports
|
||||
/// the connection as metered, the transfers with a server are paused (a running one stops after its current chunk or file
|
||||
/// and keeps its checkpoint): upload jobs, and since phase 5c the workspace's copies from a server and its renders sent to
|
||||
/// one (<see cref="Holds"/>); a copy from or a send to this PC's archive is never held. They are resumed when the connection
|
||||
/// is no longer metered or the option goes off. Only the jobs it paused are resumed. Checked when the network or one of
|
||||
/// those jobs changes, and every minute.
|
||||
/// </summary>
|
||||
public sealed class UploadHold : IDisposable
|
||||
{
|
||||
@@ -58,7 +61,26 @@ public sealed class UploadHold : IDisposable
|
||||
|
||||
private void OnJobChanged(Job job)
|
||||
{
|
||||
if (job is { Kind: JobKind.UploadCommit, State: JobState.Queued or JobState.Running }) Request();
|
||||
if (job is { State: JobState.Queued or JobState.Running } && Holds(job)) Request();
|
||||
}
|
||||
|
||||
/// <summary>The kinds of job a metered connection may hold (<see cref="Holds"/> says which of them).</summary>
|
||||
internal static IReadOnlyList<JobKind> Kinds { get; } = [JobKind.UploadCommit, JobKind.WorkspaceCopy, JobKind.WorkspaceSend];
|
||||
|
||||
/// <summary>
|
||||
/// Whether a metered connection holds <paramref name="job"/>: an upload, or a workspace copy or send whose home is a server
|
||||
/// (its parameters name one). One whose parameters cannot be read is not held: it fails with its reason.
|
||||
/// </summary>
|
||||
internal static bool Holds(Job job)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(job);
|
||||
return job.Kind switch
|
||||
{
|
||||
JobKind.UploadCommit => true,
|
||||
JobKind.WorkspaceCopy => WorkspaceJobs.CopyParameters(job)?.Server is { Length: > 0 },
|
||||
JobKind.WorkspaceSend => WorkspaceJobs.SendParameters(job)?.Server is { Length: > 0 },
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
private async Task ApplyAsync()
|
||||
@@ -71,10 +93,13 @@ public sealed class UploadHold : IDisposable
|
||||
{
|
||||
foreach (var state in new[] { JobState.Queued, JobState.Running })
|
||||
{
|
||||
foreach (var job in (await db.Jobs.ListAsync(state, JobKind.UploadCommit, 500)).Items)
|
||||
foreach (var kind in Kinds)
|
||||
{
|
||||
await engine.PauseAsync(job.Id);
|
||||
_held.Add(job.Id);
|
||||
foreach (var job in (await db.Jobs.ListAsync(state, kind, 500)).Items.Where(Holds))
|
||||
{
|
||||
await engine.PauseAsync(job.Id);
|
||||
_held.Add(job.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
@@ -87,7 +112,7 @@ public sealed class UploadHold : IDisposable
|
||||
}
|
||||
catch (Data.DatabaseException ex)
|
||||
{
|
||||
Log.Warning("sync", "Uploads could not be paused or resumed for the metered connection", ex);
|
||||
Log.Warning("sync", "Transfers could not be paused or resumed for the metered connection", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -92,6 +92,13 @@ public interface ISyncConnector
|
||||
{
|
||||
/// <summary>A signed-in remote of <paramref name="server"/> owned by the caller; null when this PC is not connected to that server.</summary>
|
||||
ValueTask<ISyncRemote?> ConnectAsync(string server, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// The project home of a workspace project whose home is <paramref name="home"/> (a server's sequence) over the same
|
||||
/// signed-in session: where a send of its renders pushes the version they name first (phase 5c). Null when this PC is not
|
||||
/// connected to that server, or the connection keeps no projects.
|
||||
/// </summary>
|
||||
Projects.IProjectHome? ProjectHome(Core.Project.BundleHome home) => null;
|
||||
}
|
||||
|
||||
/// <summary><see cref="ISyncRemote"/> over a signed-in <see cref="ChronosClient"/>, which it disposes when it owns it.</summary>
|
||||
|
||||
@@ -500,44 +500,58 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
foreach (var render in wanted)
|
||||
{
|
||||
var (part, name) = await FetchRenderAsync(remote, render, staging, ct).ConfigureAwait(false);
|
||||
// Filed under the sequence's lease into its folder as the index has it then, as a server files a render: a
|
||||
// Reorganize may have moved it since the sequence was read, and moves the row's path along when it moves it later.
|
||||
// One another index moved is found by this index's next scan; until then nothing is filed (the download stays
|
||||
// staged for the next attempt), so the folder it left is not created again, nor filed into when it holds another
|
||||
// sequence now.
|
||||
var filed = await sync.Archives.UnderLeaseAsync(local, async row =>
|
||||
var filed = await FileRenderAsync(sync.Archives, db, local, paths, part, name, new Render
|
||||
{
|
||||
if (row is null) throw new InvalidOperationException(Loc.L("Errors.Sync.LocalMissing"));
|
||||
var folder = paths.Sequence(row.Folder);
|
||||
if (!SequenceFolders.Holds(folder, row.Id)) return false;
|
||||
Directory.CreateDirectory(folder.Exports);
|
||||
var target = Unique(folder.Exports, name);
|
||||
File.Move(part, target);
|
||||
// Recorded even if the job is being cancelled: a filed export is never left without its row.
|
||||
await db.Renders.InsertAsync(new Render
|
||||
{
|
||||
Id = render.Id,
|
||||
SequenceId = row.Id,
|
||||
PresetName = render.PresetName,
|
||||
OutputPath = paths.ToRelative(target),
|
||||
Codec = render.Codec,
|
||||
Width = render.Width,
|
||||
Height = render.Height,
|
||||
Fps = render.Fps,
|
||||
DurationSeconds = render.DurationSeconds,
|
||||
Bytes = new FileInfo(target).Length,
|
||||
Created = render.Created,
|
||||
ProducedBy = render.ProducedBy,
|
||||
}, CancellationToken.None).ConfigureAwait(false);
|
||||
return true;
|
||||
Id = render.Id,
|
||||
SequenceId = local.Id,
|
||||
PresetName = render.PresetName,
|
||||
OutputPath = string.Empty,
|
||||
Codec = render.Codec,
|
||||
Width = render.Width,
|
||||
Height = render.Height,
|
||||
Fps = render.Fps,
|
||||
DurationSeconds = render.DurationSeconds,
|
||||
Created = render.Created,
|
||||
ProducedBy = render.ProducedBy,
|
||||
}, ct).ConfigureAwait(false);
|
||||
if (!filed) throw new InvalidOperationException(Loc.L("Errors.Archive.FolderMoved"));
|
||||
if (filed is null) throw new InvalidOperationException(Loc.L("Errors.Archive.FolderMoved"));
|
||||
await progress.StepAsync().ConfigureAwait(false);
|
||||
}
|
||||
TryDeleteDirectory(staging);
|
||||
await sync.RecompareAsync(remote, remoteId, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Files the render staged at <paramref name="part"/> (on the archive's volume) into the exports of
|
||||
/// <paramref name="local"/> under its lease, into its folder as the index has it then, as a server files a render: a
|
||||
/// Reorganize may have moved it since the sequence was read, and moves the row's path along when it moves it later. The
|
||||
/// file takes <paramref name="name"/>, or "name (2)" and so on (an export is never replaced), and the index records
|
||||
/// <paramref name="render"/> for it, with the sequence, the path and the size it has there (recorded even if the job is
|
||||
/// being cancelled: a filed export is never left without its row). Used by the sync's downloads of a server's exports and
|
||||
/// by a workspace's renders sent home (<see cref="Workspaces.WorkspaceJobs.Send"/>). Returns the archive-relative path
|
||||
/// it was filed at; null when the folder does not hold the sequence now (another index moved it; this index's next scan
|
||||
/// finds it): nothing is filed then, the staged file stays for the next attempt, and the folder it left is not created
|
||||
/// again, nor filed into when it holds another sequence now.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">The sequence left the index.</exception>
|
||||
/// <exception cref="ArchiveOfflineException">The archive is offline or read-only.</exception>
|
||||
/// <exception cref="LeaseUnavailableException">Another writer held the sequence for longer than the lease wait.</exception>
|
||||
internal static Task<string?> FileRenderAsync(ArchiveService archives, ChronosDatabase db, Sequence local, ArchivePaths paths, string part, string name,
|
||||
Render render, CancellationToken cancellationToken) =>
|
||||
archives.UnderLeaseAsync(local, async row =>
|
||||
{
|
||||
if (row is null) throw new InvalidOperationException(Loc.L("Errors.Sync.LocalMissing"));
|
||||
var folder = paths.Sequence(row.Folder);
|
||||
if (!SequenceFolders.Holds(folder, row.Id)) return null;
|
||||
Directory.CreateDirectory(folder.Exports);
|
||||
var target = Unique(folder.Exports, name);
|
||||
File.Move(part, target);
|
||||
var relative = paths.ToRelative(target);
|
||||
await db.Renders.InsertAsync(render with { SequenceId = row.Id, OutputPath = relative, Bytes = new FileInfo(target).Length }, CancellationToken.None)
|
||||
.ConfigureAwait(false);
|
||||
return (string?)relative;
|
||||
}, cancellationToken);
|
||||
|
||||
/// <summary>A render's file in staging (resumed with Range; started over once if the partial copy no longer fits) and the name the server gives it.</summary>
|
||||
private static async Task<(string Part, string Name)> FetchRenderAsync(ISyncRemote remote, RenderDto render, string staging, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -833,7 +847,7 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
private static Task<Job?> EnqueueFollowUpAsync(JobContext context, ArchiveJobRequest request, CancellationToken cancellationToken) =>
|
||||
context.EnqueueFollowUpAsync(request.Kind, request.Targets, request.Label, request.ParametersJson, request.Priority, cancellationToken);
|
||||
|
||||
private static void EnsureSpace(string root, long bytes)
|
||||
internal static void EnsureSpace(string root, long bytes)
|
||||
{
|
||||
long? free;
|
||||
try
|
||||
|
||||
@@ -1,25 +1,303 @@
|
||||
using System.Net;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Data.Sync;
|
||||
using Chronos.Protocol;
|
||||
using Chronos.Protocol.Client;
|
||||
|
||||
namespace Chronos.Data.Workspaces;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="JobKind.WorkspaceSend"/> (phase 5c, <see cref="WorkspaceJobs.Send"/>): the local renders of a workspace project
|
||||
/// sent home with their project and version, per unsent ledger entry: the file hashed again (another is refused and stays),
|
||||
/// the version it names pushed first when the home lacks it, then filed in this PC's archive (as the sync files a server's
|
||||
/// exports) or uploaded to its server (<c>RenderUploadRequest</c> with <c>ProjectId</c> and <c>ProjectVersionId</c>), and the
|
||||
/// entry marked sent. Idempotent by render id: a send repeated after a cut finds the home holding it already.
|
||||
/// sent home with their project and version, per unsent ledger entry (<see cref="WorkspaceRenders"/>; the ids the job names,
|
||||
/// or every unsent one): the file hashed again (one missing or changed since it was rendered is refused, marked failed in the
|
||||
/// ledger and left where it is), the version it names pushed first when the home lacks it (a push that fails fails the job,
|
||||
/// the entry left as it was), then filed in this PC's archive (copied into the archive's staging folder, read back against
|
||||
/// its SHA-256, and filed under the sequence's lease as the sync files a server's exports,
|
||||
/// <see cref="SyncTransfers.FileRenderAsync"/>, after checking the project and version are the sequence's) or uploaded to its
|
||||
/// server (<see cref="RenderUploadRequest"/> with <c>ProjectId</c> and <c>ProjectVersionId</c>, which the server checks the
|
||||
/// same way), and the entry marked sent. The file stays in <c>exports/</c> until a clean-up removes it.
|
||||
/// Idempotent by render id: a send repeated after a cut finds the home holding the render already and only marks it sent; a
|
||||
/// render id the home files under another sequence is refused. The ledger is read and written under the project's lease,
|
||||
/// each entry read again before it is written, so a render finished meanwhile is kept.
|
||||
/// A send to a server is held while <see cref="WorkspaceJobOptions.HoldTransfers"/> says so (a metered connection): it waits
|
||||
/// before each upload, and the desktop pauses it meanwhile. A send to this PC's archive is never held.
|
||||
/// </summary>
|
||||
internal sealed class WorkspaceSendJobHandler(ChronosDatabase db, ArchiveService archives, ISyncConnector? servers, WorkspaceJobOptions options) : IJobHandler
|
||||
{
|
||||
private static readonly TimeSpan LockWait = TimeSpan.FromMinutes(2);
|
||||
|
||||
/// <summary>How often a send held on a metered connection looks again.</summary>
|
||||
internal static TimeSpan HoldPoll { get; set; } = TimeSpan.FromSeconds(2);
|
||||
|
||||
public JobKind Kind => JobKind.WorkspaceSend;
|
||||
|
||||
public Task RunAsync(JobContext context)
|
||||
public async Task RunAsync(JobContext context)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
_ = (db, archives, servers, options);
|
||||
throw new NotImplementedException("Sending a workspace project's renders home comes with phase 5c (P3).");
|
||||
var ct = context.Cancellation;
|
||||
var parameters = WorkspaceJobs.SendParameters(context.Job) ?? throw new InvalidDataException(Loc.L("Errors.Workspace.JobParameters"));
|
||||
if (context.Job.TargetIds is not [var sequenceId] || string.IsNullOrWhiteSpace(parameters.WorkspaceRoot))
|
||||
throw new InvalidDataException(Loc.L("Errors.Workspace.JobParameters"));
|
||||
var server = parameters.Server is { Length: > 0 } named ? named : null;
|
||||
var project = new WorkspaceStore(parameters.WorkspaceRoot).Find(parameters.ProjectId)
|
||||
?? throw new InvalidOperationException(Loc.L("Errors.Workspace.NotCheckedOut"));
|
||||
var workspace = project.ReadBundle().Workspace ?? throw new InvalidOperationException(Loc.L("Errors.Workspace.NotCheckedOut"));
|
||||
var home = workspace.Home;
|
||||
if (home.SequenceId != sequenceId || (server is null
|
||||
? !home.IsArchive
|
||||
: home.Kind != BundleHome.ServerKind || !string.Equals(home.Server, server, StringComparison.OrdinalIgnoreCase)))
|
||||
throw new InvalidOperationException(Loc.L("Errors.Workspace.OtherHome"));
|
||||
|
||||
var entries = await ToSendAsync(project, parameters.RenderIds, ct).ConfigureAwait(false);
|
||||
await context.ReportAsync(0, entries.Count).ConfigureAwait(false);
|
||||
if (entries.Count == 0)
|
||||
{
|
||||
context.Log(Loc.L("Archive.Log.WorkspaceSent", workspace.Name, 0, 0));
|
||||
return;
|
||||
}
|
||||
if (server is null)
|
||||
{
|
||||
var sequence = await db.Sequences.GetAsync(sequenceId, ct).ConfigureAwait(false)
|
||||
?? throw new InvalidOperationException(Loc.L("Archive.Log.SequenceMissing", sequenceId));
|
||||
var archive = await db.Archives.GetAsync(sequence.ArchiveId, ct).ConfigureAwait(false)
|
||||
?? throw new InvalidOperationException(Loc.L("Errors.Sync.NoArchive"));
|
||||
var projectHome = new ArchiveProjectHome(db, archives.Projects, sequenceId);
|
||||
await SendAsync(context, project, workspace, entries, () => projectHome, held: false,
|
||||
(entry, file, token) => FileAsync(sequence, archive, entry, file, token)).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
using var remote = (servers is null ? null : await servers.ConnectAsync(server, ct).ConfigureAwait(false))
|
||||
?? throw new InvalidOperationException(Loc.L("Errors.Sync.NotConnected"));
|
||||
await SendAsync(context, project, workspace, entries,
|
||||
() => servers!.ProjectHome(home) ?? throw new InvalidOperationException(Loc.L("Errors.Sync.NotConnected")), held: true,
|
||||
(entry, file, token) => UploadAsync(remote, sequenceId, entry, file, token)).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The entries to send, oldest first: those of <paramref name="renderIds"/> the ledger holds unsent (an id it no longer
|
||||
/// holds was discarded meanwhile, one sent already is left), every unsent one when none is named.
|
||||
/// </summary>
|
||||
private static async Task<IReadOnlyList<WorkspaceRenderEntry>> ToSendAsync(WorkspaceProject project, IReadOnlyList<Guid>? renderIds, CancellationToken ct)
|
||||
{
|
||||
WorkspaceRenders ledger;
|
||||
using (await project.LockAsync(LockWait, ct).ConfigureAwait(false)) ledger = project.ReadRenders();
|
||||
if (renderIds is not { Count: > 0 }) return ledger.Unsent;
|
||||
var named = renderIds.ToHashSet();
|
||||
return [.. ledger.Entries.Where(e => named.Contains(e.RenderId) && e.IsUnsent)];
|
||||
}
|
||||
|
||||
/// <summary>Delivers one checked render home: true when the home took it now, false when it held it already.</summary>
|
||||
private delegate Task<bool> Deliver(WorkspaceRenderEntry entry, string file, CancellationToken cancellationToken);
|
||||
|
||||
private async Task SendAsync(JobContext context, WorkspaceProject project, BundleWorkspace workspace, IReadOnlyList<WorkspaceRenderEntry> entries,
|
||||
Func<IProjectHome> home, bool held, Deliver deliver)
|
||||
{
|
||||
var ct = context.Cancellation;
|
||||
var (sent, there, refused) = (0, 0, 0);
|
||||
// The versions this run found at the home, or sent there first.
|
||||
var versions = new HashSet<Guid>();
|
||||
for (var i = 0; i < entries.Count; i++)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
if (held) await WhileHeldAsync(context).ConfigureAwait(false);
|
||||
var entry = entries[i];
|
||||
var file = project.ExportFile(entry.File);
|
||||
var refusal = await CheckAsync(project, entry, file, ct).ConfigureAwait(false);
|
||||
if (refusal is null)
|
||||
{
|
||||
if (entry.ProjectVersionId is { } versionId && !versions.Contains(versionId))
|
||||
{
|
||||
await EnsureVersionAsync(context, project, workspace, home(), entry.ProjectId, versionId, ct).ConfigureAwait(false);
|
||||
versions.Add(versionId);
|
||||
}
|
||||
try
|
||||
{
|
||||
if (await deliver(entry, file, ct).ConfigureAwait(false)) sent++;
|
||||
else there++;
|
||||
// The home holds it: recorded whatever stops the job now.
|
||||
await UpdateAsync(project, entry.RenderId, e => e.AsSent(DateTimeOffset.UtcNow)).ConfigureAwait(false);
|
||||
}
|
||||
catch (SendRefusedException ex)
|
||||
{
|
||||
refusal = ex.Message;
|
||||
}
|
||||
}
|
||||
if (refusal is not null)
|
||||
{
|
||||
refused++;
|
||||
context.Log(refusal);
|
||||
await UpdateAsync(project, entry.RenderId, e => e.AsFailed(refusal)).ConfigureAwait(false);
|
||||
}
|
||||
await context.ReportAsync(i + 1, entries.Count).ConfigureAwait(false);
|
||||
}
|
||||
context.Log(Loc.L("Archive.Log.WorkspaceSent", workspace.Name, sent, there));
|
||||
if (refused > 0) throw new InvalidOperationException(Loc.L("Errors.Workspace.SendFailed", refused, entries.Count));
|
||||
}
|
||||
|
||||
/// <summary>Why the render cannot be sent as it is (gone from the exports, or another file than the one rendered); null when it can.</summary>
|
||||
private static async Task<string?> CheckAsync(WorkspaceProject project, WorkspaceRenderEntry entry, string file, CancellationToken ct)
|
||||
{
|
||||
var name = Path.GetFileName(entry.File);
|
||||
var exports = Path.TrimEndingDirectorySeparator(Path.GetFullPath(project.ExportsDirectory)) + Path.DirectorySeparatorChar;
|
||||
// A ledger names files inside the exports only; one pointing elsewhere is never read.
|
||||
if (!Path.GetFullPath(file).StartsWith(exports, StringComparison.OrdinalIgnoreCase) || !File.Exists(file)) return Loc.L("Errors.Workspace.SendMissing", name);
|
||||
if (new FileInfo(file).Length != entry.Size
|
||||
|| !string.Equals(await ChronosClient.HashFileAsync(file, ct).ConfigureAwait(false), entry.Sha256, StringComparison.OrdinalIgnoreCase))
|
||||
return Loc.L("Errors.Workspace.SendHashMismatch", name);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Version <paramref name="versionId"/> of the project at the home: pushed there first (every local version, in order, as a
|
||||
/// sync pushes them) when the home lacks it. A push that does not bring it there fails the job.
|
||||
/// </summary>
|
||||
private static async Task EnsureVersionAsync(JobContext context, WorkspaceProject project, BundleWorkspace workspace, IProjectHome home, Guid projectId,
|
||||
Guid versionId, CancellationToken ct)
|
||||
{
|
||||
if (await home.GetVersionAsync(projectId, versionId, ct).ConfigureAwait(false) is not null) return;
|
||||
try
|
||||
{
|
||||
await new WorkspaceSync(project.Store, Environment.MachineName) { LockWait = LockWait }.PushAsync(projectId, home, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is HomeUnavailableException or WorkspaceSyncException or ProjectException or LeaseUnavailableException
|
||||
or InvalidOperationException)
|
||||
{
|
||||
context.Log(ex.Message);
|
||||
throw new InvalidOperationException(Loc.L("Errors.Workspace.SendVersion"), ex);
|
||||
}
|
||||
var pushed = await home.GetVersionAsync(projectId, versionId, ct).ConfigureAwait(false)
|
||||
?? throw new InvalidOperationException(Loc.L("Errors.Workspace.SendVersion"));
|
||||
context.Log(Loc.L("Archive.Log.WorkspaceVersionPushed", workspace.Name, pushed.Number));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The render filed in this PC's archive, the home's sequence: copied into the archive's staging folder, read back against
|
||||
/// the ledger's SHA-256, then filed under the sequence's lease with the project and version (once both are checked to be
|
||||
/// the sequence's). A render id the index holds already for this sequence is the same render, sent before.
|
||||
/// </summary>
|
||||
private async Task<bool> FileAsync(Sequence sequence, Archive archive, WorkspaceRenderEntry entry, string file, CancellationToken ct)
|
||||
{
|
||||
if (await db.Renders.GetAsync(entry.RenderId, ct).ConfigureAwait(false) is { } known)
|
||||
return known.SequenceId == sequence.Id ? false : throw new SendRefusedException(Loc.L("Server.Error.RenderExists"));
|
||||
if (!await ProjectOfAsync(sequence.Id, entry, ct).ConfigureAwait(false)) throw new SendRefusedException(Loc.L("Server.Error.ProjectMismatch"));
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
ArchiveVolume.EnsureWritable(paths);
|
||||
SyncTransfers.EnsureSpace(paths.Root, entry.Size);
|
||||
Directory.CreateDirectory(paths.StagingDirectory);
|
||||
var staged = Path.Combine(paths.StagingDirectory, "workspace-render-" + entry.RenderId.ToString("N") + ".part");
|
||||
try
|
||||
{
|
||||
await using (var source = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 1 << 20, FileOptions.Asynchronous | FileOptions.SequentialScan))
|
||||
await using (var target = new FileStream(staged, FileMode.Create, FileAccess.Write, FileShare.None, 1 << 20, FileOptions.Asynchronous))
|
||||
{
|
||||
await source.CopyToAsync(target, ct).ConfigureAwait(false);
|
||||
target.Flush(flushToDisk: true);
|
||||
}
|
||||
// Read back from the archive's volume: what is filed is the render as it was written.
|
||||
if (!string.Equals(await ChronosClient.HashFileAsync(staged, ct).ConfigureAwait(false), entry.Sha256, StringComparison.OrdinalIgnoreCase))
|
||||
throw new IOException(Loc.L("Errors.Workspace.CopyMismatch", staged));
|
||||
var name = SyncTransfers.SafeFileName(Path.GetFileName(entry.File)) ?? entry.RenderId.ToString("N");
|
||||
var filed = await SyncTransfers.FileRenderAsync(archives, db, sequence, paths, staged, name, new Render
|
||||
{
|
||||
Id = entry.RenderId,
|
||||
SequenceId = sequence.Id,
|
||||
PresetName = entry.PresetName,
|
||||
OutputPath = string.Empty,
|
||||
Codec = entry.Codec,
|
||||
Width = entry.Width,
|
||||
Height = entry.Height,
|
||||
Fps = entry.Fps,
|
||||
DurationSeconds = entry.DurationSeconds,
|
||||
Created = entry.Rendered,
|
||||
ProducedBy = options.Processing.ProducedBy,
|
||||
ProjectId = entry.ProjectId,
|
||||
ProjectVersionId = entry.ProjectVersionId,
|
||||
}, ct).ConfigureAwait(false);
|
||||
return filed is not null ? true : throw new InvalidOperationException(Loc.L("Errors.Archive.FolderMoved"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
TryDelete(staged);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The project the entry names is one of the sequence's, and its version (when it names one) one of that project's.</summary>
|
||||
private async Task<bool> ProjectOfAsync(Guid sequenceId, WorkspaceRenderEntry entry, CancellationToken ct)
|
||||
{
|
||||
var project = await db.Projects.GetAsync(entry.ProjectId, ct).ConfigureAwait(false);
|
||||
var version = entry.ProjectVersionId is { } versionId ? await db.Projects.GetVersionAsync(versionId, ct).ConfigureAwait(false) : null;
|
||||
if (project is null || (entry.ProjectVersionId is not null && version is null))
|
||||
{
|
||||
// One the index does not know yet may be in the sequence's sidecars: listing reads them first.
|
||||
await archives.Projects.ListAsync(sequenceId, ct).ConfigureAwait(false);
|
||||
project = await db.Projects.GetAsync(entry.ProjectId, ct).ConfigureAwait(false);
|
||||
if (entry.ProjectVersionId is { } id) version = await db.Projects.GetVersionAsync(id, ct).ConfigureAwait(false);
|
||||
}
|
||||
return project is not null && project.SequenceId == sequenceId && (entry.ProjectVersionId is null || version?.ProjectId == project.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The render uploaded to its server's sequence, with its project and version, resumable and checked against its SHA-256
|
||||
/// there; the server answers that it holds the render already when it does. Refused for good (the server takes the
|
||||
/// project for another sequence's, the id for another sequence's render, the file for too large or not the one hashed):
|
||||
/// marked failed; anything else (the server out of reach, busy, the sign-in expired) fails the job, the entry left as it was.
|
||||
/// </summary>
|
||||
private async Task<bool> UploadAsync(ISyncRemote remote, Guid sequenceId, WorkspaceRenderEntry entry, string file, CancellationToken ct)
|
||||
{
|
||||
var name = SyncTransfers.SafeFileName(Path.GetFileName(entry.File)) ?? entry.RenderId.ToString("N");
|
||||
var request = new RenderUploadRequest(entry.RenderId, name, entry.Size, entry.Sha256.ToLowerInvariant(), entry.PresetName, entry.Codec, entry.Width, entry.Height,
|
||||
entry.Fps, entry.DurationSeconds, options.Processing.ProducedBy, entry.ProjectId, entry.ProjectVersionId);
|
||||
try
|
||||
{
|
||||
return await remote.UploadRenderAsync(sequenceId, file, request, ct).ConfigureAwait(false) is not null;
|
||||
}
|
||||
catch (ChronosApiException ex) when (ex.Status is HttpStatusCode.BadRequest or HttpStatusCode.Conflict or HttpStatusCode.RequestEntityTooLarge
|
||||
or HttpStatusCode.UnprocessableEntity)
|
||||
{
|
||||
throw new SendRefusedException(string.IsNullOrWhiteSpace(ex.Error.Message) ? ex.Message : ex.Error.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Waits while transfers with a server are held (a metered connection), saying so once.</summary>
|
||||
private async Task WhileHeldAsync(JobContext context)
|
||||
{
|
||||
if (!options.HoldTransfers()) return;
|
||||
context.Log(Loc.L("Archive.Log.WorkspaceSendHeld"));
|
||||
while (options.HoldTransfers()) await Task.Delay(HoldPoll, context.Cancellation).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The ledger entry of <paramref name="renderId"/> changed by <paramref name="change"/>, under the project's lease, read again
|
||||
/// first (a render finished meanwhile stays); an entry discarded meanwhile stays discarded. Written whatever stops the job.
|
||||
/// </summary>
|
||||
private static async Task UpdateAsync(WorkspaceProject project, Guid renderId, Func<WorkspaceRenderEntry, WorkspaceRenderEntry> change)
|
||||
{
|
||||
using (await project.LockAsync(LockWait, CancellationToken.None).ConfigureAwait(false))
|
||||
{
|
||||
var ledger = project.ReadRenders();
|
||||
if (ledger.Find(renderId) is { } entry) project.WriteRenders(ledger.With(change(entry)));
|
||||
}
|
||||
}
|
||||
|
||||
private static void TryDelete(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(path)) File.Delete(path);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Debug("workspace", $"{path} could not be removed: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The home will not take this render as it is: it is marked failed with this message, and the others go on.</summary>
|
||||
private sealed class SendRefusedException(string message) : Exception(message);
|
||||
}
|
||||
@@ -109,6 +109,13 @@ public sealed record ManifestFrame(Guid Id, int Index, string FileName, long Siz
|
||||
/// <summary>How the server's proxies are made (BRIEF §8.5): long edge in pixels (0 = full size) and JPEG quality.</summary>
|
||||
public sealed record ProxySpec(int LongEdge, int Quality);
|
||||
|
||||
/// <summary>A render of a sequence, filed under its exports.</summary>
|
||||
/// <param name="ProjectId">
|
||||
/// The project whose version was rendered (owner review 2, R8): a render node's output of a job that names one, or a render a
|
||||
/// workspace made on a client and sent home (phase 5c); null for a render of the sequence's own settings, and from servers
|
||||
/// before phase 5c.
|
||||
/// </param>
|
||||
/// <param name="ProjectVersionId">The project version rendered; null when none is named (see <paramref name="ProjectId"/>).</param>
|
||||
public sealed record RenderDto(
|
||||
Guid Id,
|
||||
string PresetName,
|
||||
@@ -119,7 +126,9 @@ public sealed record RenderDto(
|
||||
double DurationSeconds,
|
||||
long Bytes,
|
||||
DateTimeOffset Created,
|
||||
string? ProducedBy);
|
||||
string? ProducedBy,
|
||||
Guid? ProjectId = null,
|
||||
Guid? ProjectVersionId = null);
|
||||
|
||||
/// <summary>Aggregates for the archive dashboard and the admin panel.</summary>
|
||||
/// <param name="FreeBytes">Free bytes of the space volume: the server's space path when one is set, else the archive's volume.</param>
|
||||
|
||||
@@ -23,7 +23,8 @@ public static class Dto
|
||||
f.Id, f.SequenceId, f.Index, f.FileName, f.Size, f.Sha256, f.CapturedAt, f.UtcOffsetMinutes, f.ExposureTime, f.FNumber, f.Iso,
|
||||
f.FocalLength, f.Orientation, f.Width, f.Height, f.Format.ToString().ToLowerInvariant(), (long)f.Flags, f.PreviewWidth, f.PreviewHeight);
|
||||
|
||||
public static RenderDto Render(Render r) => new(r.Id, r.PresetName, r.Codec, r.Width, r.Height, r.Fps, r.DurationSeconds, r.Bytes, r.Created, r.ProducedBy);
|
||||
public static RenderDto Render(Render r) => new(r.Id, r.PresetName, r.Codec, r.Width, r.Height, r.Fps, r.DurationSeconds, r.Bytes, r.Created, r.ProducedBy,
|
||||
r.ProjectId, r.ProjectVersionId);
|
||||
|
||||
public static JobDto Job(Job j) => new(
|
||||
j.Id, JobWire.Kind(j.Kind), j.TargetIds, j.TargetLabel, JobWire.State(j.State), JobWire.Priority(j.Priority), j.ProgressDone,
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Text.Json;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Repositories;
|
||||
using Chronos.Protocol;
|
||||
using Chronos.Server.Api;
|
||||
@@ -17,7 +18,12 @@ namespace Chronos.Server.Storage;
|
||||
/// Renders a client sends on request (BRIEF §14.5): chunked like frame uploads (same store, chunk endpoint and
|
||||
/// retention), verified against their SHA-256 at completion and filed by <see cref="RenderStore"/> under the
|
||||
/// sequence's exports with the client's render id, so both sides recognise the same render.
|
||||
/// A render a workspace made (phase 5c) names its project and version: both are checked against the sequence when the upload
|
||||
/// starts and again when it completes (422 <c>project_mismatch</c>: the project is not one of the sequence's, or the version
|
||||
/// not one of the project's) and recorded with the render; a version alone names its project. Without them a render is filed
|
||||
/// as before, recording neither.
|
||||
/// </summary>
|
||||
/// <param name="archives">The archive engine whose projects are read when the index does not know one yet; one of its own when none is registered.</param>
|
||||
public sealed class RenderUploads(
|
||||
ChronosDatabase db,
|
||||
ServerOptions options,
|
||||
@@ -26,7 +32,8 @@ public sealed class RenderUploads(
|
||||
RenderStore renders,
|
||||
EventHub events,
|
||||
VolumeSpace volumes,
|
||||
TimeProvider time)
|
||||
TimeProvider time,
|
||||
ArchiveService? archives = null)
|
||||
{
|
||||
public async Task<Outcome<StartUploadResponse>> StartAsync(Caller caller, Guid sequenceId, RenderUploadRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -45,6 +52,8 @@ public sealed class RenderUploads(
|
||||
? new StartUploadResponse(null, options.ChunkSizeBytes, [], AlreadyExists: true, null)
|
||||
: Outcome<StartUploadResponse>.Fail(409, "render_exists");
|
||||
}
|
||||
// Checked before a byte is sent, and again at completion.
|
||||
if (await ProjectOfAsync(sequenceId, request, cancellationToken) is null) return Outcome<StartUploadResponse>.Fail(422, "project_mismatch");
|
||||
var sha = request.Sha256.ToLowerInvariant();
|
||||
if (await db.Uploads.FindReceivingAsync(caller.Id, sha, cancellationToken) is { } open && open.SequenceId == sequenceId && open.Size == request.Size
|
||||
&& Read(open)?.RenderId == request.RenderId && store.Exists(open.Id))
|
||||
@@ -81,6 +90,8 @@ public sealed class RenderUploads(
|
||||
if (upload.State != UploadState.Receiving) return Outcome<RenderDto>.Fail(409, "upload_state");
|
||||
if (!upload.IsComplete)
|
||||
return Outcome<RenderDto>.Fail(409, "incomplete", (upload.ChunkCount - upload.ReceivedCount).ToString(CultureInfo.InvariantCulture));
|
||||
// Asked again: the upload may have been started before the project was deleted from the index, or by an earlier server.
|
||||
if (await ProjectOfAsync(sequenceId, output, cancellationToken) is not { } provenance) return Outcome<RenderDto>.Fail(422, "project_mismatch");
|
||||
if (!uploads.ArchiveHasRoom(upload)) return Outcome<RenderDto>.Fail(507, "insufficient_storage");
|
||||
if (!await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Receiving, UploadState.Committing, cancellationToken: cancellationToken))
|
||||
return Outcome<RenderDto>.Fail(409, "upload_state");
|
||||
@@ -93,7 +104,7 @@ public sealed class RenderUploads(
|
||||
try
|
||||
{
|
||||
render = sequence is null ? null : await renders.CommitAsync(sequence, staged, result, output.ProducedBy ?? caller.ClientName ?? caller.User.Username,
|
||||
output.RenderId, cancellationToken);
|
||||
output.RenderId, provenance.ProjectId, provenance.ProjectVersionId, cancellationToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is LeaseUnavailableException or ArchiveOfflineException or FolderMovedException)
|
||||
{
|
||||
@@ -113,6 +124,33 @@ public sealed class RenderUploads(
|
||||
return Dto.Render(render);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The project and version <paramref name="request"/> names, checked against sequence <paramref name="sequenceId"/>: the
|
||||
/// project one of its own (deleted ones included: their versions are kept), the version one of that project's (a version
|
||||
/// alone names its project). One the index does not know yet may be in the sequence's sidecars: listing reads them first.
|
||||
/// Both null when the request names neither; null when they do not hold.
|
||||
/// </summary>
|
||||
private async Task<(Guid? ProjectId, Guid? ProjectVersionId)?> ProjectOfAsync(Guid sequenceId, RenderUploadRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.ProjectId is null && request.ProjectVersionId is null) return (null, null);
|
||||
var (project, version) = await FindAsync();
|
||||
if (project is null || (request.ProjectVersionId is not null && version is null))
|
||||
{
|
||||
await LibraryEndpoints.Engine(archives, db).Projects.ListAsync(sequenceId, cancellationToken);
|
||||
(project, version) = await FindAsync();
|
||||
}
|
||||
if (project is null || project.SequenceId != sequenceId || (version is not null && version.ProjectId != project.Id)) return null;
|
||||
return (project.Id, version?.Id);
|
||||
|
||||
async Task<(SequenceProject? Project, ProjectVersion? Version)> FindAsync()
|
||||
{
|
||||
var known = request.ProjectVersionId is { } versionId ? await db.Projects.GetVersionAsync(versionId, cancellationToken) : null;
|
||||
if (request.ProjectVersionId is not null && known is null) return (null, null);
|
||||
var projectId = request.ProjectId ?? known!.ProjectId;
|
||||
return (await db.Projects.GetAsync(projectId, cancellationToken), known);
|
||||
}
|
||||
}
|
||||
|
||||
private static RenderUploadRequest? Read(Upload upload)
|
||||
{
|
||||
if (upload.RenderJson is not { } json) return null;
|
||||
|
||||
Reference in new issue
Block a user