fix(menu): 串行化 worker 拉起,消除多选时闪出多个窗口
- Explorer 对多选是并发发起 N 个入队调用,此前每个调用都在 worker 抢到锁 之前判断「没有 worker」,于是各拉起一个窗口,抢不到锁的随即退出 - 改为入队后先拿 spawn.lock 再复查并拉起,其他并发调用排队后看到已有 worker 就直接跳过 - worker 启动时先写一行「进程启动(抢锁中)」日志,便于核对是否只拉起一次 - 实测:4 次快速入队 → 只拉起 1 个 worker,顺序处理完 4 项
This commit is contained in:
@@ -48,6 +48,7 @@ $QueueDir = Join-Path $env:LOCALAPPDATA 'SceneMarshal'
|
||||
$QueueFile = Join-Path $QueueDir 'queue.jsonl'
|
||||
$QueueLock = Join-Path $QueueDir 'queue.lock'
|
||||
$WorkerLock = Join-Path $QueueDir 'worker.lock'
|
||||
$SpawnLock = Join-Path $QueueDir 'spawn.lock'
|
||||
$WorkerLog = Join-Path $QueueDir 'worker.log'
|
||||
$WorkerIdleSeconds = 20
|
||||
$PwshPath = (Get-Process -Id $PID).Path
|
||||
@@ -161,8 +162,10 @@ function Invoke-Action([string]$itemAction, [string]$target) {
|
||||
# ---------------------------------------------------------------- worker 模式
|
||||
|
||||
function Invoke-Worker {
|
||||
Write-WorkerLog 'worker 进程启动(抢锁中)'
|
||||
$workerLock = Enter-FileLock $WorkerLock 200
|
||||
if ($null -eq $workerLock) {
|
||||
Write-WorkerLog '已有 worker 在运行,本次进程直接退出'
|
||||
Write-Host '已有 worker 在运行,退出。' -ForegroundColor DarkGray
|
||||
return 0
|
||||
}
|
||||
@@ -237,7 +240,17 @@ $target = Resolve-Target $Paths
|
||||
if ($Enqueue) {
|
||||
if (-not $target) { exit 0 } # 没拿到路径就静默退出,不弹窗
|
||||
Add-QueueItem $Action $target
|
||||
if (-not (Test-WorkerAlive)) { Start-Worker }
|
||||
if (-not (Test-WorkerAlive)) {
|
||||
# 多选时 Explorer 会同时发起 N 个入队调用,这里必须串行化,
|
||||
# 否则每个调用都会在 worker 抢到锁之前各拉起一个窗口。
|
||||
$spawnLock = Enter-FileLock $SpawnLock 8000
|
||||
try {
|
||||
if ($null -ne $spawnLock -and -not (Test-WorkerAlive)) { Start-Worker }
|
||||
}
|
||||
finally {
|
||||
if ($null -ne $spawnLock) { $spawnLock.Close() }
|
||||
}
|
||||
}
|
||||
exit 0
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user