Files
Chronos/src/Chronos.App/Server/SyncHost.cs
T
Alby96andClaude Opus 5.5 9ad8c6f7a9 feat: wire separate homes into the desktop (owner review 5 P3.2)
The desktop now uses the per-home layout from P3.1: each home keeps its
own projects, proxies and originals, and a server is known by its id
whichever address reaches it.

- Settings keep the known servers (id, name, last address, certificate,
  aliases, originals archive). Reset and Import keep them; Export leaves
  them out.
- SyncHost records each server the first time it signs in with an id. It
  names the cache and workspace folders by that id, and re-keys the
  folders a server had under its addresses at sign-in. The re-key waits
  while transfer or workspace jobs are pending.
- Download originals goes to the server's own archive. It is created
  beside this PC's archive at the first download, and a toast says
  where. A sequence that already has a local copy keeps receiving into
  that copy's archive. A server without an id keeps the 1.0 behaviour.
- At start, after the index opens and before any job runs, the folders
  are sorted into their homes. A toast reports the result: sorted,
  pending or failed.
- The workspace host gives each home's store its own sync. Find, List,
  usage, clean-up and the trash plan and empty cover every home. Server
  comparisons go through the identity, so a project stays Online when
  its server is reached by another address.
- The render-node downloads and the server import staging move into the
  server's folder of the cache. The cache trim keeps `servers`, and the
  scheduled scan also scans the originals archives.
- Settings shows the Home folders rows (Workspace) and the Known servers
  table (Server). The diagnostics bundle lists the homes.
- The dead `AppPaths.CreateDirectories` and `ThumbnailsDirectory` are
  removed.

Refs #66

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 14:00:46 +01:00

517 lines
30 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.IO;
using Chronos.App.Archiving;
using Chronos.App.Editor;
using Chronos.App.Infrastructure;
using Chronos.Core.Diagnostics;
using Chronos.Core.IO;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data;
using Chronos.Data.Homes;
using Chronos.Data.Jobs;
using Chronos.Data.Projects;
using Chronos.Data.Sync;
using Chronos.Protocol;
using Chronos.Protocol.Client;
namespace Chronos.App.Server;
/// <summary>Makes the sync connection of a signed-in session; <paramref name="server"/> is the key it names the server by (<see cref="SyncHost.Server"/>).</summary>
public delegate ISyncRemote SyncRemoteFactory(Uri address, string token, IReadOnlyCollection<string> trustedFingerprints, string server);
/// <summary>
/// Where Download originals puts a server sequence's photos (<see cref="SyncHost.OriginalsArchiveAsync"/>): the archive's id,
/// and its root when it was made just now (the first download from that server); a null id when there is none to use.
/// </summary>
public sealed record OriginalsTarget(Guid? ArchiveId, string? Created = null);
/// <summary>
/// This PC's sync with the signed-in server (BRIEF §4.4, §14.5): the <see cref="SyncService"/> of the index, the
/// connection of upload and download jobs (<see cref="ISyncConnector"/>, also the workspace copies of server projects), a
/// <see cref="SyncAgent"/> that keeps the table current while a session is open, started and stopped in order as the
/// session changes, the <see cref="UploadHold"/> for metered connections, the connections of editors of server sequences
/// (<see cref="ConnectEditor"/>), the server as the home of workspace projects when it keeps projects (owner review 2, R8,
/// phase 5b: <see cref="ProjectHome"/>, <see cref="ProjectChanged"/>) and the Export "Sync" of finished renders
/// (<see cref="ExportSync"/>). Options come from <see cref="AppSettings.SyncSettingsAutomatically"/>,
/// <see cref="AppSettings.KeepProxiesOffline"/> and <see cref="AppSettings.PauseUploadsOnMetered"/>. A server that announces
/// its stable id (owner review 5, #66) is keyed by it (<see cref="Server"/>) and recorded in
/// <see cref="AppSettings.KnownServers"/> with the address keys it was reached under, which match it all the same
/// (<see cref="Identity"/>); at a sign-in that tells its id, what this PC keeps under those addresses moves to its id
/// (<see cref="HomeLayoutMigration.RekeyAsync"/>) before the agent starts.
/// </summary>
public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDisposable
{
/// <summary>How long a job resumed at start waits for the saved session to be restored.</summary>
private static readonly TimeSpan RestoreWait = TimeSpan.FromSeconds(30);
/// <summary>Jobs that write into the workspace or the servers' homes in the cache: a re-key waits for them.</summary>
private static readonly JobKind[] RekeyHeldBy =
[JobKind.WorkspaceCopy, JobKind.WorkspaceRender, JobKind.WorkspaceSend, JobKind.WorkspaceMove, JobKind.Download, JobKind.UploadCommit, JobKind.VideoCopy];
private readonly Lock _gate = new();
private Task _switch = Task.CompletedTask;
private Credentials? _credentials;
private Running? _running;
private JobEngine? _renders;
/// <summary>The alias table of <see cref="AppSettings.KnownServers"/>, made again whenever a sign-in records a server.</summary>
private ServerIdentity _identity = services.Settings.KnownIdentity();
/// <summary>The client project homes of the session share, made at first use for the credentials it holds.</summary>
private SharedClient? _projects;
/// <summary>Connections over <see cref="ChronosClient"/>; screenshots substitute a fake server.</summary>
public SyncRemoteFactory RemoteFactory { get; set; } = (address, token, trusted, server) =>
new ChronosSyncRemote(new ChronosClient(address, new CertificatePinning(trusted), $"Chronos ({Environment.MachineName})") { Token = token }, ownsClient: true,
server);
/// <summary>Connections of editors of server sequences over <see cref="ChronosClient"/>; screenshots substitute a fake server.</summary>
public RemoteSequenceApiFactory EditorFactory { get; set; } = (address, token, trusted) =>
new ChronosRemoteSequenceApi(new ChronosClient(address, new CertificatePinning(trusted), $"Chronos ({Environment.MachineName})") { Token = token });
/// <summary>The client the session's project homes share (<see cref="ProjectHome"/>); tests substitute one of their own.</summary>
public Func<Uri, string, IReadOnlyCollection<string>, ChronosClient> ProjectClientFactory { get; set; } = (address, token, trusted) =>
new ChronosClient(address, new CertificatePinning(trusted), $"Chronos ({Environment.MachineName})") { Token = token };
/// <summary>The engine; null until the index is open.</summary>
public SyncService? Service { get; private set; }
/// <summary>The background comparison of the signed-in server; null while signed out.</summary>
public SyncAgent? Agent => Volatile.Read(ref _running)?.Agent;
/// <summary>The agent's connection, for the table's own calls (conflict resolution); null while signed out.</summary>
public ISyncRemote? Remote => Volatile.Read(ref _running)?.Remote;
/// <summary>
/// Key of the signed-in server: its id key when it announced its id (<see cref="ServerIdentity.Key(Guid?, Uri)"/>), else
/// its address key (<see cref="SyncService.ServerKey"/>); null while signed out.
/// </summary>
public string? Server => Volatile.Read(ref _credentials) is { } c ? c.Key : null;
/// <summary>The servers known by id and the address keys each was reached under (any thread).</summary>
public ServerIdentity Identity => Volatile.Read(ref _identity);
/// <summary>
/// A known server id came back with another TLS certificate and another name (owner review 5: a copied server database,
/// two servers sharing one id): the server's name now, and the name it had (UI thread).
/// </summary>
public event Action<string, string>? IdentityClash;
/// <summary>The key a server reached at <paramref name="address"/> goes by: the signed-in one's when it is that server, else its address key mapped through the aliases.</summary>
public string KeyOf(Uri address)
{
ArgumentNullException.ThrowIfNull(address);
if (Volatile.Read(ref _credentials) is { } c && c.Address == address) return c.Key;
return Identity.Canonical(SyncService.ServerKey(address));
}
/// <summary>The options, and whether the account may change server sequences (without it this PC's edits are not sent).</summary>
public SyncPolicy Policy => new(services.Settings.SyncSettingsAutomatically, services.Settings.KeepProxiesOffline, Allows(Permissions.Edit));
/// <summary>The signed-in account may send files (originals, renders) to the server.</summary>
public bool CanUpload => Allows(Permissions.Upload);
/// <summary>The signed-in account may queue renders on the server.</summary>
public bool CanRender => Allows(Permissions.Render);
/// <summary>
/// The signed-in server keeps its sequences' projects (owner review 2, R8: the <c>projects</c> capability), so Edit opens
/// its sequences as workspace projects; false while signed out and for an older server, whose sequences Edit opens with
/// proxies as before.
/// </summary>
public bool HasProjects =>
services.Server.IsConnected && services.Server.Flow.Info?.Capabilities is { } capabilities && capabilities.Contains(ServerCapabilities.Projects);
/// <summary>
/// The highest project settings format the signed-in server reads and renders (owner review 1, phase 4); 1 while signed
/// out and for an older server, which does not announce it. Pushes and renders that need more are held back
/// (<see cref="ServerJobs.NeedsNewerServer"/>).
/// </summary>
public int SettingsFormat => SettingsFormatOf(services.Server.IsConnected ? services.Server.Flow.Info : null);
/// <summary>The settings format a server's <c>server/info</c> announces; 1 when it announces none (an older server).</summary>
public static int SettingsFormatOf(ServerInfo? info) => info?.SettingsFormat is > 0 and var format ? format : ProjectSettings.BaseVersion;
/// <summary>The home of a workspace project of server sequence <paramref name="remoteId"/>; null while signed out or when the server keeps no projects.</summary>
public BundleHome? ServerHome(Guid remoteId) => Server is { } server && HasProjects ? new BundleHome(BundleHome.ServerKind, remoteId, null, server) : null;
/// <summary>
/// The project home of a workspace project whose home is a server (<see cref="ServerProjectHome"/> over the session's
/// client); null while signed out, signed in to another server, or when the server keeps no projects: the workspace then
/// shows the project offline and its work waits. A home named by an address the signed-in server was reached under before
/// it told its id is that server's all the same (owner review 5).
/// </summary>
public IProjectHome? ProjectHome(BundleHome home)
{
ArgumentNullException.ThrowIfNull(home);
if (home.IsArchive || !HasProjects || Volatile.Read(ref _credentials) is not { } credentials || !Identity.Same(home.Server, credentials.Key))
return null;
return new ServerProjectHome(SessionClient(credentials), home.SequenceId, services.Server.ServerName, SettingsFormat, credentials.Key);
}
/// <summary>
/// A project of a sequence of the signed-in server changed there (the server's key, and its <c>projectChanged</c>), or,
/// with null, changes may have been missed while its events were lost, or the session with that server started or
/// stopped (its projects become reachable, or offline) (any thread).
/// </summary>
public event Action<string, ProjectChangedEvent?>? ProjectChanged;
/// <summary>Pauses uploads while the connection is metered and the option is on.</summary>
public UploadHold Uploads { get; } = new(services);
/// <summary>The agent started, stopped or compared, or the table of the signed-in server changed (any thread).</summary>
public event Action? Changed;
/// <summary>
/// Creates the engine over the open index; the proxy cache lives in the client's cache folder, one home per server
/// (<c>&lt;cache&gt;\servers\&lt;folder&gt;\proxies</c>, owner review 5), read where 1.0 put it until it is moved.
/// </summary>
public SyncService Open(ChronosDatabase database, Chronos.Data.Archiving.ArchiveService archives, Chronos.Data.Archiving.ArchiveServiceOptions options)
{
var cache = CacheStore.Folder(services.Environment, services.Settings);
Service = new SyncService(database, archives, new SyncOptions
{
Archive = options,
ProxyCacheDirectory = Path.Combine(cache, HomeLayoutMigration.LegacyProxies),
ServersDirectory = CacheStore.ServersFolder(cache),
Identity = () => Identity,
});
Service.Changed += server =>
{
if (Server is { } signedIn && Identity.Same(server, signedIn)) Changed?.Invoke();
};
return Service;
}
/// <summary>Follows the server session (UI thread): starts the agent for a new sign-in, stops it on sign-out.</summary>
public void Update()
{
var session = services.Server;
Credentials? wanted = session.IsConnected && session.Flow is { Address: { } address, Token: { } token }
? new Credentials(address, token, session.Fingerprints, session.Flow.Info?.ServerId is { } id && id != Guid.Empty ? id : null)
: null;
if (wanted is { ServerId: { } serverId } && session.Flow.Info is { } info) Remember(serverId, wanted.Address, info);
Volatile.Write(ref _credentials, wanted);
_switch = SwitchAsync(_switch, wanted);
if (services.Engine is not { } engine) return;
Uploads.Attach(engine);
if (ReferenceEquals(_renders, engine)) return;
if (_renders is not null) _renders.JobChanged -= OnJobChanged;
_renders = engine;
engine.JobChanged += OnJobChanged;
}
/// <summary>The servers known by id were changed elsewhere (Settings › Server › Forget, an import): the alias table follows (UI thread).</summary>
public void KnownServersChanged() => Volatile.Write(ref _identity, services.Settings.KnownIdentity());
/// <summary>The sign-in <see cref="ConnectEditor"/> uses now (another object once signed in anew); null while signed out.</summary>
public object? Session => Volatile.Read(ref _credentials);
/// <summary>A connection for the editor of a sequence of the signed-in server; null while signed out.</summary>
public IRemoteSequenceApi? ConnectEditor() =>
Volatile.Read(ref _credentials) is { } c ? EditorFactory(c.Address, c.Token, c.Fingerprints) : null;
/// <summary>Compares now, once the session switch in progress settled; null while signed out.</summary>
public async Task<SyncSnapshot?> RefreshAsync(CancellationToken ct = default)
{
await _switch.ConfigureAwait(false);
return Agent is { } agent ? await agent.RefreshAsync(ct).ConfigureAwait(false) : null;
}
/// <summary>Compares again soon (an option changed, an edit on this PC).</summary>
public void RequestRefresh() => Agent?.RequestRefresh();
/// <summary>This PC's archive: the one at Settings › Archive's folder, else the only one that is not a server's originals archive.</summary>
public async Task<Archive?> CurrentArchiveAsync(CancellationToken ct = default)
{
if (services.Database is not { } db) return null;
var archives = await db.Archives.ListAsync(ct);
if (services.Settings.LastArchivePath is { Length: > 0 } root && CurrentArchive.At(archives, root) is { } current) return current;
var originals = services.Settings.KnownServers.Select(k => k.OriginalsArchiveId).OfType<Guid>().ToHashSet();
var own = archives.Where(a => !originals.Contains(a.Id)).ToList();
return own.Count == 1 ? own[0] : null;
}
/// <summary>
/// Where Download originals puts the photos of a server sequence (owner review 5, Q1; UI thread): the archive of its local
/// copy when it has one (a copy keeps receiving where it is), else the signed-in server's own originals archive
/// (<see cref="KnownServer.OriginalsArchiveId"/>), made at the first download beside this PC's archive
/// (<see cref="HomeFolders.OriginalsArchiveRoot"/>: <c>D:\ChronosArchive - Studio NAS</c>, named "Studio NAS (server)").
/// A server that announces no id (an older one) has no home of its own: its originals go to this PC's archive, as in 1.0.
/// </summary>
/// <exception cref="IOException">The originals archive's folder cannot be made.</exception>
public async Task<OriginalsTarget> OriginalsArchiveAsync(Sequence? local, CancellationToken ct = default)
{
if (local?.ArchiveId is { } held) return new OriginalsTarget(held);
if (services.Database is not { } db || services.Archives is not { } archives) return new OriginalsTarget(null);
if (Volatile.Read(ref _credentials) is not { ServerId: { } id } || services.Settings.FindKnownServer(id) is not { } known)
return new OriginalsTarget((await CurrentArchiveAsync(ct))?.Id);
var listed = await db.Archives.ListAsync(ct);
if (known.OriginalsArchiveId is { } chosen && listed.Any(a => a.Id == chosen)) return new OriginalsTarget(chosen);
var name = known.Name is { Length: > 0 } n ? n : services.Server.ServerName ?? known.LastAddress ?? id.ToString("N");
var mine = await CurrentArchiveAsync(ct);
var root = HomeFolders.OriginalsArchiveRoot(mine?.RootPath ?? services.Settings.LastArchivePath, name, ArchiveLocations.SuggestRoot(),
path => Directory.Exists(path) || File.Exists(path) || CurrentArchive.At(listed, path) is not null);
var made = await archives.CreateAsync(root, Loc.L("Archive.Name.ServerOriginals", name), cancellationToken: ct);
known.OriginalsArchiveId = made.Id;
services.SavePreferences();
Log.Info("sync", $"The originals of {name} ({id}) go to their own archive {made.RootPath}");
return new OriginalsTarget(made.Id, made.RootPath);
}
/// <summary>
/// The archive Download originals puts a sequence's photos in (<see cref="OriginalsArchiveAsync"/>; UI thread), with what
/// the user is told: where a new originals archive went, or why there is none (<paramref name="notify"/>: title, message, is error).
/// </summary>
public async Task<Guid?> OriginalsForDownloadAsync(Sequence? local, Action<string, string?, bool> notify)
{
ArgumentNullException.ThrowIfNull(notify);
try
{
var target = await OriginalsArchiveAsync(local);
if (target.ArchiveId is null) notify(Loc.L("Errors.Sync.NoArchive"), null, true);
else if (target.Created is { } made) notify(Loc.L("Shell.Homes.Originals"), AppEnvironment.Shown(made), false);
return target.ArchiveId;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidOperationException or ArgumentException or DatabaseException)
{
Log.Warning("sync", "The originals archive of the signed-in server could not be made", ex);
notify(Loc.L("Errors.Sync.NoOriginalsFolder", services.Server.ServerName ?? Server ?? string.Empty), null, true);
return null;
}
}
/// <summary>The archives of the known servers' downloaded originals that the index holds (the scan schedule scans them too).</summary>
public async Task<IReadOnlyList<Archive>> OriginalsArchivesAsync(CancellationToken ct = default)
{
if (services.Database is not { } db) return [];
var ids = services.Settings.KnownServers.Select(k => k.OriginalsArchiveId).OfType<Guid>().ToHashSet();
if (ids.Count == 0) return [];
return [.. (await db.Archives.ListAsync(ct).ConfigureAwait(false)).Where(a => ids.Contains(a.Id))];
}
public async ValueTask<ISyncRemote?> ConnectAsync(string server, CancellationToken cancellationToken)
{
// A job resumed at start runs before the saved session is restored: give the restore a moment.
var until = DateTime.UtcNow + RestoreWait;
while (Volatile.Read(ref _credentials) is null && services.Settings.ProtectedToken is not null && DateTime.UtcNow < until)
await Task.Delay(500, cancellationToken);
// A job queued under an address the server was reached under before it told its id reaches it all the same.
if (Volatile.Read(ref _credentials) is not { } c || !Identity.Same(c.Key, server)) return null;
CacheStore.Prepare(CacheStore.Folder(services.Environment, services.Settings));
return RemoteFactory(c.Address, c.Token, c.Fingerprints, c.Key);
}
/// <summary>
/// Stops the agent once the session switch in progress settled. The app's exit waits for this on the UI thread, so
/// nothing here, in the switch or in the stop continues on it.
/// </summary>
public async ValueTask DisposeAsync()
{
Volatile.Write(ref _credentials, null);
Uploads.Dispose();
if (_renders is not null) _renders.JobChanged -= OnJobChanged;
await _switch.ConfigureAwait(false);
await StopAsync().ConfigureAwait(false);
lock (_gate)
{
_projects?.Client.Dispose();
_projects = null;
}
}
private bool Allows(Permissions permission) => services.Server.User is { } user && (user.Permissions & (long)permission) != 0;
/// <summary>
/// The client of <paramref name="credentials"/>' session, made once for its address and token: another sign-in replaces
/// it (a call still on the old one fails as a server out of reach, and is tried again on the new one).
/// </summary>
private ChronosClient SessionClient(Credentials credentials)
{
lock (_gate)
{
if (_projects is { } current && current.Address == credentials.Address && current.Token == credentials.Token) return current.Client;
_projects?.Client.Dispose();
var client = ProjectClientFactory(credentials.Address, credentials.Token, credentials.Fingerprints);
_projects = new SharedClient(credentials.Address, credentials.Token, client);
return client;
}
}
/// <summary>
/// Export "Sync": a render of this PC that asked for it is sent to the server once done (<see cref="ExportSync"/>),
/// also when it ends while the session is down: the send is queued for the saved session's server and waits for it.
/// </summary>
private void OnJobChanged(Job job)
{
if (job is not { Kind: JobKind.Render, State: JobState.Done } || Service is not { } service || services.Database is not { } db || _renders is not { } engine)
return;
var signedIn = Server;
_ = SendRenderAsync(new ExportSync(db, service, engine), job, signedIn ?? SavedServer(), signedIn is null || CanUpload);
}
/// <summary>The key of the server this PC is signed in to, else of the saved session's; null without either.</summary>
public string? LastServer => Server ?? SavedServer();
/// <summary>Key of the server of the saved session (Disconnect keeps it), its id key when it is known by id; null without one.</summary>
private string? SavedServer() =>
services.Server.HasSavedSession && ServerAddress.TryParse(services.Settings.ServerUrl, out var address) ? Identity.Canonical(SyncService.ServerKey(address)) : null;
private static async Task SendRenderAsync(ExportSync exports, Job job, string? server, bool canUpload)
{
try
{
switch (await exports.SendAsync(job, server, canUpload))
{
case { Send: { } send }:
Log.Info("sync", $"{job.TargetLabel} will be sent to the server (job {send.Id})");
break;
case { Gate: var gate }:
Log.Warning("sync", $"{job.TargetLabel} asked to be sent to the server but was not ({gate})");
break;
}
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
// Nobody awaits this: the render itself succeeded, and the send can be asked for again on the Server screen.
Log.Warning("sync", $"The render {job.TargetLabel} could not be queued for the server", ex);
}
}
/// <summary>
/// Records the signed-in server in <see cref="AppSettings.KnownServers"/> (UI thread): its id, name, address, certificate,
/// and the address key it was reached by as an alias; the settings are written only when something changed. A known id
/// presented with another certificate and another name is told (<see cref="IdentityClash"/>).
/// </summary>
private void Remember(Guid id, Uri address, ServerInfo info)
{
var settings = services.Settings;
var alias = SyncService.ServerKey(address);
var shown = ServerAddress.Display(address);
var known = settings.FindKnownServer(id);
var changed = false;
if (known is null)
{
known = new KnownServer { Id = id, Name = info.Name, LastAddress = shown, Fingerprint = info.TlsFingerprint };
settings.KnownServers.Add(known);
changed = true;
Log.Info("sync", $"Server {info.Name} is known by its id {id} from now on (reached at {shown})");
}
else if (known.Fingerprint is { Length: > 0 } before && info.TlsFingerprint is { Length: > 0 } now
&& !string.Equals(before, now, StringComparison.OrdinalIgnoreCase)
&& !string.Equals(known.Name, info.Name, StringComparison.Ordinal))
{
Log.Warning("sync", $"Server id {id} was {known.Name} with another certificate; it is {info.Name} now: two servers may share one copied database");
IdentityClash?.Invoke(info.Name, known.Name ?? shown);
}
if (!known.Aliases.Contains(alias, StringComparer.OrdinalIgnoreCase))
{
known.Aliases.Add(alias);
changed = true;
}
if (!string.Equals(known.Name, info.Name, StringComparison.Ordinal) || !string.Equals(known.LastAddress, shown, StringComparison.OrdinalIgnoreCase)
|| (info.TlsFingerprint is { Length: > 0 } fingerprint
&& !string.Equals(known.Fingerprint, fingerprint, StringComparison.OrdinalIgnoreCase)))
{
(known.Name, known.LastAddress, known.Fingerprint) = (info.Name, shown, info.TlsFingerprint ?? known.Fingerprint);
changed = true;
}
if (DateTimeOffset.UtcNow - known.LastSeen > TimeSpan.FromHours(1))
{
known.LastSeen = DateTimeOffset.UtcNow;
changed = true;
}
if (!changed) return;
Volatile.Write(ref _identity, settings.KnownIdentity());
services.SavePreferences();
}
/// <summary>
/// Whatever this PC keeps under the address keys the server of <paramref name="credentials"/> was reached by moves to its
/// id (owner review 5, #66: <see cref="HomeLayoutMigration.RekeyAsync"/>), before the agent starts. Skipped while a job
/// writes into the workspace or the cache, or the workspace is being moved: a later sign-in does it, and the aliases match
/// meanwhile. A failure is logged: nothing is lost, and the next sign-in goes on.
/// </summary>
private async Task RekeyAsync(Credentials credentials)
{
if (credentials.ServerId is not { } id || services.Database is not { } db) return;
var aliases = Identity.AliasesOf(id);
if (aliases.Count == 0) return;
try
{
if (await db.Jobs.HasPendingAsync(RekeyHeldBy).ConfigureAwait(false))
{
Log.Info("sync", $"Server {id}: what this PC keeps under its addresses moves to its id at a later sign-in (jobs are under way)");
return;
}
var workspace = services.Workspace;
var migration = new HomeLayoutMigration(new HomeLayoutOptions
{
DataDirectory = services.Environment.DataDirectory,
WorkspaceRoot = workspace.IsEnabled && !workspace.IsMoving ? workspace.Root : null,
CacheRoot = CacheStore.Folder(services.Environment, services.Settings),
Identity = Identity,
Database = db,
});
await migration.RekeyAsync(id, aliases).ConfigureAwait(false);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or DatabaseException)
{
Log.Warning("sync", $"What this PC keeps under the addresses of server {id} could not all move to its id; the next sign-in goes on", ex);
}
}
/// <summary>
/// Switches are chained (each waits for the previous one) and continue on the thread pool: a switch still stopping
/// the old agent when the app exits must not need the UI thread its exit blocks.
/// </summary>
private async Task SwitchAsync(Task previous, Credentials? wanted)
{
await previous.ConfigureAwait(false);
if (Volatile.Read(ref _running) is { } running && wanted is { } same && running.Credentials.Address == same.Address && running.Credentials.Token == same.Token
&& running.Credentials.ServerId == same.ServerId)
return;
var stopped = Volatile.Read(ref _running)?.Remote.Server;
await StopAsync().ConfigureAwait(false);
// The projects of the server left are offline now: their state is read again.
if (stopped is not null) ProjectChanged?.Invoke(stopped, null);
if (wanted is not { } start || Service is not { } service)
{
Changed?.Invoke();
return;
}
await RekeyAsync(start).ConfigureAwait(false);
var remote = RemoteFactory(start.Address, start.Token, start.Fingerprints, start.Key);
var agent = new SyncAgent(service, remote, () => Policy, services.Engine);
agent.Changed += () => Changed?.Invoke();
agent.ProjectChanged += e => ProjectChanged?.Invoke(remote.Server, e);
agent.Start();
Volatile.Write(ref _running, new Running(agent, remote, start));
Log.Info("sync", $"Comparing with {remote.Server}");
Changed?.Invoke();
ProjectChanged?.Invoke(remote.Server, null);
}
private async Task StopAsync()
{
if (Interlocked.Exchange(ref _running, null) is not { } running) return;
await running.Agent.DisposeAsync().ConfigureAwait(false);
running.Remote.Dispose();
Changed?.Invoke();
}
/// <param name="ServerId">The id the server announced (owner review 5); null for an older server.</param>
private sealed record Credentials(Uri Address, string Token, IReadOnlyCollection<string> Fingerprints, Guid? ServerId)
{
/// <summary>The key the server goes by (<see cref="ServerIdentity.Key(Guid?, Uri)"/>).</summary>
public string Key => ServerIdentity.Key(ServerId, Address);
}
/// <summary>The client project homes share, and the session it was made for.</summary>
private sealed record SharedClient(Uri Address, string Token, ChronosClient Client);
/// <summary>The agent of the signed-in session: one reference, so the UI thread never reads half of a switch.</summary>
private sealed record Running(SyncAgent Agent, ISyncRemote Remote, Credentials Credentials);
}