feat(menu): 队列处理完即退出,不再空等 20 秒
- worker 取不到任务就直接退出(保留 2 秒让汇总留在屏幕上) - 后到的入队若发现没有 worker 会自动拉起新的,不会漏处理 - 移除不再使用的 WorkerIdleSeconds 与空闲轮询逻辑
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
把任务写进队列后立即退出,由 worker 统一处理。多次点击只会拉起一个窗口。
|
把任务写进队列后立即退出,由 worker 统一处理。多次点击只会拉起一个窗口。
|
||||||
|
|
||||||
2) 队列 worker(内部使用):-Worker
|
2) 队列 worker(内部使用):-Worker
|
||||||
串行消费队列;队列空且空闲超过 $WorkerIdleSeconds 秒后自行退出。
|
串行消费队列;队列处理完即退出(不等新任务)。
|
||||||
同一时刻只允许一个 worker(靠 worker.lock 独占锁保证)。
|
同一时刻只允许一个 worker(靠 worker.lock 独占锁保证)。
|
||||||
|
|
||||||
3) 直接执行(调试/手动):-Action <动作> -Paths <路径>
|
3) 直接执行(调试/手动):-Action <动作> -Paths <路径>
|
||||||
@@ -50,7 +50,6 @@ $QueueLock = Join-Path $QueueDir 'queue.lock'
|
|||||||
$WorkerLock = Join-Path $QueueDir 'worker.lock'
|
$WorkerLock = Join-Path $QueueDir 'worker.lock'
|
||||||
$SpawnLock = Join-Path $QueueDir 'spawn.lock'
|
$SpawnLock = Join-Path $QueueDir 'spawn.lock'
|
||||||
$WorkerLog = Join-Path $QueueDir 'worker.log'
|
$WorkerLog = Join-Path $QueueDir 'worker.log'
|
||||||
$WorkerIdleSeconds = 20
|
|
||||||
$PwshPath = (Get-Process -Id $PID).Path
|
$PwshPath = (Get-Process -Id $PID).Path
|
||||||
|
|
||||||
$ActionPlan = @{
|
$ActionPlan = @{
|
||||||
@@ -180,31 +179,16 @@ function Invoke-Worker {
|
|||||||
try { $Host.UI.RawUI.WindowTitle = 'SceneMarshal 队列' } catch { }
|
try { $Host.UI.RawUI.WindowTitle = 'SceneMarshal 队列' } catch { }
|
||||||
Write-WorkerLog ("worker 启动(stdin重定向={0} stdout重定向={1})" -f `
|
Write-WorkerLog ("worker 启动(stdin重定向={0} stdout重定向={1})" -f `
|
||||||
[Console]::IsInputRedirected, [Console]::IsOutputRedirected)
|
[Console]::IsInputRedirected, [Console]::IsOutputRedirected)
|
||||||
Write-Host '📋 SceneMarshal 队列已启动(空闲 20 秒后自动关闭)' -ForegroundColor Cyan
|
Write-Host '📋 SceneMarshal 队列已启动(处理完即退出)' -ForegroundColor Cyan
|
||||||
Write-Host ''
|
Write-Host ''
|
||||||
|
|
||||||
$done = 0
|
$done = 0
|
||||||
$failed = 0
|
$failed = 0
|
||||||
$idle = 0
|
|
||||||
$summarized = $false
|
|
||||||
|
|
||||||
while ($true) {
|
while ($true) {
|
||||||
$item = Take-QueueItem
|
$item = Take-QueueItem
|
||||||
if ($null -eq $item) {
|
if ($null -eq $item) { break } # 队列空就退出,不再等待新任务
|
||||||
# 队列刚空时就报一次汇总,用户在窗口自动关闭前也能看到
|
|
||||||
if (($done + $failed) -gt 0 -and -not $summarized) {
|
|
||||||
Write-Host ('📊 已处理 {0} 项:成功 {1},失败 {2};{3} 秒内无新任务将自动关闭' -f `
|
|
||||||
($done + $failed), $done, $failed, $WorkerIdleSeconds) -ForegroundColor Cyan
|
|
||||||
$summarized = $true
|
|
||||||
}
|
|
||||||
if ($idle -ge $WorkerIdleSeconds) { break }
|
|
||||||
$idle += 2
|
|
||||||
Start-Sleep -Seconds 2
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
$idle = 0
|
|
||||||
$summarized = $false
|
|
||||||
$name = Split-Path -Leaf $item.path
|
$name = Split-Path -Leaf $item.path
|
||||||
Write-Host ('─── [{0}] {1}' -f $item.action, $name) -ForegroundColor Yellow
|
Write-Host ('─── [{0}] {1}' -f $item.action, $name) -ForegroundColor Yellow
|
||||||
Write-WorkerLog ("开始处理 {0} :: {1}" -f $item.action, $item.path)
|
Write-WorkerLog ("开始处理 {0} :: {1}" -f $item.action, $item.path)
|
||||||
@@ -231,9 +215,10 @@ function Invoke-Worker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Write-Host ('📊 本次共处理 {0} 项:成功 {1},失败 {2}' -f ($done + $failed), $done, $failed) -ForegroundColor Cyan
|
Write-Host ('📊 本次共处理 {0} 项:成功 {1},失败 {2}' -f ($done + $failed), $done, $failed) -ForegroundColor Cyan
|
||||||
Write-Host (' 队列已空,{0} 秒内无新任务将自动关闭(详细记录见 worker.log)' -f $WorkerIdleSeconds) -ForegroundColor DarkGray
|
Write-Host ' 队列已空,窗口即将关闭(详细记录见 worker.log)' -ForegroundColor DarkGray
|
||||||
Write-WorkerLog ("worker 退出:成功 {0},失败 {1}" -f $done, $failed)
|
Write-WorkerLog ("worker 退出:成功 {0},失败 {1}" -f $done, $failed)
|
||||||
$workerLock.Close()
|
$workerLock.Close()
|
||||||
|
if (($done + $failed) -gt 0) { Start-Sleep -Seconds 2 } # 留两秒给汇总留在屏幕上
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user