using System.IO;
using Chronos.Core.Diagnostics;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Projects;
using Chronos.Data.Workspaces;
namespace Chronos.App.Workspace;
///
/// What the workspace takes of its drive (Settings › Workspace, phase 5c): the projects' media (proxies and originals, which
/// the clean-up and the size limit may remove), the whole folder, and its trash ().
///
/// The bytes of every project's media/ folder.
/// The bytes of the whole workspace folder, its trash included.
/// The bytes of the workspace's trash.
/// The entries of the workspace's trash (one per copy removed or originals set aside).
public sealed record WorkspaceUsage(long MediaBytes, long TotalBytes, long TrashBytes, int TrashItems)
{
/// A workspace that holds nothing.
public static WorkspaceUsage None { get; } = new(0, 0, 0, 0);
}
/// One entry of the workspace's trash as Empty… lists it: its folder name, size, files and when it was put there.
public sealed record WorkspaceTrashItem(string Name, long Bytes, int Files, DateTimeOffset Trashed);
///
/// The plan of Settings › Workspace › Trash › Empty… (phase 5c): every entry of the workspace's trash and their bytes, and a
/// fingerprint over their names, sizes and times. Emptying erases exactly these entries, and only while the fingerprint
/// still matches (the user types the number of entries to confirm); schedules and the size limit never empty the trash.
///
public sealed record WorkspaceTrashPlan(string Folder, IReadOnlyList Items, long Bytes, string Fingerprint)
{
/// Nothing to erase: Empty… is disabled.
public bool IsEmpty => Items.Count == 0;
///
/// The trash of each home the plan lists (owner review 5, separate homes: local\ and each servers\<id>\
/// keep their own), each with its own fingerprint; emptying erases each one only while all of them still match.
///
public IReadOnlyList Listings { get; init; } = [];
}
/// What emptying the workspace's trash erased.
public sealed record WorkspaceTrashResult(int Items, long Bytes);
///
/// The workspace's clean-up side (phase 5c, P4): its usage, the clean-up of media (: Settings ›
/// Workspace › Clean after and Size limit, run by the workspace-cleanup schedule and, for the size limit, after each
/// copy of media), the plan and emptying of its trash, and what a local copy's removal keeps of renders not sent and
/// originals the home lost.
///
public sealed partial class WorkspaceHost
{
private readonly SemaphoreSlim _cleaning = new(1, 1);
///
/// The workspace project open in Edit now (any thread may ask); null when none is. The clean-up never removes the media of
/// that one. Unset (as in a test without the shell), no project counts as open.
///
public Func? OpenProject { get; set; }
/// What the workspace takes of its drive (), measured off the UI thread; none while its folder is not made.
public Task UsageAsync(CancellationToken cancellationToken = default) => Task.Run(() =>
{
var root = Root;
if (!Directory.Exists(root)) return WorkspaceUsage.None;
var media = 0L;
foreach (var store in Homes.All())
{
if (!Directory.Exists(store.Root)) continue;
foreach (var folder in Directory.EnumerateDirectories(store.Root).Where(d => Guid.TryParseExact(Path.GetFileName(d), "N", out _)))
{
cancellationToken.ThrowIfCancellationRequested();
media += WorkspaceCleaner.Measure(Path.Combine(folder, WorkspaceProject.MediaFolder)).Bytes;
}
}
var trash = Homes.Trash(cancellationToken);
return new WorkspaceUsage(media, WorkspaceCleaner.Measure(root).Bytes, trash.Sum(t => t.Bytes), trash.Sum(t => t.Entries.Count));
}, cancellationToken);
/// The plan of emptying the trash of every home of the workspace (); nothing is erased.
public Task PlanTrashAsync(CancellationToken cancellationToken = default) => Task.Run(() =>
{
var listings = Homes.Trash(cancellationToken);
var folder = listings.Count == 1 ? listings[0].Folder : WorkspaceTrash.FolderOf(Root);
var fingerprint = string.Join('|', listings.Select(l => l.Folder + "=" + l.Fingerprint));
return new WorkspaceTrashPlan(folder, [.. listings.SelectMany(l => l.Entries).Select(e => new WorkspaceTrashItem(e.Name, e.Bytes, e.Files, e.Trashed))
.OrderBy(i => i.Trashed)], listings.Sum(l => l.Bytes), fingerprint)
{ Listings = listings };
}, cancellationToken);
///
/// Erases the entries of from the workspace's trash for good, after the user typed their number;
/// refused when the trash changed since the plan (Errors.Workspace.TrashChanged).
///
/// The trash changed since the plan; nothing was erased.
/// An entry could not be erased (a file held open); the others were.
public Task EmptyTrashAsync(WorkspaceTrashPlan plan, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(plan);
return Task.Run(() =>
{
EnsureNotMoving();
IReadOnlyList listings = plan.Listings.Count > 0 ? plan.Listings
: [new WorkspaceTrashListing(plan.Folder, [.. plan.Items.Select(i => new WorkspaceTrashEntry(i.Name, i.Bytes, i.Files, i.Trashed))], plan.Bytes,
plan.Fingerprint)];
try
{
// Every trash is checked before any is emptied: a plan is erased whole or not at all.
foreach (var listing in listings)
{
var now = WorkspaceTrash.List(Path.GetDirectoryName(listing.Folder)!, cancellationToken);
if (!string.Equals(now.Fingerprint, listing.Fingerprint, StringComparison.Ordinal))
throw new WorkspaceTrashChangedException($"The trash {now.Folder} changed after it was listed.");
}
var (items, bytes) = (0, 0L);
foreach (var listing in listings.Where(l => !l.IsEmpty))
{
var (erased, freed) = WorkspaceTrash.Empty(Path.GetDirectoryName(listing.Folder)!, listing, cancellationToken);
items += erased;
bytes += freed;
}
return new WorkspaceTrashResult(items, bytes);
}
catch (WorkspaceTrashChangedException ex)
{
Log.Info("workspace", $"The workspace's trash was not emptied: {ex.Message}");
throw new WorkspaceException(Loc.L("Errors.Workspace.TrashChanged"), ex);
}
}, cancellationToken);
}
///
/// One run of the workspace's clean-up (): with , Settings ›
/// Workspace › Clean after, then Size limit (the workspace-clean step of the schedule); without, the size limit
/// alone, never taking (after a copy of its media). Null when the workspace cannot be
/// cleaned now (the index not open, the folder being moved); a folder not made yet has nothing to clean. One run at a time.
///
public async Task CleanAsync(bool cleanAfter, Guid? justCopied = null, CancellationToken cancellationToken = default)
{
if (_db() is not { } db || IsMoving || !IsEnabled) return null;
var root = Root;
if (!Directory.Exists(root)) return WorkspaceCleanReport.Nothing;
var options = new WorkspaceCleanOptions
{
CleanAfterDays = cleanAfter ? Math.Max(0, _settings.WorkspaceCleanAfterDays) : 0,
SizeLimitBytes = Math.Max(0, _settings.WorkspaceSizeLimitGb) * (1L << 30),
Opened = OpenedSnapshot(),
IsOpen = id => OpenProject?.Invoke() == id,
JustCopied = justCopied,
};
if (options is { CleanAfterDays: 0, SizeLimitBytes: 0 }) return WorkspaceCleanReport.Nothing;
await _cleaning.WaitAsync(cancellationToken).ConfigureAwait(false);
try
{
var cleaner = new WorkspaceCleaner(AllSyncs(), db, HomeOf) { LockWait = LockWait };
return await Task.Run(() => cleaner.CleanAsync(options, cancellationToken), cancellationToken).ConfigureAwait(false);
}
finally
{
_cleaning.Release();
}
}
///
/// A job of the engine changed (any thread): once a copy of media into this workspace is done, the size limit runs, never
/// taking the project just copied. Nothing else follows from a job here.
///
public void OnJobChanged(Job job)
{
ArgumentNullException.ThrowIfNull(job);
if (job is not { Kind: JobKind.WorkspaceCopy, State: JobState.Done } || _settings.WorkspaceSizeLimitGb <= 0) return;
if (WorkspaceJobs.CopyParameters(job) is not { } copied || !IsUnderRoot(copied.WorkspaceRoot)) return;
_ = Task.Run(async () =>
{
try
{
if (await CleanAsync(cleanAfter: false, copied.ProjectId).ConfigureAwait(false) is { Cleaned.Count: > 0 } report)
Log.Info("workspace", $"Size limit after the copy of project {copied.ProjectId}: {report.Cleaned.Count} project(s) cleaned, {report.Freed} bytes freed");
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
Log.Warning("workspace", $"The size limit after the copy of project {copied.ProjectId} did not run", ex);
}
});
}
/// The folder is the workspace's own or one of its homes (local\, servers\<id>\).
private bool IsUnderRoot(string folder)
{
if (string.IsNullOrWhiteSpace(folder)) return false;
var root = Path.TrimEndingDirectorySeparator(Path.GetFullPath(Root));
var full = Path.TrimEndingDirectorySeparator(Path.GetFullPath(folder));
return string.Equals(full, root, StringComparison.OrdinalIgnoreCase)
|| full.StartsWith(root + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase);
}
/// When each project was opened last, as Settings hold it now (written on the UI thread: copied until a copy reads whole).
private IReadOnlyDictionary OpenedSnapshot()
{
for (var attempt = 0; ; attempt++)
{
try
{
return new Dictionary(_settings.WorkspaceOpened);
}
catch (InvalidOperationException) when (attempt < 5)
{
// Written while it was copied: copied again.
}
}
}
///
/// What a local copy's removal plan counts besides its versions (,
/// ): renders of its ledger not sent, and originals the home no longer
/// holds (all of them while the home cannot be asked: they go to the trash then, never deleted).
///
private async Task<(int UnsentRenders, int LastOriginals)> RemovalMediaAsync(WorkspaceProject project, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(project);
var unsent = WorkspaceCleaner.UnsentRenders(project);
var last = await LastOriginalsOfAsync(project, cancellationToken).ConfigureAwait(false);
return (unsent, last);
}
///
/// A removal without Discard is refused while the project's ledger holds renders not sent: Send first queues their
/// sending home (unless one is queued or running already), and the copy goes once they are there.
///
/// Renders wait to be sent.
private async Task EnsureRendersSentAsync(WorkspaceProject project, BundleWorkspace workspace, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(project);
ArgumentNullException.ThrowIfNull(workspace);
if (WorkspaceCleaner.UnsentRenders(project) == 0) return;
try
{
await QueueRenderSendAsync(project, workspace, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (ex is DatabaseException or InvalidOperationException or IOException or InvalidDataException)
{
Log.Warning("workspace", $"The renders of project {project.ProjectId} could not be queued to be sent", ex);
}
throw new WorkspaceException(WorkspaceText.ForHome("Errors.Workspace.UnsentRenders", !workspace.Home.IsArchive));
}
///
/// Before a removed copy's media are deleted, under the project's lease: every file of its folder of originals, named by
/// the media index or not (an index that cannot be read names none), hashed now and deleted only when the home's frames,
/// read now, hold a photo of that SHA-256; every other one moved into (the copy's trash entry),
/// never deleted, and every one when the home cannot be asked ().
///
/// The originals that could be neither deleted nor moved: they stay in the folder of originals.
private async Task ClearOriginalsAsync(WorkspaceProject project, string aside, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(project);
ArgumentException.ThrowIfNullOrEmpty(aside);
if (!WorkspaceCleaner.HoldsOriginalFiles(project)) return 0;
// The working state went into the entry first: the home is read from there.
BundleHome? home = null;
try
{
home = ProjectBundle.Load(Path.Combine(aside, WorkspaceProject.BundleFile)).Workspace?.Home;
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
Log.Info("workspace", $"The home of removed project {project.ProjectId} is not known: every original it holds is kept ({ex.Message})");
}
var held = home is null ? null : await HomeFramesAsync(project, home, cancellationToken).ConfigureAwait(false);
var (deleted, trashed, _, left) = WorkspaceOriginals.ClearOriginals(project, held?.Frames, aside);
Log.Info("workspace", $"Originals of removed project {project.ProjectId}: {deleted} deleted (the home holds them), {trashed} kept in {aside}"
+ (left > 0 ? $", {left} left where they are (held open)" : string.Empty));
return left;
}
/// The frames of as it holds them now; null (logged) when it cannot be asked.
private async Task HomeFramesAsync(WorkspaceProject project, BundleHome home, CancellationToken cancellationToken)
{
try
{
return HomeOf(home) is { } reached ? await reached.FramesAsync(cancellationToken).ConfigureAwait(false) : null;
}
catch (Exception ex) when (ex is HomeUnavailableException or ProjectException or DatabaseException or IOException or InvalidDataException)
{
// Not known: every original counts as the last one, and goes to the trash.
Log.Info("workspace", $"The home of project {project.ProjectId} could not be asked for its frames: {ex.Message}");
return null;
}
}
///
/// What a removal plan counts of the originals that go to the trash rather than away: the files of the folder of
/// originals the media index does not name (every one when the index cannot be read), plus the frames whose recorded
/// original the home no longer holds as it is (every one while the home cannot be asked). The removal hashes each file
/// itself; this count is what the plan says, never what decides.
///
private async Task LastOriginalsOfAsync(WorkspaceProject project, CancellationToken cancellationToken)
{
if (!WorkspaceCleaner.HoldsOriginalFiles(project)) return 0;
var files = Directory.EnumerateFiles(project.OriginalsDirectory)
.Where(f => !f.EndsWith(".part", StringComparison.OrdinalIgnoreCase)).Select(Path.GetFileName).OfType().ToList();
WorkspaceMedia? media;
try
{
media = project.ReadMedia();
}
catch (InvalidDataException ex)
{
Log.Info("workspace", $"The media index of project {project.ProjectId} cannot be read: every original counts as one to keep ({ex.Message})");
return files.Count;
}
var named = media?.Frames.Where(f => f.Original is not null).Select(f => Path.GetFileName(f.Original!)).ToHashSet(StringComparer.OrdinalIgnoreCase) ?? [];
var unnamed = files.Count(f => !named.Contains(f));
if (media is null || named.Count == 0) return unnamed;
HomeFrames? held = null;
try
{
held = await HomeFramesAsync(project, project.ReadBundle().Workspace!.Home, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
Log.Info("workspace", $"The home of project {project.ProjectId} is not known: {ex.Message}");
}
return unnamed + WorkspaceCleaner.LastOriginals(media, held).Count(f => files.Contains(Path.GetFileName(f.Original!), StringComparer.OrdinalIgnoreCase));
}
///
/// Edit › Export › Send again: queues the sending home of every render of the project not sent yet
/// (), unless one is queued, running or paused already.
///
/// True when a send is queued or waits already; false when nothing waits to be sent.
/// The workspace is being moved, or the index or the engine is not open.
public Task SendRendersAsync(Guid projectId, CancellationToken cancellationToken = default) => Task.Run(async () =>
{
EnsureNotMoving();
if (_engine() is null || _db() is null) throw new WorkspaceException(Loc.L("Editor.Error.NoEngine"));
var project = Existing(projectId);
if (WorkspaceCleaner.UnsentRenders(project) == 0) return false;
await QueueRenderSendAsync(project, project.ReadBundle().Workspace!, cancellationToken).ConfigureAwait(false);
return true;
}, cancellationToken);
/// A send of the project's renders is queued, running or paused (Send again waits for it).
public async Task RenderSendPendingAsync(Guid projectId, Guid sequenceId, CancellationToken cancellationToken = default)
{
if (_db() is not { } db) return false;
foreach (var pending in await db.Jobs.ListPendingAsync(JobKind.WorkspaceSend, sequenceId, cancellationToken).ConfigureAwait(false))
{
if (WorkspaceJobs.SendParameters(pending)?.ProjectId == projectId) return true;
}
return false;
}
/// Queues the sending of the project's renders not sent yet (), unless one is queued or running.
private async Task QueueRenderSendAsync(WorkspaceProject project, BundleWorkspace workspace, CancellationToken cancellationToken)
{
if (_engine() is not { } engine || _db() is not { } db) return;
foreach (var pending in await db.Jobs.ListPendingAsync(JobKind.WorkspaceSend, workspace.Home.SequenceId, cancellationToken).ConfigureAwait(false))
{
if (WorkspaceJobs.SendParameters(pending)?.ProjectId == project.ProjectId) return;
}
var title = project.ReadBundle().Sequence.Title ?? string.Empty;
var job = await engine.EnqueueAsync(WorkspaceJobs.Send(workspace.Home.SequenceId, title, project.Store.Root, project.ProjectId, [], workspace.Home.Server),
cancellationToken: cancellationToken).ConfigureAwait(false);
Log.Info("workspace", $"The renders of project {project.ProjectId} not sent yet are queued to be sent (job {job.Id})");
}
}