@leiyi-works/dsh-auto-restart 0.2.1 → 0.2.2
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 +3 -0
- package/index.js +57 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
首次启动时插件会检测:没装就自动弹一个**中文控制台窗口**,说明用途并等你按回车安装。安装做三件事:把脚本拷到 `~/.dsh/dsh-auto-restart/`、在启动文件夹写一个隐藏引线、立刻启动看门狗。之后每次登录 Windows 它自动起来,所有自动重启都不再经过 WMI。
|
|
30
30
|
|
|
31
31
|
- 面板里只显示看门狗状态(运行中/未安装/已安装无心跳),**没有安装/卸载按钮**;
|
|
32
|
+
- **插件会定期体检**(默认每 60 秒):看门狗没在回答(进程没了/心跳过期)就**自动重新打开安装窗口**(两次提醒最少隔 5 分钟);这期间自动重启会退回 WMI 兜底;
|
|
32
33
|
- 卸载:在 `~/.dsh/dsh-auto-restart/` 里执行 `powershell -File install-watchdog.ps1 -Uninstall`;
|
|
33
34
|
- 看门狗日志:`~/.dsh/dsh-auto-restart/watchdog.log`。
|
|
34
35
|
|
|
@@ -70,6 +71,8 @@
|
|
|
70
71
|
| `maxPerHour` | `0` | 每小时上限;`0` = 不限制 |
|
|
71
72
|
| `waitForIdle` | `true` | 等会话空闲再重启 |
|
|
72
73
|
| `idleSettleMs` | `10000` | 最后一次会话活动后安静多久再重启;防止刚发出、回合还没起来的消息被吞掉 |
|
|
74
|
+
| `watchdogCheckMs` | `60000` | 多久体检一次后台看门狗 |
|
|
75
|
+
| `watchdogReinstallCooldownMs` | `300000` | 看门狗不在时,重新打开安装窗口的最小间隔 |
|
|
73
76
|
| `restartDelayMs` | `2000` | 守护进程收到请求后等多久动手 |
|
|
74
77
|
| `maximizeOnRestart` | `true` | 重启后最大化主窗口 |
|
|
75
78
|
| `exePath` | `''` | 应用可执行文件,留空自动推导 |
|
package/index.js
CHANGED
|
@@ -79,6 +79,10 @@ const DEFAULTS = Object.freeze({
|
|
|
79
79
|
* 防止「刚按发送、回合还没起来」的瞬间被重启吞掉,导致输入框回填已发出的文字。
|
|
80
80
|
*/
|
|
81
81
|
idleSettleMs: 10000,
|
|
82
|
+
/** 多久检查一次后台看门狗是否活着(毫秒)。 */
|
|
83
|
+
watchdogCheckMs: 60000,
|
|
84
|
+
/** 看门狗不在时,重新打开安装窗口的最小间隔(毫秒)。 */
|
|
85
|
+
watchdogReinstallCooldownMs: 300000,
|
|
82
86
|
/** 守护进程收到请求后等多久动手(毫秒),留时间给当前回合收尾。 */
|
|
83
87
|
restartDelayMs: 2000,
|
|
84
88
|
/** 重启后把主窗口最大化(开关在左侧「自动重启」面板里)。 */
|
|
@@ -251,7 +255,30 @@ function dispatchToWatchdog(state, cfg, reason) {
|
|
|
251
255
|
return { ok: true, dryRun: false, via: 'watchdog', delayMs: request.delayMs }
|
|
252
256
|
}
|
|
253
257
|
|
|
254
|
-
/**
|
|
258
|
+
/**
|
|
259
|
+
* Periodic watchdog health check: when the resident watchdog is not answering,
|
|
260
|
+
* re-open the console installer (rate-limited), because a restart without it
|
|
261
|
+
* falls back to WMI and may be blocked by security software.
|
|
262
|
+
*/
|
|
263
|
+
function watchdogHealthCheck(state, cfg, now = Date.now()) {
|
|
264
|
+
if (!isDesktopHost() || !isRealUserHome()) return
|
|
265
|
+
if (now - state.lastWatchdogCheckAt < cfg.watchdogCheckMs) return
|
|
266
|
+
state.lastWatchdogCheckAt = now
|
|
267
|
+
const heartbeat = readWatchdogHeartbeat()
|
|
268
|
+
if (heartbeat !== null) {
|
|
269
|
+
state.watchdogMissingLogged = false
|
|
270
|
+
return
|
|
271
|
+
}
|
|
272
|
+
if (now - state.lastWatchdogOfferAt < cfg.watchdogReinstallCooldownMs) return
|
|
273
|
+
state.lastWatchdogOfferAt = now
|
|
274
|
+
if (!state.watchdogMissingLogged) {
|
|
275
|
+
state.watchdogMissingLogged = true
|
|
276
|
+
logLine('warn', watchdogInstalled()
|
|
277
|
+
? '后台看门狗已安装但当前没有心跳,重新打开安装窗口'
|
|
278
|
+
: '后台看门狗未安装,重新打开安装窗口')
|
|
279
|
+
}
|
|
280
|
+
offerWatchdogInstall()
|
|
281
|
+
}
|
|
255
282
|
function offerWatchdogInstall() {
|
|
256
283
|
const installer = watchdogInstallerPath()
|
|
257
284
|
if (!existsSync(installer)) {
|
|
@@ -359,6 +386,8 @@ function sanitizeConfig(raw) {
|
|
|
359
386
|
cfg.cooldownMs = num(raw.cooldownMs, cfg.cooldownMs, 0, 3600000)
|
|
360
387
|
cfg.maxPerHour = num(raw.maxPerHour, cfg.maxPerHour, 0, 100)
|
|
361
388
|
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)
|
|
362
391
|
cfg.restartDelayMs = num(raw.restartDelayMs, cfg.restartDelayMs, 0, 600000)
|
|
363
392
|
cfg.logMaxBytes = num(raw.logMaxBytes, cfg.logMaxBytes, 4096, 64 * 1024 * 1024)
|
|
364
393
|
for (const key of ['exePath', 'supervisorScript', 'powershellPath', 'profileDir']) {
|
|
@@ -675,7 +704,9 @@ function createState(profileDir) {
|
|
|
675
704
|
busy: new Set(),
|
|
676
705
|
lastReason: '',
|
|
677
706
|
lastSkipLog: 0,
|
|
678
|
-
|
|
707
|
+
watchdogMissingLogged: false,
|
|
708
|
+
lastWatchdogCheckAt: 0,
|
|
709
|
+
lastWatchdogOfferAt: 0,
|
|
679
710
|
}
|
|
680
711
|
}
|
|
681
712
|
|
|
@@ -689,6 +720,8 @@ function tick(baseConfig) {
|
|
|
689
720
|
const now = Date.now()
|
|
690
721
|
state.restarts = state.restarts.filter((t) => now - t < 3600000)
|
|
691
722
|
|
|
723
|
+
watchdogHealthCheck(state, cfg, now)
|
|
724
|
+
|
|
692
725
|
// A restart is already on its way; the process will disappear shortly.
|
|
693
726
|
if (state.dispatchedAt > 0 && now - state.dispatchedAt < 60000) return
|
|
694
727
|
|
|
@@ -926,14 +959,24 @@ export function apply(ctx, config) {
|
|
|
926
959
|
if (initialResult.phase === 'stopped') {
|
|
927
960
|
logLine('info', '上次重启正在进行(守护进程已停掉旧进程,等它拉起新窗口)')
|
|
928
961
|
try {
|
|
929
|
-
|
|
962
|
+
// The supervisor writes `relaunched` only after the window shows, which can
|
|
963
|
+
// take a while on a cold start: poll instead of reading once.
|
|
964
|
+
let attempts = 0
|
|
965
|
+
const poll = () => {
|
|
966
|
+
attempts += 1
|
|
930
967
|
const final = readLastResult()
|
|
931
968
|
if (final !== null && (final.phase === 'relaunched' || final.phase === 'relaunched-no-window')) {
|
|
932
969
|
logLine('info', `上次重启结果:${describeResult(final)}`)
|
|
933
|
-
|
|
970
|
+
return
|
|
971
|
+
}
|
|
972
|
+
if (attempts >= 14) {
|
|
934
973
|
logLine('warn', `上次重启没有确认完成:${final === null ? 'missing' : describeResult(final)}`)
|
|
974
|
+
return
|
|
935
975
|
}
|
|
936
|
-
|
|
976
|
+
const timer = setTimeout(poll, 1500)
|
|
977
|
+
timer.unref?.()
|
|
978
|
+
}
|
|
979
|
+
const timer = setTimeout(poll, 1500)
|
|
937
980
|
timer.unref?.()
|
|
938
981
|
} catch {
|
|
939
982
|
// diagnostics only
|
|
@@ -946,21 +989,18 @@ export function apply(ctx, config) {
|
|
|
946
989
|
|
|
947
990
|
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()}`)
|
|
948
991
|
|
|
949
|
-
// Resident watchdog: keep its files current
|
|
950
|
-
//
|
|
951
|
-
// Startup entry is absent).
|
|
992
|
+
// Resident watchdog: keep its files current, report its state, and let the
|
|
993
|
+
// periodic health check re-open the installer when it stops answering.
|
|
952
994
|
ensureWatchdogFiles()
|
|
953
995
|
const heartbeat = readWatchdogHeartbeat()
|
|
954
996
|
if (heartbeat !== null) {
|
|
955
997
|
logLine('info', `后台看门狗运行中:pid=${heartbeat.pid} 心跳=${heartbeat.at}`)
|
|
956
|
-
} else if (
|
|
957
|
-
if (state.installerOffered === false && isRealUserHome() && isDesktopHost()) {
|
|
958
|
-
state.installerOffered = true
|
|
959
|
-
offerWatchdogInstall()
|
|
960
|
-
}
|
|
961
|
-
} else {
|
|
998
|
+
} else if (watchdogInstalled()) {
|
|
962
999
|
logLine('warn', '后台看门狗已安装但当前没有心跳(可能刚登录还没起来),本次重启会用 WMI 兜底')
|
|
1000
|
+
} else {
|
|
1001
|
+
logLine('info', '后台看门狗未安装:会自动打开安装窗口')
|
|
963
1002
|
}
|
|
1003
|
+
watchdogHealthCheck(state, cfg)
|
|
964
1004
|
|
|
965
1005
|
// Track busy sessions so a restart never cuts an answer.
|
|
966
1006
|
ctx.effect(() => ctx.on('api-session/status', (sessionId, running) => {
|
|
@@ -1023,4 +1063,7 @@ export const _internals = {
|
|
|
1023
1063
|
sanitizeConfig,
|
|
1024
1064
|
parseJsonLoose,
|
|
1025
1065
|
isDesktopHost,
|
|
1066
|
+
watchdogAlive,
|
|
1067
|
+
readWatchdogHeartbeat,
|
|
1068
|
+
watchdogInstalled,
|
|
1026
1069
|
}
|
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.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"title": "自动重启",
|
|
6
6
|
"description": "装/更插件后自动重启 DSH 并回到原会话:检测 profile 插件文件变化,一直等所有会话空闲且最近活动安静,由常驻后台看门狗(登录自启、不碰 WMI)或 WMI 守护先杀主进程再拉起(可最大化窗口),带冷却、试运行、日志和右侧原生侧边栏面板。",
|