Files
Chronos/src/Chronos.Data/Sync/SyncJobHandler.cs
T
Alby96andClaude Opus 5.5 fe752a0a21 fix(copies): address the owner review 6 findings
The review of the found-copies work showed pairs that could stick, jobs that
could act on a pair the user had answered otherwise, and detection gaps:

- forgetting a link on an address-keyed server deletes the pair and Candidate
  rows of that remote and resets the probe, so the sequence is found again
  when it is back; a Linked/Integrated row with no link is asked again
- 404s count only on id-keyed servers, at most once per recheck interval
- probes store the server's newest update time (copy_probes.server_newest,
  migration 0012) instead of comparing with this PC's clock; the lookup
  sample carries hashed photos by size and capture time too, for servers
  that have not hashed their copy
- the older-server gate no longer re-compares unchanged manifests
- a failed Integrate keeps its job id so a retry resumes only an untouched
  pair; cancel and stale plans release the pair (ReleaseAsync)
- automatic photo transfers never create or retarget a sequence
- a first upload files photos the server holds elsewhere into the new
  sequence
- Unlink re-compares before keeping apart; Trash here names only true last
  copies; Drop... trashes one version on both sides after re-checking the
  other
- a merge keeping the pair's other sequence swaps the dialog's columns and
  re-keys Different choices; Upload all reads every pair

Each fix has a regression test where it makes sense.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 13:12:00 +01:00

102 lines
4.3 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Jobs;
namespace Chronos.Data.Sync;
/// <summary>What an upload or download job moves.</summary>
public enum SyncTransferKind
{
/// <summary>Originals and settings of a local sequence to the server.</summary>
Upload,
/// <summary>Renders of a local copy to the server.</summary>
SendExports,
/// <summary>Originals of a server sequence into the local archive.</summary>
DownloadOriginals,
/// <summary>Proxies of a server sequence into the proxy cache.</summary>
DownloadProxies,
/// <summary>Renders of a server sequence into the local copy's exports.</summary>
GetExports,
}
/// <summary>
/// Parameters of upload and download jobs. The job's target is the local sequence for an upload and the server
/// sequence for a download.
/// </summary>
/// <param name="ArchiveId">Archive that receives downloaded originals when the sequence has no local copy yet.</param>
/// <param name="Automatic">
/// The new photos of a pair integrated with automatic photos (owner review 6, design §7.3): they go only into the two
/// sequences of that pair, never into one the job would create or retarget.
/// </param>
internal sealed record SyncJobParameters(string Server, SyncDownloadMode? Mode = null, Guid? ArchiveId = null, SyncUploadMode? Upload = null,
bool? Automatic = null)
{
[JsonIgnore]
public bool IsAutomatic => Automatic == true;
public static SyncJobParameters? Read(Job job)
{
if (job.Kind is not (JobKind.UploadCommit or JobKind.Download) || job.ParametersJson is not { } json) return null;
try
{
return JsonSerializer.Deserialize(json, SyncJobJson.Default.SyncJobParameters);
}
catch (JsonException)
{
return null;
}
}
public SyncTransferKind Kind(JobKind kind) => (kind, Mode, Upload) switch
{
(JobKind.UploadCommit, _, SyncUploadMode.Exports) => SyncTransferKind.SendExports,
(JobKind.UploadCommit, _, _) => SyncTransferKind.Upload,
(_, SyncDownloadMode.Proxies, _) => SyncTransferKind.DownloadProxies,
(_, SyncDownloadMode.Exports, _) => SyncTransferKind.GetExports,
_ => SyncTransferKind.DownloadOriginals,
};
}
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, UseStringEnumConverter = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(SyncJobParameters))]
internal sealed partial class SyncJobJson : JsonSerializerContext;
/// <summary>Runs upload and download jobs with a connection to their server; the job fails with a reason when this PC is not connected to it.</summary>
internal sealed class SyncJobHandler(JobKind kind, SyncTransfers transfers, ISyncConnector connector) : IJobHandler
{
public JobKind Kind => kind;
public async Task RunAsync(JobContext context)
{
var parameters = SyncJobParameters.Read(context.Job) ?? throw new InvalidDataException("The job has no sync parameters.");
if (context.Job.TargetIds is not [var target, ..]) throw new InvalidDataException("The job has no target sequence.");
using var remote = await connector.ConnectAsync(parameters.Server, context.Cancellation).ConfigureAwait(false)
?? throw new InvalidOperationException(Loc.L("Errors.Sync.NotConnected"));
switch (parameters.Kind(kind))
{
case SyncTransferKind.Upload:
await transfers.UploadAsync(context, remote, target, parameters.IsAutomatic).ConfigureAwait(false);
break;
case SyncTransferKind.SendExports:
await transfers.SendExportsAsync(context, remote, target).ConfigureAwait(false);
break;
case SyncTransferKind.DownloadProxies:
await transfers.DownloadProxiesAsync(context, remote, target).ConfigureAwait(false);
break;
case SyncTransferKind.GetExports:
await transfers.GetExportsAsync(context, remote, target).ConfigureAwait(false);
break;
default:
await transfers.DownloadOriginalsAsync(context, remote, target, parameters.ArchiveId, parameters.IsAutomatic).ConfigureAwait(false);
break;
}
}
}