Files
Chronos/src/Titano.Data/Sync/SyncJobHandler.cs
T
Claude b79e4ed6d9 feat(sync): client sync engine with links, settings conflicts, transfers and background agent
Per-sequence state between the local index and a server from listings and
manifest diffs; version-counter settings sync with explicit conflicts; upload,
original and proxy download jobs; WebSocket-driven background agent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 22:23:23 +00:00

47 lines
2.3 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Jobs;
namespace Titano.Data.Sync;
/// <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>
internal sealed record SyncJobParameters(string Server, SyncDownloadMode? Mode = null, Guid? ArchiveId = null);
[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 = (context.Job.ParametersJson is { } json ? JsonSerializer.Deserialize(json, SyncJobJson.Default.SyncJobParameters) : null)
?? 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 (kind, parameters.Mode)
{
case (JobKind.UploadCommit, _):
await transfers.UploadAsync(context, remote, target).ConfigureAwait(false);
break;
case (JobKind.Download, SyncDownloadMode.Proxies):
await transfers.DownloadProxiesAsync(context, remote, target).ConfigureAwait(false);
break;
default:
await transfers.DownloadOriginalsAsync(context, remote, target, parameters.ArchiveId).ConfigureAwait(false);
break;
}
}
}