diff --git a/Modules/Controller.ps1 b/Modules/Controller.ps1 index 70a75c9..16f2d1d 100644 --- a/Modules/Controller.ps1 +++ b/Modules/Controller.ps1 @@ -418,6 +418,24 @@ $script:fullPipelineEngineWorker = { $classified = Get-OrCreateFullPipelineChapterClassification -Cache $ChapterClassificationCache -ChapterInfo $chapterInfo ` -MagickExe $MagickExe -LooseImageExtensions $LooseImageExtensions -EventQueue $EventQueue -ZipBlockReasons $ZipBlockReasons + # A .webp named like one of the chapter's animated .avif files is + # the animated engine's output file, whatever it holds now (a + # 0-byte one is left by a force-killed run). Converting it here + # would race that engine and could leave only a still of the animation. + $images = $classified.Images + if ($AnimatedEnabled -and $classified.AnimatedFiles.Count -gt 0) { + $animatedOutputs = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) + foreach ($f in $classified.AnimatedFiles) { $null = $animatedOutputs.Add([System.IO.Path]::ChangeExtension($f.FullName, ".webp")) } + $images = [System.Collections.Generic.List[System.IO.FileInfo]]::new() + foreach ($f in $classified.Images) { + if ($animatedOutputs.Contains($f.FullName)) { + Write-Log ("Not converting '" + $f.FullName + "' to .jxl: an animated .avif of the same name converts to this file.") + } else { + $images.Add($f) + } + } + } + $skippedAnimated = [System.Collections.Generic.List[System.IO.FileInfo]]::new() if (-not $AnimatedEnabled) { $imagePathSet = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) @@ -429,7 +447,7 @@ $script:fullPipelineEngineWorker = { ConvertedCount = 0; FailedCount = 0; SkippedCount = 0; DeletedCount = 0; DeleteFailedCount = 0 InspectFailedCount = 0; ForceKilledCount = 0 SkippedPaths = [System.Collections.Generic.List[string]]::new() - TotalFiles = $classified.Images.Count + $skippedAnimated.Count + TotalFiles = $images.Count + $skippedAnimated.Count } $chapterResults[$chapterInfo] = $cr @@ -449,7 +467,7 @@ $script:fullPipelineEngineWorker = { } $allJobs = [System.Collections.Generic.List[hashtable]]::new() - foreach ($img in $classified.Images) { + foreach ($img in $images) { $srcPath = $img.FullName if ((Get-NeverConvertedExtensions) -icontains [System.IO.Path]::GetExtension($srcPath)) { Write-Log ("SKIPPING '" + $srcPath + "': not converted by this project (see Get-NeverConvertedExtensions's own comment for why). File left untouched.") -Level Warn diff --git a/README.md b/README.md index ee63a2c..3bcf84e 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ Full Pipeline leaves a chapter folder as it is, and says why in the summary and - a file failed to convert, - a file name clashes with an existing `.jxl` that wasn't made from it, - an AVIF couldn't be inspected, or -- the folder holds both an `.avif` and the `.webp` made from it (when "Delete Original" is off for animated files). +- the folder holds both an `.avif` and a `.webp` of the same name (for example, when "Delete Original" is off for animated files). Fix the cause and run it again. Finished chapters are skipped. diff --git a/_Harness/Controller.Harness.ps1 b/_Harness/Controller.Harness.ps1 index 2b98efa..b7c2193 100644 --- a/_Harness/Controller.Harness.ps1 +++ b/_Harness/Controller.Harness.ps1 @@ -833,7 +833,7 @@ Write-Host "" # ============================================================================== # Scenario H: CCR wiring - real CleanConsoleRendering.ps1 functions get -# called, series resolves once its one chapter completes, merged Detail +# called, series resolves once its chapters complete, merged Detail # string actually concatenates both engines' sub-summaries. # ============================================================================== Write-Host "=== Scenario H: CCR event emission with a merged Warning detail ===" -ForegroundColor Cyan @@ -841,6 +841,14 @@ $chH = New-Dir "scenario-h\CCR Series\Chapter 1" New-FakeImageIn -DirPath $chH -Name "page1.jpg" | Out-Null $webpH = [System.IO.Path]::ChangeExtension((New-FakeAvifIn -DirPath $chH -Name "anim1.avif"), ".webp") [System.IO.File]::WriteAllText($webpH, "pre-existing webp") # forces a Skipped (Warning) animated outcome, Overwrite defaults false +# Chapter 2 gets a Warning too, from a skipped animated .webp, with no .avif +# of the same name to keep it from being zipped. +$chH2 = New-Dir "scenario-h\CCR Series\Chapter 2" +New-FakeImageIn -DirPath $chH2 -Name "page1.jpg" | Out-Null +$webpBytesH2 = New-Object byte[] 24 +[System.Text.Encoding]::ASCII.GetBytes("VP8X").CopyTo($webpBytesH2, 12) +$webpBytesH2[20] = 0x02 +[System.IO.File]::WriteAllBytes((Join-Path $chH2 "anim2.webp"), $webpBytesH2) $script:CleanConsoleOutputEnabled = $true try { @@ -849,9 +857,10 @@ try { $script:CleanConsoleOutputEnabled = $false } -Test-Assert ($script:ccrActiveSeries.Count -eq 0) "the series resolved and left the active registry once its chapter completed" +Test-Assert ($script:ccrActiveSeries.Count -eq 0) "the series resolved and left the active registry once its chapters completed" Test-Assert ($totalsH.ConvertedAnimatedSkipped -eq 1) "the animated file was skipped (pre-existing output, Overwrite off)" -Test-Assert ($totalsH.ZipCreatedSuccess -eq 1) "a Warning-tier outcome (a skip, not a failure) did NOT block zipping" +Test-Assert ($totalsH.ConvertedImageSkipped -eq 1 -and $totalsH.ZipCreatedSuccess -eq 1 -and (Test-Path -LiteralPath ($chH2 + ".zip"))) "a Warning-tier outcome (a skip, not a failure) did NOT block zipping" +Test-Assert ((Test-Path -LiteralPath $webpH) -and -not (Test-Path -LiteralPath ($chH + ".zip"))) "the chapter holding an .avif and its .webp keeps both and isn't zipped" Write-Host "" # ============================================================================== @@ -1871,6 +1880,10 @@ Write-Host "" # by a force-killed run) as not converted, with Overwrite off - it # reconverts instead of skipping and zipping the unconverted AVIF. A real # (non-empty) .webp in a second chapter is still skipped. +# The image engine must leave that .webp alone: it's the animated engine's +# output file. Converting it too would turn the new animation's first frame +# into a .jxl and delete the .webp, with the .avif already gone. One process +# slot (ZB2) puts the image job behind the animated one, the losing order. # ============================================================================== Write-Host "=== Scenario ZB: a 0-byte .webp from a killed run is reconverted, not skipped (Full Pipeline) ===" -ForegroundColor Cyan $chZB1 = New-Dir "scenario-zb\Series\Chapter 1" @@ -1884,6 +1897,39 @@ $totalsZB = Invoke-FullPipeline -Path (Join-Path $scratchDir "scenario-zb") -Cjx -FullPipelineMaxConcurrentProcs 2 -FullLogging $false -PermanentDelete $true -AnimatedOverwrite $false Test-Assert ($totalsZB.ConvertedAnimatedSuccess -eq 1) ("the 0-byte .webp's source was reconverted (converted " + $totalsZB.ConvertedAnimatedSuccess + ")") Test-Assert ($totalsZB.ConvertedAnimatedSkipped -eq 1) ("only the chapter with a real, non-empty .webp was skipped (skipped " + $totalsZB.ConvertedAnimatedSkipped + ")") +function Get-ZBZipEntries { + param([string]$ZipPath) + if (-not (Test-Path -LiteralPath $ZipPath)) { return "" } + $zip = [System.IO.Compression.ZipFile]::OpenRead($ZipPath) + try { return (@($zip.Entries | Sort-Object FullName | ForEach-Object { $_.FullName + $(if ($_.Length -eq 0) { "(0 bytes)" }) }) -join ",") } finally { $zip.Dispose() } +} +$entriesZB = Get-ZBZipEntries ($chZB1 + ".zip") +Test-Assert ($entriesZB -eq "p03.webp") ("the zip holds the new .webp and no still .jxl of it (got " + $entriesZB + ")") +Test-Assert ($totalsZB.ConvertedImageSuccess -eq 0 -and $totalsZB.ConvertedImageFail -eq 0) ("the image engine didn't touch either .webp (converted " + $totalsZB.ConvertedImageSuccess + ", failed " + $totalsZB.ConvertedImageFail + ")") +Test-Assert ((Test-Path -LiteralPath (Join-Path $chZB2 "p01.avif")) -and (Test-Path -LiteralPath (Join-Path $chZB2 "p01.webp")) -and -not (Test-Path -LiteralPath ($chZB2 + ".zip"))) "the chapter with the real .webp keeps both files and isn't zipped" + +Write-Host "=== Scenario ZB2: the same with one process slot, a normal page alongside, and Overwrite on ===" -ForegroundColor Cyan +$chZB3 = New-Dir "scenario-zb2\Series\Chapter 1" +New-FakeAvifIn -DirPath $chZB3 -Name "p03.avif" -Frames 5 | Out-Null +[System.IO.File]::WriteAllBytes((Join-Path $chZB3 "p03.webp"), [byte[]]@()) +New-FakeImageIn -DirPath $chZB3 -Name "p04.jpg" | Out-Null +$chZB4 = New-Dir "scenario-zb2\Series\Chapter 2" +New-FakeAvifIn -DirPath $chZB4 -Name "p01.avif" -Frames 5 | Out-Null +[System.IO.File]::WriteAllText((Join-Path $chZB4 "p01.webp"), "real earlier output") +$totalsZB2 = Invoke-FullPipeline -Path (Join-Path $scratchDir "scenario-zb2") -CjxlExe $fakeCjxlExe -MagickExe $fakeMagickExe ` + -FullPipelineMaxConcurrentProcs 1 -FullLogging $false -PermanentDelete $true -AnimatedOverwrite $true +$entriesZB3 = Get-ZBZipEntries ($chZB3 + ".zip") +Test-Assert ($entriesZB3 -like "p03.webp,p04.jxl*" -and $entriesZB3 -notlike "*p03.jxl*") ("one slot: the zip holds the new .webp and the normal page's .jxl (got " + $entriesZB3 + ")") +$entriesZB4 = Get-ZBZipEntries ($chZB4 + ".zip") +Test-Assert ($entriesZB4 -eq "p01.webp") ("overwrite on: the replaced .webp is zipped, not a still of it (got " + $entriesZB4 + ")") + +Write-Host "=== Scenario ZB3: with animated conversion off, a same-named .webp is converted as usual ===" -ForegroundColor Cyan +$chZB5 = New-Dir "scenario-zb3\Series\Chapter 1" +New-FakeAvifIn -DirPath $chZB5 -Name "p01.avif" -Frames 5 | Out-Null +[System.IO.File]::WriteAllText((Join-Path $chZB5 "p01.webp"), "a static page") +$totalsZB3 = Invoke-FullPipeline -Path (Join-Path $scratchDir "scenario-zb3") -CjxlExe $fakeCjxlExe -MagickExe $fakeMagickExe ` + -FullPipelineMaxConcurrentProcs 2 -FullLogging $false -PermanentDelete $true -AnimatedConversionEnabled $false +Test-Assert ($totalsZB3.ConvertedImageSuccess -eq 1) ("nothing else writes that .webp, so the image engine converts it (converted " + $totalsZB3.ConvertedImageSuccess + ")") Write-Host "" # ============================================================================== @@ -1936,7 +1982,7 @@ try { $chAP2 = New-Dir "scenario-ap2\Series\Overwrite Off" New-FakeImageIn -DirPath $chAP2 -Name "page1.jpg" | Out-Null New-FakeAvifIn -DirPath $chAP2 -Name "anim.avif" -Frames 5 | Out-Null - [System.IO.File]::WriteAllBytes((Join-Path $chAP2 "anim.webp"), $animatedWebpBytes) # animated, like a real earlier conversion - a static one would be converted to .jxl by the image engine + [System.IO.File]::WriteAllBytes((Join-Path $chAP2 "anim.webp"), $animatedWebpBytes) # animated, like a real earlier conversion - either way the image engine leaves it alone (Scenario ZB) $totalsAP2 = Invoke-APRun -Root "scenario-ap2" -Extra @{ AnimatedOverwrite = $false } $stAP2 = Get-APStatus "Overwrite Off" Test-Assert ($totalsAP2.ConvertedAnimatedSkipped -eq 1) "overwrite-off: sanity - the AVIF was skipped because its .webp exists" diff --git a/module-descriptions.md b/module-descriptions.md index e038057..3ce6459 100644 --- a/module-descriptions.md +++ b/module-descriptions.md @@ -823,7 +823,7 @@ The top of the tool. It holds Full Pipeline (convert images, convert animated AV **How it works: Full Pipeline** - **Two engines, one process budget.** `Invoke-FullPipeline` starts an image engine and, if animated conversion is enabled, an animated engine. It does this with `Invoke-ZipParallelProcessing`, where each "job" is one whole engine (`-MaxParallel` equals the engine count). Each engine's worker runs `Invoke-ChapterDispatch` over every series and chapter in the library. Both share one `SemaphoreSlim` sized to `FullPipelineMaxConcurrentProcs`, so cjxl and magick processes together never exceed it. -- **Engine worker.** Each engine builds its own series/chapter provider and its own job builder, starter, poll, finalize and abort-cleanup scriptblocks. The image engine builds one cjxl job per image (via a magick-to-PNG step first for formats cjxl can't read, such as AVIF, WebP, BMP, HEIC/HEIF and TIFF). It skips animated WebP and the extensions the project never converts. The animated engine builds one magick job per animated AVIF. Completing a job reuses `Complete-ImageDispatchJob` / `Complete-AnimatedDispatchJob` from the pipeline modules. +- **Engine worker.** Each engine builds its own series/chapter provider and its own job builder, starter, poll, finalize and abort-cleanup scriptblocks. The image engine builds one cjxl job per image (via a magick-to-PNG step first for formats cjxl can't read, such as AVIF, WebP, BMP, HEIC/HEIF and TIFF). It skips animated WebP and the extensions the project never converts. When animated conversion is on, it also leaves alone a `.webp` named like one of the chapter's animated `.avif` files, whatever it holds: that's the animated engine's output file, and converting it here could leave only a still of the animation. The animated engine builds one magick job per animated AVIF. Completing a job reuses `Complete-ImageDispatchJob` / `Complete-AnimatedDispatchJob` from the pipeline modules. - **Shared caches.** The first engine to reach a series builds its bundle (chapters, loose images, animated candidates, "extra" folders that aren't chapters) and the other engine reuses it. After getting a series' bundle, the image engine (which always runs) calls `Invoke-SeriesCoverNormalization` on it, outside the bundle's lock so a slow cover conversion never holds up the animated engine; both candidate scans already skip series-root covers, so the order doesn't matter. Its counts travel on a `CoverNormalized` event into the totals' `CoverNormalizedSuccess`/`CoverNormalizedFail`, which `Invoke-Mode` maps to the summary's "Covers Normalised" row (Image Conversion's totals carry the same two fields). The first to reach a chapter classifies it: each AVIF in a chapter is checked for animation once, and is then given to exactly one engine (static to the image engine, animated to the animated engine, undeterminable to neither). Stale loose sources are cleaned there too. Each cache slot is guarded so its factory runs once even when both engines ask at the same moment. - **Events instead of shared state.** Workers don't touch the console or the totals. They put small event hashtables on a `ConcurrentQueue` (`SeriesReady`, `CoverNormalized`, `ChapterStarted`, `ChapterRunning`, `ChapterInspectFailed`, `ChapterStaleCleanup`, `AbortMessage`, `EngineChapterDone`). The main thread drains the queue from `Invoke-ZipParallelProcessing`'s poll callback. - **Merge on the main thread.** Each engine sends `EngineChapterDone` for every chapter it handled (with `Result = $null` if it had nothing there). The first event creates a merge entry; when both engines have reported (or immediately, if animated conversion is off), the chapter is finalized: totals are updated, then `Complete-MergedFullPipelineChapter` zips and deletes. Zipping and folder deletion therefore happen on the main thread while the engines keep working on later chapters. A chapter is marked merged when finalizing ends, which releases any engine that was holding it (see `-ChapterReleaseCheck` in `ChapterDispatch.ps1`).