Skip to content

Commit f529855

Browse files
committed
feat: 添加带超时功能的 Electron 运行脚本,支持 Windows 和 Unix 系统
1 parent 62095be commit f529855

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed

.github/workflows/npm-run-electron.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ jobs:
100100
echo "--------------------";
101101
pnpm install;
102102
103-
- name: npm run electron
103+
- name: npm run electron (auto-exit after 30min)
104104
run: |
105105
echo "======================================================================";
106106
echo "cd packages/gui";
@@ -109,6 +109,10 @@ jobs:
109109
dir || ls -lah;
110110
111111
echo "======================================================================";
112-
echo "npm run electron";
112+
echo "npm run electron with timeout";
113113
echo "--------------------";
114-
npm run electron;
114+
if [ "$RUNNER_OS" = "Windows" ]; then
115+
powershell scripts/run-electron-with-timeout.ps1
116+
else
117+
bash scripts/run-electron-with-timeout.sh
118+
fi
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env node
2+
import { spawn } from 'node:child_process'
3+
import path from 'node:path'
4+
5+
const timeoutMs = Number(process.env.ELECTRON_TIMEOUT_MS ?? 30 * 60 * 1000) // 30 minutes
6+
const workdir = path.resolve(process.argv[2] ?? 'packages/gui')
7+
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm'
8+
9+
console.log(`Starting npm run electron in ${workdir} with ${timeoutMs / 60000} minute timeout...`)
10+
11+
const child = spawn(command, ['run', 'electron'], {
12+
cwd: workdir,
13+
stdio: 'inherit',
14+
})
15+
16+
let timedOut = false
17+
18+
const timer = setTimeout(() => {
19+
timedOut = true
20+
console.log(`\n⏱ Electron run completed ${timeoutMs / 60000} minutes without error, terminating...`)
21+
child.kill('SIGTERM')
22+
setTimeout(() => {
23+
if (!child.killed)
24+
child.kill('SIGKILL')
25+
}, 5000)
26+
}, timeoutMs)
27+
28+
child.on('exit', (code, signal) => {
29+
clearTimeout(timer)
30+
if (timedOut) {
31+
console.log('✅ 程序运行正常')
32+
process.exit(0)
33+
} else {
34+
console.log('❌ 程序运行不正常')
35+
process.exit(1)
36+
}
37+
})
38+
39+
child.on('error', (err) => {
40+
clearTimeout(timer)
41+
console.error('Failed to start electron:', err)
42+
process.exit(1)
43+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
param(
2+
[string]$Workdir = "packages/gui",
3+
[int]$TimeoutSeconds = 1800 # 30 minutes
4+
)
5+
6+
Write-Host "Starting npm run electron in $Workdir with 30 minute timeout..."
7+
8+
$process = Start-Process -FilePath "npm" -ArgumentList "run electron" -WorkingDirectory $Workdir -PassThru -NoNewWindow
9+
10+
$timer = [System.Diagnostics.Stopwatch]::StartNew()
11+
12+
while ($timer.Elapsed.TotalSeconds -lt $TimeoutSeconds) {
13+
if ($process.HasExited) {
14+
Write-Host "❌ 程序运行不正常"
15+
exit 1
16+
}
17+
Start-Sleep -Seconds 1
18+
}
19+
20+
if (!$process.HasExited) {
21+
Write-Host "⏱ Electron run completed 30 minutes without error, terminating..."
22+
$process.Kill()
23+
Write-Host "✅ 程序运行正常"
24+
exit 0
25+
}
26+
else {
27+
Write-Host "❌ 程序运行不正常"
28+
exit 1
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
set -e
3+
4+
workdir="${1:-packages/gui}"
5+
timeout_seconds=1800 # 30 minutes
6+
7+
echo "Starting npm run electron in $workdir with 30 minute timeout..."
8+
9+
cd "$workdir"
10+
# don't exit for sandboxing issues
11+
npm run electron --no-sandbox &
12+
pid=$!
13+
14+
sleep $timeout_seconds &
15+
16+
sleep_pid=$!
17+
18+
wait $sleep_pid 2>/dev/null || true
19+
20+
if kill -0 $pid 2>/dev/null; then
21+
echo "⏱ Electron run completed 30 minutes without error, terminating..."
22+
kill $pid
23+
sleep 5
24+
if kill -0 $pid 2>/dev/null; then
25+
kill -9 $pid
26+
fi
27+
echo "✅ 程序运行正常"
28+
exit 0
29+
else
30+
echo "❌ 程序运行不正常"
31+
exit 1
32+
fi

0 commit comments

Comments
 (0)