From 0e82f054adf1bcf643af96386cd6d2573bd1b031 Mon Sep 17 00:00:00 2001 From: ali Date: Thu, 19 Nov 2026 18:25:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(menu):=20=E4=B8=B2=E8=A1=8C=E5=8C=96=20work?= =?UTF-8?q?er=20=E6=8B=89=E8=B5=B7=EF=BC=8C=E6=B6=88=E9=99=A4=E5=A4=9A?= =?UTF-8?q?=E9=80=89=E6=97=B6=E9=97=AA=E5=87=BA=E5=A4=9A=E4=B8=AA=E7=AA=97?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Explorer 对多选是并发发起 N 个入队调用,此前每个调用都在 worker 抢到锁 之前判断「没有 worker」,于是各拉起一个窗口,抢不到锁的随即退出 - 改为入队后先拿 spawn.lock 再复查并拉起,其他并发调用排队后看到已有 worker 就直接跳过 - worker 启动时先写一行「进程启动(抢锁中)」日志,便于核对是否只拉起一次 - 实测:4 次快速入队 → 只拉起 1 个 worker,顺序处理完 4 项 --- scripts/explorer-media-rename.ps1 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts/explorer-media-rename.ps1 b/scripts/explorer-media-rename.ps1 index 0b4b834..46324db 100644 --- a/scripts/explorer-media-rename.ps1 +++ b/scripts/explorer-media-rename.ps1 @@ -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 }