Refactor updater script for Windows application restart handling
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good

- Simplified the script for restarting the application by removing unnecessary process checks and sleep logic.
- Introduced a new variable for the application directory to ensure the correct working directory is set before launching the application.
- Updated the method of executing the restart script to use `spawn` instead of `schtasks`, improving reliability and clarity in the restart process.
This commit is contained in:
Tom Butcher 2026-08-02 23:12:28 +01:00
parent c48b8d72da
commit 4cf4f5e27f

View File

@ -129,37 +129,25 @@ const spawnDetachedWindowsRestart = ({ appLaunchPath, parentPid }) => {
)
const scriptPath = path.join(updateDir, `restart-${parentPid}.bat`)
const appLaunchWin = appLaunchPath.replaceAll('/', '\\')
const scriptWin = scriptPath.replaceAll('/', '\\')
const appDirWin = path.dirname(appLaunchWin)
const script = `@echo off
setlocal
set "PARENT_PID=${parentPid}"
set "APP_LAUNCHER=${appLaunchWin}"
set "APP_DIR=${appDirWin}"
:waitparent
tasklist /FI "PID eq %PARENT_PID%" 2>NUL | find /I "%PARENT_PID%" >NUL && (
tasklist /FI "PID eq %PARENT_PID%" 2>NUL | find "%PARENT_PID%" >NUL && (
timeout /t 1 /nobreak >nul
goto waitparent
)
:waitprocesses
tasklist /FI "IMAGENAME eq launcher.exe" 2>NUL | find /I /N "launcher.exe">NUL && goto waitsleep
tasklist /FI "IMAGENAME eq bun.exe" 2>NUL | find /I /N "bun.exe">NUL && goto waitsleep
tasklist 2>NUL | find /I "bun Helper">NUL && goto waitsleep
goto waitdone
:waitsleep
timeout /t 1 /nobreak >nul
goto waitprocesses
:waitdone
timeout /t 2 /nobreak >nul
cd /d "%APP_DIR%"
start "" "%APP_LAUNCHER%"
for /f "tokens=1" %%t in ('schtasks /query /fo list ^| findstr /i "FarmControlRestart_"') do (
schtasks /delete /tn "%%t" /f >nul 2>&1
)
ping -n 2 127.0.0.1 >nul
del "%~f0"
`
@ -167,15 +155,12 @@ del "%~f0"
mkdirSync(updateDir, { recursive: true })
writeFileSync(scriptPath, script, 'utf8')
const taskName = `FarmControlRestart_${parentPid}_${Date.now()}`
execSync(
`schtasks /create /tn ${quoteBatchArg(taskName)} /tr ${quoteBatchArg(`cmd /c "${scriptWin}"`)} /sc once /st 00:00 /f`,
{ stdio: 'ignore', windowsHide: true }
)
execSync(`schtasks /run /tn ${quoteBatchArg(taskName)}`, {
const child = spawn('cmd.exe', ['/c', scriptPath], {
detached: true,
stdio: 'ignore',
windowsHide: true
})
child.unref()
}
export const scheduleAppRestart = ({ parentPid = process.pid } = {}) => {