fix(menu): 队列积压时重复右键不再无窗口

- check 只读动作免二次确认
- worker 汇总按目录计数并显示批次
- 重复入队但队列有积压时也拉起 worker 清积压
This commit is contained in:
ali
2026-11-28 13:53:19 +08:00
parent 659a6af456
commit 18a7dee439
+32 -12
View File
@@ -62,7 +62,8 @@ $ActionPlan = @{
'archive' = @{ Runner = 'scene-marshal.ps1'; Extra = @('archive') } 'archive' = @{ Runner = 'scene-marshal.ps1'; Extra = @('archive') }
'variety' = @{ Runner = 'scene-marshal.ps1'; Extra = @('variety') } 'variety' = @{ Runner = 'scene-marshal.ps1'; Extra = @('variety') }
'extract-subs' = @{ Runner = 'scene-marshal.ps1'; Extra = @('extract-subs') } 'extract-subs' = @{ Runner = 'scene-marshal.ps1'; Extra = @('extract-subs') }
'check' = @{ Runner = 'scene-marshal.ps1'; Extra = @('check') } # 兼容性校验是纯只读(只出 preflight_report),不值得为它挡一次确认
'check' = @{ Runner = 'scene-marshal.ps1'; Extra = @('check', '--pass') }
'rename' = @{ Runner = 'scene-marshal.ps1'; Extra = @() } 'rename' = @{ Runner = 'scene-marshal.ps1'; Extra = @() }
} }
@@ -216,6 +217,15 @@ function Test-WorkerAlive {
return $false return $false
} }
function Get-PendingQueueCount {
if (-not (Test-Path -LiteralPath $QueueFile)) { return 0 }
try {
return @(Get-Content -LiteralPath $QueueFile -Encoding utf8 |
Where-Object { $_.Trim() }).Count
}
catch { return 0 }
}
function Start-Worker { function Start-Worker {
Start-Process -FilePath $PwshPath ` Start-Process -FilePath $PwshPath `
-ArgumentList @('-NoLogo', '-NoProfile', '-ExecutionPolicy', 'Bypass', -ArgumentList @('-NoLogo', '-NoProfile', '-ExecutionPolicy', 'Bypass',
@@ -310,9 +320,10 @@ function Invoke-Worker {
Write-Host '📋 SceneMarshal 队列已启动' -ForegroundColor Cyan Write-Host '📋 SceneMarshal 队列已启动' -ForegroundColor Cyan
Write-Host '' Write-Host ''
$done = 0 $batches = 0 # 实际跑了几批(一次右键多选合批后算 1 批)
$failed = 0 $okDirs = 0 # 下面三个按「目录」累加:一批 10 个目录成功就是 10
$cancelled = 0 $cancelDirs = 0
$failDirs = 0
$summaryShown = $false $summaryShown = $false
$closeDeadline = $null $closeDeadline = $null
@@ -383,19 +394,20 @@ function Invoke-Worker {
Remove-Item -LiteralPath $CurrentFile -Force -ErrorAction SilentlyContinue Remove-Item -LiteralPath $CurrentFile -Force -ErrorAction SilentlyContinue
} }
$cost = [int]((Get-Date) - $started).TotalSeconds $cost = [int]((Get-Date) - $started).TotalSeconds
$batches++
if ($code -eq 0) { if ($code -eq 0) {
$done++ $okDirs += $targets.Count
Write-Host (" ✅ 完成,用时 {0}s" -f $cost) -ForegroundColor Green Write-Host (" ✅ 完成,用时 {0}s" -f $cost) -ForegroundColor Green
Write-WorkerLog ("完成 {0}({1}s)" -f ($targets -join '; '), $cost) Write-WorkerLog ("完成 {0}({1}s)" -f ($targets -join '; '), $cost)
} }
elseif ($code -eq 2) { elseif ($code -eq 2) {
# 退出码 2 约定为「用户主动取消」,不算失败 # 退出码 2 约定为「用户主动取消」,不算失败
$cancelled++ $cancelDirs += $targets.Count
Write-Host (" ⏭ 已取消(未改动任何文件),用时 {0}s" -f $cost) -ForegroundColor DarkYellow Write-Host (" ⏭ 已取消(未改动任何文件),用时 {0}s" -f $cost) -ForegroundColor DarkYellow
Write-WorkerLog ("已取消 {0}" -f ($targets -join '; ')) Write-WorkerLog ("已取消 {0}" -f ($targets -join '; '))
} }
else { else {
$failed++ $failDirs += $targets.Count
Write-Host (" ⚠️ 失败(退出码 {0}),用时 {1}s" -f $code, $cost) -ForegroundColor Red Write-Host (" ⚠️ 失败(退出码 {0}),用时 {1}s" -f $code, $cost) -ForegroundColor Red
Write-WorkerLog ("失败 {0}(code={1}, {2}s)" -f ($targets -join '; '), $code, $cost) Write-WorkerLog ("失败 {0}(code={1}, {2}s)" -f ($targets -join '; '), $code, $cost)
} }
@@ -405,10 +417,13 @@ function Invoke-Worker {
# 队列已空:只显示一次汇总,然后等 Esc / Enter 关窗 # 队列已空:只显示一次汇总,然后等 Esc / Enter 关窗
if (-not $summaryShown) { if (-not $summaryShown) {
Write-Host ('📊 本次共处理 {0} 项:成功 {1},取消 {2},失败 {3}' -f ` $dirTotal = $okDirs + $cancelDirs + $failDirs
($done + $cancelled + $failed), $done, $cancelled, $failed) -ForegroundColor Cyan $batchNote = if ($batches -gt 1) { "($batches 批)" } else { '' }
Write-Host ('📊 本次共处理 {0} 个目录{1}:成功 {2},取消 {3},失败 {4}' -f `
$dirTotal, $batchNote, $okDirs, $cancelDirs, $failDirs) -ForegroundColor Cyan
Write-Host ' 按 Esc / Enter 关闭窗口;期间有新任务会接着处理(2 分钟无操作自动关闭)' -ForegroundColor DarkGray Write-Host ' 按 Esc / Enter 关闭窗口;期间有新任务会接着处理(2 分钟无操作自动关闭)' -ForegroundColor DarkGray
Write-WorkerLog ("队列处理完毕:成功 {0},取消 {1},失败 {2}" -f $done, $cancelled, $failed) Write-WorkerLog ("队列处理完毕:成功 {0} 个目录,取消 {1},失败 {2}({3} 批)" -f `
$okDirs, $cancelDirs, $failDirs, $batches)
$summaryShown = $true $summaryShown = $true
$closeDeadline = (Get-Date).AddSeconds(120) $closeDeadline = (Get-Date).AddSeconds(120)
} }
@@ -469,8 +484,13 @@ if ($Enqueue) {
exit 0 exit 0
} }
$queued = Add-QueueItem $Action $targets $queued = Add-QueueItem $Action $targets
# 只有真的入队了才需要 worker;全部是重复项时不要白拉一个窗口 $pendingCount = Get-PendingQueueCount
if ($queued -and -not (Test-WorkerAlive)) { # 即使本次全是重复项,只要队列里还有积压且没有 worker,也要拉起 worker 清积压。
# 否则 worker 在关窗/退出瞬间留下的条目会让后续同路径点击永远静默、连窗口都不开。
if (-not (Test-WorkerAlive) -and ($queued -or $pendingCount -gt 0)) {
if (-not $queued -and $pendingCount -gt 0) {
Write-WorkerLog ("重复入队,但队列有 {0} 条积压;拉起 worker 清积压" -f $pendingCount)
}
# 多选时 Explorer 会同时发起 N 个入队调用,这里必须串行化, # 多选时 Explorer 会同时发起 N 个入队调用,这里必须串行化,
# 否则每个调用都会在 worker 抢到锁之前各拉起一个窗口。 # 否则每个调用都会在 worker 抢到锁之前各拉起一个窗口。
$spawnLock = Enter-FileLock $SpawnLock 8000 $spawnLock = Enter-FileLock $SpawnLock 8000