using System.Runtime.CompilerServices; using Chronos.App.Server; using Chronos.Core.Model; using Chronos.Data.Jobs; using Chronos.Protocol; namespace Chronos.App.DesignTime; /// /// The sample server's jobs for the Server section's job list (owner review 5, #65), for the screenshots: empty until /// , then a verification and a thumbnail pass running with what each does now, and a proxy pass paused. /// Pause, resume and cancel change the list; no event ever comes. Thread-safe; disposing it does nothing, so it can be reused. /// public sealed class SampleServerJobs : IServerJobsApi { private readonly Lock _gate = new(); private List _jobs = []; /// The sample's running and paused jobs. public void Fill() { var now = SampleData.Now; lock (_gate) { _jobs = [ Job(JobKind.Verify, "Harbour sunset", JobState.Running, 812, 2_400, 38.5, 41, now.AddMinutes(-6), new JobDetail(JobStep.Verify, "Harbour sunset", 812, 2_400, 3, 41, JobPartUnit.Sequence)), Job(JobKind.Thumbs, "Sunrise over Superga", JobState.Running, 1_240, 3_600, 52.0, 45, now.AddMinutes(-3), new JobDetail(JobStep.Thumbs, "Sunrise over Superga", 1_240, 3_600, 2, 7, JobPartUnit.Sequence)), Job(JobKind.Proxies, "Alpine lake, winter", JobState.Paused, 300, 1_800, null, null, now.AddMinutes(-40), new JobDetail(JobStep.Proxies, "Alpine lake, winter", 300, 1_800, 1, 2, JobPartUnit.Sequence)), ]; } } /// Nothing runs on the server again. public void Clear() { lock (_gate) _jobs = []; } public Task> ListJobsAsync(string state, string? cursor, CancellationToken cancellationToken) { lock (_gate) { var items = _jobs.Where(j => j.State == state).ToList(); return Task.FromResult(new Page(items, null, items.Count)); } } public Task GetJobAsync(Guid id, CancellationToken cancellationToken) { lock (_gate) return Task.FromResult(_jobs.Single(j => j.Id == id)); } public Task PauseJobAsync(Guid id, CancellationToken cancellationToken) => SetState(id, JobState.Paused); public Task ResumeJobAsync(Guid id, CancellationToken cancellationToken) => SetState(id, JobState.Running); public Task CancelJobAsync(Guid id, CancellationToken cancellationToken) => SetState(id, JobState.Cancelled); public async IAsyncEnumerable EventsAsync([EnumeratorCancellation] CancellationToken cancellationToken) { await Task.Delay(Timeout.Infinite, cancellationToken).ConfigureAwait(false); yield break; } public void Dispose() { } private Task SetState(Guid id, JobState state) { lock (_gate) { var index = _jobs.FindIndex(j => j.Id == id); var job = _jobs[index] with { State = JobWire.State(state) }; if (state is JobState.Running or JobState.Paused) _jobs[index] = job; else _jobs.RemoveAt(index); return Task.FromResult(job); } } private static JobDto Job(JobKind kind, string target, JobState state, long done, long total, double? rate, double? eta, DateTimeOffset started, JobDetail detail) => new(Guid.NewGuid(), JobWire.Kind(kind), [Guid.NewGuid()], target, JobWire.State(state), JobWire.Priority(JobPriority.Background), done, total, rate, eta, null, null, 1, null, started, started, null, null, JobWire.Detail(detail)); }