@leiyi-works/dsh-auto-restart 0.2.3 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -4
- package/client.js +4 -2
- package/index.js +78 -37
- package/lib/install-watchdog.ps1 +12 -1
- package/lib/watchdog.ps1 +12 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,11 +28,19 @@
|
|
|
28
28
|
|
|
29
29
|
首次启动时插件会检测:没装就自动弹一个**中文控制台窗口**,说明用途并等你按回车安装。安装做三件事:把脚本拷到 `~/.dsh/dsh-auto-restart/`、在启动文件夹写一个隐藏引线、立刻启动看门狗。之后每次登录 Windows 它自动起来,所有自动重启都不再经过 WMI。
|
|
30
30
|
|
|
31
|
-
-
|
|
32
|
-
-
|
|
31
|
+
- 面板里只显示看门狗状态(运行中/未安装/已安装无心跳/脚本已更新待重装),**没有安装/卸载按钮**;
|
|
32
|
+
- **每次 DSH 启动(含自动重启后)体检一次**:看门狗没在回答(进程没了/心跳过期)就自动打开安装窗口;不再做周期检查(避免工作中反复弹窗);这期间自动重启会退回 WMI 兜底;
|
|
33
|
+
- **脚本更新检测**:心跳里带脚本哈希;插件升级后如果后台还跑着旧脚本,会再弹一次安装窗口把看门狗重启到新脚本(安装器会先停掉旧进程);
|
|
34
|
+
- 急停(可选):在 `~/.dsh/dsh-auto-restart/` 里新建空文件 `disabled`,插件就不再动手(这和直接在插件列表里停用插件基本等价,留着只是应急用);删掉即恢复;
|
|
33
35
|
- 卸载:在 `~/.dsh/dsh-auto-restart/` 里执行 `powershell -File install-watchdog.ps1 -Uninstall`;
|
|
34
36
|
- 看门狗日志:`~/.dsh/dsh-auto-restart/watchdog.log`。
|
|
35
37
|
|
|
38
|
+
### 多机同步注意
|
|
39
|
+
|
|
40
|
+
- 看门狗本体和安装脚本在 `~/.dsh/dsh-auto-restart/`,会跟着云端同步走;**启动文件夹的引线不在 `~/.dsh` 里,不会被同步**——所以每台机器第一次都会弹安装窗口,各自装一次即可(路径全部运行时由 `%USERPROFILE%` / `%APPDATA%` 计算,不同用户名不影响);
|
|
41
|
+
- 其它机器同步过来的旧重启请求(`watchdog-request.json` / `restart-request.json`)会在启动时按 exe 路径校验后清理,不会被本机看门狗误执行;
|
|
42
|
+
- `disabled`(急停文件)和 `config.json` 会同步:一处急停等于全机队急停、一处改配置全机队生效;如果不想要这个行为,把这两个文件加进云端同步的排除项。
|
|
43
|
+
|
|
36
44
|
> 少数安全软件(如 360)对「PowerShell 操作 WMI」有行为规则:装了看门狗后重启不走 WMI,就不会再因此弹窗;安装看门狗那一次仍可能被提示一次,允许即可。
|
|
37
45
|
|
|
38
46
|
会话记录在磁盘上,重启后回到同一个会话。**没有每小时次数上限**,只有 60 秒冷却防止连环重启。
|
|
@@ -71,8 +79,6 @@
|
|
|
71
79
|
| `maxPerHour` | `0` | 每小时上限;`0` = 不限制 |
|
|
72
80
|
| `waitForIdle` | `true` | 等会话空闲再重启 |
|
|
73
81
|
| `idleSettleMs` | `10000` | 最后一次会话活动后安静多久再重启;防止刚发出、回合还没起来的消息被吞掉 |
|
|
74
|
-
| `watchdogCheckMs` | `60000` | 多久体检一次后台看门狗 |
|
|
75
|
-
| `watchdogReinstallCooldownMs` | `300000` | 看门狗不在时,重新打开安装窗口的最小间隔 |
|
|
76
82
|
| `restartDelayMs` | `2000` | 守护进程收到请求后等多久动手 |
|
|
77
83
|
| `maximizeOnRestart` | `true` | 重启后最大化主窗口 |
|
|
78
84
|
| `exePath` | `''` | 应用可执行文件,留空自动推导 |
|
package/client.js
CHANGED
|
@@ -271,7 +271,9 @@ window.__ModuleLoader__.load({
|
|
|
271
271
|
h('span', { style: styles.val }, status.watchdog === undefined
|
|
272
272
|
? '—'
|
|
273
273
|
: status.watchdog.alive === true
|
|
274
|
-
?
|
|
274
|
+
? status.watchdog.scriptStale === true
|
|
275
|
+
? '运行中(脚本已更新,等待重装)'
|
|
276
|
+
: `运行中(pid=${status.watchdog.heartbeat?.pid ?? '?'})`
|
|
275
277
|
: status.watchdog.installed === true
|
|
276
278
|
? '已安装但当前没有心跳(重启会用 WMI 兜底)'
|
|
277
279
|
: '未安装(DSH 启动时会弹安装窗口;未安装时重启走 WMI,可能触发安全软件提示)'),
|
|
@@ -350,7 +352,7 @@ window.__ModuleLoader__.load({
|
|
|
350
352
|
save.busy ? h('p', { style: styles.note }, '正在保存…') : null,
|
|
351
353
|
statusCard,
|
|
352
354
|
settingsCard,
|
|
353
|
-
h('p', { style: styles.note }, '配置文件:' + (status?.configPath ?? '—')
|
|
355
|
+
h('p', { style: styles.note }, '配置文件:' + (status?.configPath ?? '—')),
|
|
354
356
|
),
|
|
355
357
|
)
|
|
356
358
|
}
|
package/index.js
CHANGED
|
@@ -79,10 +79,6 @@ const DEFAULTS = Object.freeze({
|
|
|
79
79
|
* 防止「刚按发送、回合还没起来」的瞬间被重启吞掉,导致输入框回填已发出的文字。
|
|
80
80
|
*/
|
|
81
81
|
idleSettleMs: 10000,
|
|
82
|
-
/** 多久检查一次后台看门狗是否活着(毫秒)。 */
|
|
83
|
-
watchdogCheckMs: 60000,
|
|
84
|
-
/** 看门狗不在时,重新打开安装窗口的最小间隔(毫秒)。 */
|
|
85
|
-
watchdogReinstallCooldownMs: 300000,
|
|
86
82
|
/** 守护进程收到请求后等多久动手(毫秒),留时间给当前回合收尾。 */
|
|
87
83
|
restartDelayMs: 2000,
|
|
88
84
|
/** 重启后把主窗口最大化(开关在左侧「自动重启」面板里)。 */
|
|
@@ -190,6 +186,15 @@ function ensureWatchdogFiles() {
|
|
|
190
186
|
}
|
|
191
187
|
}
|
|
192
188
|
|
|
189
|
+
/** SHA1 of the watchdog script shipped with this plugin version. */
|
|
190
|
+
function watchdogScriptHash() {
|
|
191
|
+
try {
|
|
192
|
+
return createHash('sha1').update(readFileSync(join(PKG_DIR, 'lib', 'watchdog.ps1'))).digest('hex')
|
|
193
|
+
} catch {
|
|
194
|
+
return ''
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
193
198
|
/** The watchdog's heartbeat, when it is fresh enough to be trusted. */
|
|
194
199
|
function readWatchdogHeartbeat() {
|
|
195
200
|
const parsed = readJsonFile(watchdogHeartbeatPath())
|
|
@@ -198,7 +203,21 @@ function readWatchdogHeartbeat() {
|
|
|
198
203
|
if (!Number.isFinite(at)) return null
|
|
199
204
|
const ageMs = Date.now() - at
|
|
200
205
|
if (ageMs < 0 || ageMs > 20000) return null
|
|
201
|
-
return {
|
|
206
|
+
return {
|
|
207
|
+
pid: Number(parsed.pid) || 0,
|
|
208
|
+
at: new Date(at).toISOString(),
|
|
209
|
+
ageMs,
|
|
210
|
+
script: typeof parsed.script === 'string' ? parsed.script : '',
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The running watchdog answers but was started from older code. The installer
|
|
216
|
+
* stops it and starts the new script, so this is worth re-prompting for.
|
|
217
|
+
*/
|
|
218
|
+
function watchdogScriptStale(heartbeat) {
|
|
219
|
+
const current = watchdogScriptHash()
|
|
220
|
+
return heartbeat !== null && current !== '' && heartbeat.script !== '' && heartbeat.script !== current
|
|
202
221
|
}
|
|
203
222
|
|
|
204
223
|
function watchdogAlive() {
|
|
@@ -256,29 +275,60 @@ function dispatchToWatchdog(state, cfg, reason) {
|
|
|
256
275
|
}
|
|
257
276
|
|
|
258
277
|
/**
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
278
|
+
* Removes restart requests left by another machine's sync: a request names the
|
|
279
|
+
* exe it was created for, so a request whose exe does not exist here (or differs
|
|
280
|
+
* from this machine's app) must never be replayed by the local watchdog.
|
|
281
|
+
*/
|
|
282
|
+
function cleanForeignRequests() {
|
|
283
|
+
const exe = resolveExe(readConfig({}))
|
|
284
|
+
for (const file of [watchdogRequestPath(), requestPath()]) {
|
|
285
|
+
const parsed = readJsonFile(file)
|
|
286
|
+
if (parsed === null || typeof parsed !== 'object') continue
|
|
287
|
+
const wanted = typeof parsed.exe === 'string' ? parsed.exe : ''
|
|
288
|
+
if (wanted === '') {
|
|
289
|
+
try { rmSync(file, { force: true }) } catch {}
|
|
290
|
+
continue
|
|
291
|
+
}
|
|
292
|
+
let same = false
|
|
293
|
+
try {
|
|
294
|
+
same = resolve(wanted) === resolve(exe)
|
|
295
|
+
} catch {
|
|
296
|
+
same = false
|
|
297
|
+
}
|
|
298
|
+
if (same) continue
|
|
299
|
+
try {
|
|
300
|
+
rmSync(file, { force: true })
|
|
301
|
+
logLine('warn', `已清理其它机器同步过来的重启请求:${wanted}`)
|
|
302
|
+
} catch {}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Boot-time watchdog check: once per DSH start (and therefore once after every
|
|
308
|
+
* automatic restart) the plugin verifies the resident watchdog. Missing or running
|
|
309
|
+
* older code means a restart would fall back to WMI, so the console installer is
|
|
310
|
+
* opened. Deliberately not periodic: one check per boot is enough and keeps
|
|
311
|
+
* console windows from popping up while the user works.
|
|
262
312
|
*/
|
|
263
|
-
function
|
|
313
|
+
function bootWatchdogCheck(state) {
|
|
264
314
|
if (!isDesktopHost() || !isRealUserHome()) return
|
|
265
|
-
if (
|
|
266
|
-
state.
|
|
315
|
+
if (state.bootWatchdogChecked === true) return
|
|
316
|
+
state.bootWatchdogChecked = true
|
|
267
317
|
const heartbeat = readWatchdogHeartbeat()
|
|
268
|
-
if (heartbeat !== null) {
|
|
269
|
-
|
|
318
|
+
if (heartbeat !== null && !watchdogScriptStale(heartbeat)) {
|
|
319
|
+
logLine('info', `后台看门狗运行中:pid=${heartbeat.pid} 心跳=${heartbeat.at}`)
|
|
270
320
|
return
|
|
271
321
|
}
|
|
272
|
-
if (
|
|
273
|
-
|
|
274
|
-
if (
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
: '后台看门狗未安装,重新打开安装窗口')
|
|
322
|
+
if (heartbeat !== null) {
|
|
323
|
+
logLine('warn', `后台看门狗运行的脚本已过期(${heartbeat.script.slice(0, 8)} ≠ ${watchdogScriptHash().slice(0, 8)}),打开安装窗口更新`)
|
|
324
|
+
} else if (watchdogInstalled()) {
|
|
325
|
+
logLine('warn', '后台看门狗已安装但当前没有心跳,打开安装窗口重新启动')
|
|
326
|
+
} else {
|
|
327
|
+
logLine('info', '后台看门狗未安装:打开安装窗口')
|
|
279
328
|
}
|
|
280
329
|
offerWatchdogInstall()
|
|
281
330
|
}
|
|
331
|
+
|
|
282
332
|
function offerWatchdogInstall() {
|
|
283
333
|
const installer = watchdogInstallerPath()
|
|
284
334
|
if (!existsSync(installer)) {
|
|
@@ -386,8 +436,6 @@ function sanitizeConfig(raw) {
|
|
|
386
436
|
cfg.cooldownMs = num(raw.cooldownMs, cfg.cooldownMs, 0, 3600000)
|
|
387
437
|
cfg.maxPerHour = num(raw.maxPerHour, cfg.maxPerHour, 0, 100)
|
|
388
438
|
cfg.idleSettleMs = num(raw.idleSettleMs, cfg.idleSettleMs, 0, 600000)
|
|
389
|
-
cfg.watchdogCheckMs = num(raw.watchdogCheckMs, cfg.watchdogCheckMs, 10000, 3600000)
|
|
390
|
-
cfg.watchdogReinstallCooldownMs = num(raw.watchdogReinstallCooldownMs, cfg.watchdogReinstallCooldownMs, 60000, 86400000)
|
|
391
439
|
cfg.restartDelayMs = num(raw.restartDelayMs, cfg.restartDelayMs, 0, 600000)
|
|
392
440
|
cfg.logMaxBytes = num(raw.logMaxBytes, cfg.logMaxBytes, 4096, 64 * 1024 * 1024)
|
|
393
441
|
for (const key of ['exePath', 'supervisorScript', 'powershellPath', 'profileDir']) {
|
|
@@ -705,8 +753,7 @@ function createState(profileDir) {
|
|
|
705
753
|
lastReason: '',
|
|
706
754
|
lastSkipLog: 0,
|
|
707
755
|
watchdogMissingLogged: false,
|
|
708
|
-
|
|
709
|
-
lastWatchdogOfferAt: 0,
|
|
756
|
+
bootWatchdogChecked: false,
|
|
710
757
|
}
|
|
711
758
|
}
|
|
712
759
|
|
|
@@ -720,8 +767,6 @@ function tick(baseConfig) {
|
|
|
720
767
|
const now = Date.now()
|
|
721
768
|
state.restarts = state.restarts.filter((t) => now - t < 3600000)
|
|
722
769
|
|
|
723
|
-
watchdogHealthCheck(state, cfg, now)
|
|
724
|
-
|
|
725
770
|
// A restart is already on its way; the process will disappear shortly.
|
|
726
771
|
if (state.dispatchedAt > 0 && now - state.dispatchedAt < 60000) return
|
|
727
772
|
|
|
@@ -874,6 +919,7 @@ function installRoutes(ctx, baseConfig) {
|
|
|
874
919
|
watchdog: {
|
|
875
920
|
alive: watchdogAlive(),
|
|
876
921
|
heartbeat: readWatchdogHeartbeat(),
|
|
922
|
+
scriptStale: watchdogScriptStale(readWatchdogHeartbeat()),
|
|
877
923
|
installed: watchdogInstalled(),
|
|
878
924
|
startupEntry: startupEntryPath(),
|
|
879
925
|
logPath: watchdogLogPath(),
|
|
@@ -989,18 +1035,11 @@ export function apply(ctx, config) {
|
|
|
989
1035
|
|
|
990
1036
|
logLine('info', `${MARK} ${VERSION} 已加载:profile=${profileDir} dryRun=${cfg.dryRun} cooldown=${cfg.cooldownMs}ms cap=${cfg.maxPerHour === 0 ? '无' : cfg.maxPerHour + '/h'} maximize=${cfg.maximizeOnRestart} desktop=${isDesktopHost()}`)
|
|
991
1037
|
|
|
992
|
-
// Resident watchdog: keep its files current,
|
|
993
|
-
//
|
|
1038
|
+
// Resident watchdog: keep its files current, drop requests synced from another
|
|
1039
|
+
// machine, then check the watchdog once for this boot (no periodic checks).
|
|
994
1040
|
ensureWatchdogFiles()
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
logLine('info', `后台看门狗运行中:pid=${heartbeat.pid} 心跳=${heartbeat.at}`)
|
|
998
|
-
} else if (watchdogInstalled()) {
|
|
999
|
-
logLine('warn', '后台看门狗已安装但当前没有心跳(可能刚登录还没起来),本次重启会用 WMI 兜底')
|
|
1000
|
-
} else {
|
|
1001
|
-
logLine('info', '后台看门狗未安装:会自动打开安装窗口')
|
|
1002
|
-
}
|
|
1003
|
-
watchdogHealthCheck(state, cfg)
|
|
1041
|
+
cleanForeignRequests()
|
|
1042
|
+
bootWatchdogCheck(state)
|
|
1004
1043
|
|
|
1005
1044
|
// Track busy sessions so a restart never cuts an answer.
|
|
1006
1045
|
ctx.effect(() => ctx.on('api-session/status', (sessionId, running) => {
|
|
@@ -1066,4 +1105,6 @@ export const _internals = {
|
|
|
1066
1105
|
watchdogAlive,
|
|
1067
1106
|
readWatchdogHeartbeat,
|
|
1068
1107
|
watchdogInstalled,
|
|
1108
|
+
watchdogScriptHash,
|
|
1109
|
+
watchdogScriptStale,
|
|
1069
1110
|
}
|
package/lib/install-watchdog.ps1
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
param(
|
|
2
2
|
[switch]$Uninstall
|
|
3
3
|
)
|
|
4
4
|
|
|
@@ -13,6 +13,7 @@ $DataDir = Join-Path $env:USERPROFILE '.dsh\dsh-auto-restart'
|
|
|
13
13
|
$StartupDir = Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs\Startup'
|
|
14
14
|
$StartupFile = Join-Path $StartupDir 'dsh-auto-restart-watchdog.vbs'
|
|
15
15
|
$WatchdogVbs = Join-Path $DataDir 'watchdog.vbs'
|
|
16
|
+
$WatchdogPs1 = Join-Path $DataDir 'watchdog.ps1'
|
|
16
17
|
$Heartbeat = Join-Path $DataDir 'watchdog-heartbeat.json'
|
|
17
18
|
$StopFile = Join-Path $DataDir 'watchdog.stop'
|
|
18
19
|
|
|
@@ -84,6 +85,16 @@ try {
|
|
|
84
85
|
) -join "`r`n"
|
|
85
86
|
Write-Utf8NoBom $StartupFile $vbs
|
|
86
87
|
if (Test-Path -LiteralPath $StopFile) { Remove-Item -LiteralPath $StopFile -Force -ErrorAction SilentlyContinue }
|
|
88
|
+
# Stop a watchdog that is still running old code, so the new script answers.
|
|
89
|
+
try {
|
|
90
|
+
$existing = @(Get-CimInstance Win32_Process -Filter "Name='powershell.exe'" -ErrorAction SilentlyContinue |
|
|
91
|
+
Where-Object { $_.CommandLine -ne $null -and $_.CommandLine.Contains($WatchdogPs1) })
|
|
92
|
+
foreach ($proc in $existing) {
|
|
93
|
+
Write-Host (' 停止旧看门狗进程 pid=' + $proc.ProcessId) -ForegroundColor DarkGray
|
|
94
|
+
Stop-Process -Id $proc.ProcessId -Force -ErrorAction SilentlyContinue
|
|
95
|
+
}
|
|
96
|
+
} catch {}
|
|
97
|
+
if (Test-Path -LiteralPath $Heartbeat) { Remove-Item -LiteralPath $Heartbeat -Force -ErrorAction SilentlyContinue }
|
|
87
98
|
Start-Process -FilePath 'wscript.exe' -ArgumentList ('//B "' + $WatchdogVbs + '"') | Out-Null
|
|
88
99
|
$installed = Test-Heartbeat 15
|
|
89
100
|
if (-not $installed) { throw '看门狗已启动但 15 秒内没有心跳,请把窗口内容发给助手。' }
|
package/lib/watchdog.ps1
CHANGED
|
@@ -149,6 +149,17 @@ function Write-Result([string]$Phase, [string]$Detail) {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
Write-Log ('watchdog started pid=' + $PID)
|
|
152
|
+
# The heartbeat carries the hash of the script that is actually running, so the
|
|
153
|
+
# plugin can tell "the code on disk changed but this process still runs the old
|
|
154
|
+
# one" and ask for a reinstall (which restarts the watchdog).
|
|
155
|
+
$ScriptHash = ''
|
|
156
|
+
try {
|
|
157
|
+
$own = $MyInvocation.MyCommand.Path
|
|
158
|
+
if (-not [string]::IsNullOrWhiteSpace($own)) {
|
|
159
|
+
$ScriptHash = (Get-FileHash -LiteralPath $own -Algorithm SHA1).Hash.ToLowerInvariant()
|
|
160
|
+
}
|
|
161
|
+
} catch {}
|
|
162
|
+
Write-Log ('script hash=' + $ScriptHash)
|
|
152
163
|
$handledId = ''
|
|
153
164
|
$lastHeartbeat = [datetime]::MinValue
|
|
154
165
|
while ($true) {
|
|
@@ -156,7 +167,7 @@ while ($true) {
|
|
|
156
167
|
$now = Get-Date
|
|
157
168
|
if (($now - $lastHeartbeat).TotalSeconds -ge 5) {
|
|
158
169
|
try {
|
|
159
|
-
$beat = '{"pid":' + $PID + ',"at":"' + $now.ToString('o') + '"}'
|
|
170
|
+
$beat = '{"pid":' + $PID + ',"at":"' + $now.ToString('o') + '","script":"' + $ScriptHash + '"}'
|
|
160
171
|
[System.IO.File]::WriteAllText($HeartbeatPath, $beat, (New-Object System.Text.UTF8Encoding($false)))
|
|
161
172
|
} catch {}
|
|
162
173
|
$lastHeartbeat = $now
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leiyi-works/dsh-auto-restart",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"title": "自动重启",
|
|
6
6
|
"description": "装/更插件后自动重启 DSH 并回到原会话:检测 profile 插件文件变化,一直等所有会话空闲且最近活动安静,由常驻后台看门狗(登录自启、不碰 WMI)或 WMI 守护先杀主进程再拉起(可最大化窗口),带冷却、试运行、日志和右侧原生侧边栏面板。",
|