using System.Globalization; using System.Text.Json; using Titano.Core.Import; using Titano.Core.Localization; using Titano.Core.Model; using Titano.Data.Jobs; namespace Titano.Data.Archiving.Jobs; /// /// : executes the confirmed plan in its parameters; progress in bytes. Checkpoint: the /// number of plan items done. A removed card fails the job at a batch boundary; resuming it after re-insertion /// continues there. /// internal sealed class ImportJobHandler(ImportService imports) : IJobHandler { public JobKind Kind => JobKind.Import; public async Task RunAsync(JobContext context) { var parameters = (context.Job.ParametersJson is { Length: > 0 } json ? JsonSerializer.Deserialize(json, ArchiveJobJson.Default.ImportJobParameters) : null) ?? throw new InvalidOperationException($"Import job {context.Job.Id} has no plan."); var start = int.TryParse(context.Checkpoint, NumberStyles.None, CultureInfo.InvariantCulture, out var next) ? next : 0; var final = await imports.ExecuteAsync(parameters, context.Job.Id, start, async batch => { foreach (var failed in batch.Results.Where(r => r.Status == ImportItemStatus.Failed)) context.Log(Loc.L("Archive.Log.ImportFailed", failed.Item.SourcePath, failed.Error)); await context.CheckpointAsync(batch.NextItem.ToString(CultureInfo.InvariantCulture), batch.Progress.BytesDone, batch.Progress.BytesTotal) .ConfigureAwait(false); }, p => context.ReportAsync(p.BytesDone, p.BytesTotal, p.Current), context.Cancellation).ConfigureAwait(false); context.Log(Loc.L("Archive.Log.ImportDone", final.Copied, final.Skipped, final.Failed)); } }