Originals are now judged by their bytes, never by the media index: a removal or clean-up hashes every file under media/originals and deletes it only when the home holds that SHA-256; every other file, named or not, goes to the workspace trash. Trash entries are filled under a staging name that Empty never lists. A ledger can only name files inside exports/. Jobs follow the workspace: a copy, render or send resolves the folder that holds its project now (a moved workspace, a retry after a move) and waits while a move has not switched yet. A clean-up failure after the switch is a warning that keeps the journal for a later resume; entries that changed since they were copied are kept, and a same-volume rename refused across volumes falls back to copy and verify. Sends map a replayed version by content hash, or go with the project only, and note an old server that drops provenance. The media index (format 2) and project file (format 3) refuse older readers while they hold originals. Metered connections also hold sync downloads and server copies. The editor gains Send again, the Settings page can resume a stopped move, the disabled tooltips say why, and unused strings are pruned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
6.4 KiB
C#
174 lines
6.4 KiB
C#
using System.Net.NetworkInformation;
|
||
using System.Runtime.InteropServices;
|
||
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 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, the sync's downloads, 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
|
||
{
|
||
private static readonly TimeSpan Interval = TimeSpan.FromMinutes(1);
|
||
|
||
private readonly AppServices _services;
|
||
private readonly HashSet<Guid> _held = [];
|
||
private readonly SemaphoreSlim _gate = new(1, 1);
|
||
private readonly Timer _timer;
|
||
private JobEngine? _engine;
|
||
|
||
public UploadHold(AppServices services)
|
||
{
|
||
_services = services;
|
||
_timer = new Timer(_ => Request(), null, Interval, Interval);
|
||
NetworkChange.NetworkAddressChanged += OnNetworkChanged;
|
||
NetworkChange.NetworkAvailabilityChanged += OnNetworkChanged;
|
||
}
|
||
|
||
/// <summary>Follows the jobs of <paramref name="engine"/> (once the index is open).</summary>
|
||
public void Attach(JobEngine engine)
|
||
{
|
||
if (ReferenceEquals(_engine, engine)) return;
|
||
if (_engine is not null) _engine.JobChanged -= OnJobChanged;
|
||
_engine = engine;
|
||
engine.JobChanged += OnJobChanged;
|
||
Request();
|
||
}
|
||
|
||
/// <summary>Applies the option and the connection's cost now (the option changed).</summary>
|
||
public void Request() => _ = ApplyAsync();
|
||
|
||
public void Dispose()
|
||
{
|
||
_timer.Dispose();
|
||
NetworkChange.NetworkAddressChanged -= OnNetworkChanged;
|
||
NetworkChange.NetworkAvailabilityChanged -= OnNetworkChanged;
|
||
if (_engine is not null) _engine.JobChanged -= OnJobChanged;
|
||
}
|
||
|
||
private bool Holding => _services.Settings.PauseUploadsOnMetered && MeteredNetwork.IsMetered();
|
||
|
||
private void OnNetworkChanged(object? sender, EventArgs e) => Request();
|
||
|
||
private void OnJobChanged(Job job)
|
||
{
|
||
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.Download, JobKind.WorkspaceCopy, JobKind.WorkspaceSend];
|
||
|
||
/// <summary>
|
||
/// Whether a metered connection holds <paramref name="job"/>: an upload, a sync download (every one is from a server), 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 or JobKind.Download => true,
|
||
JobKind.WorkspaceCopy => WorkspaceJobs.CopyParameters(job)?.Server is { Length: > 0 },
|
||
JobKind.WorkspaceSend => WorkspaceJobs.SendParameters(job)?.Server is { Length: > 0 },
|
||
_ => false,
|
||
};
|
||
}
|
||
|
||
private async Task ApplyAsync()
|
||
{
|
||
if (_engine is not { } engine || _services.Database is not { } db) return;
|
||
await _gate.WaitAsync();
|
||
try
|
||
{
|
||
if (Holding)
|
||
{
|
||
foreach (var state in new[] { JobState.Queued, JobState.Running })
|
||
{
|
||
foreach (var kind in Kinds)
|
||
{
|
||
foreach (var job in (await db.Jobs.ListAsync(state, kind, 500)).Items.Where(Holds))
|
||
{
|
||
await engine.PauseAsync(job.Id);
|
||
_held.Add(job.Id);
|
||
}
|
||
}
|
||
}
|
||
return;
|
||
}
|
||
foreach (var id in _held)
|
||
{
|
||
if (await db.Jobs.GetAsync(id) is { State: JobState.Paused }) await engine.ResumeAsync(id);
|
||
}
|
||
_held.Clear();
|
||
}
|
||
catch (Data.DatabaseException ex)
|
||
{
|
||
Log.Warning("sync", "Transfers could not be paused or resumed for the metered connection", ex);
|
||
}
|
||
finally
|
||
{
|
||
_gate.Release();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>The connection cost Windows reports (Settings › Network › Metered connection) through <c>INetworkCostManager</c>.</summary>
|
||
internal static class MeteredNetwork
|
||
{
|
||
private const uint Fixed = 0x2;
|
||
private const uint Variable = 0x4;
|
||
private const uint OverDataLimit = 0x10000;
|
||
private const uint Roaming = 0x40000;
|
||
|
||
/// <summary>True when the machine's connection is metered (fixed or variable cost, over its data limit, or roaming); false when unknown.</summary>
|
||
public static bool IsMetered()
|
||
{
|
||
try
|
||
{
|
||
var manager = (INetworkCostManager)new NetworkListManagerClass();
|
||
try
|
||
{
|
||
return manager.GetCost(out var cost, IntPtr.Zero) == 0 && (cost & (Fixed | Variable | OverDataLimit | Roaming)) != 0;
|
||
}
|
||
finally
|
||
{
|
||
Marshal.ReleaseComObject(manager);
|
||
}
|
||
}
|
||
catch (COMException ex)
|
||
{
|
||
Log.Warning("sync", "The connection cost could not be read", ex);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
[ComImport]
|
||
[Guid("DCB00C01-570F-4A9B-8D69-199FDBA5723B")]
|
||
private class NetworkListManagerClass;
|
||
|
||
[ComImport]
|
||
[Guid("DCB00008-570F-4A9B-8D69-199FDBA5723B")]
|
||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||
private interface INetworkCostManager
|
||
{
|
||
[PreserveSig]
|
||
int GetCost(out uint cost, IntPtr destination);
|
||
|
||
[PreserveSig]
|
||
int GetDataPlanStatus(IntPtr status, IntPtr destination);
|
||
|
||
[PreserveSig]
|
||
int SetDestinationAddresses(uint length, IntPtr addresses, short append);
|
||
}
|
||
}
|