@leiyi-works/dsh-auto-restart 0.2.0 → 0.2.3
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/client.js +11 -42
- package/index.js +70 -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/client.js
CHANGED
|
@@ -198,7 +198,6 @@ window.__ModuleLoader__.load({
|
|
|
198
198
|
function AutoRestartPanel() {
|
|
199
199
|
const [state, setState] = useState({ loading: true, error: '', status: null, config: null })
|
|
200
200
|
const [save, setSave] = useState({ busy: false, error: '', ok: '' })
|
|
201
|
-
const [restarting, setRestarting] = useState(false)
|
|
202
201
|
const dirty = useRef(false)
|
|
203
202
|
|
|
204
203
|
const load = useCallback(async () => {
|
|
@@ -237,23 +236,6 @@ window.__ModuleLoader__.load({
|
|
|
237
236
|
saveConfig(patch)
|
|
238
237
|
}, [saveConfig])
|
|
239
238
|
|
|
240
|
-
const restartNow = useCallback(async () => {
|
|
241
|
-
setRestarting(true)
|
|
242
|
-
setSave({ busy: false, error: '', ok: '' })
|
|
243
|
-
try {
|
|
244
|
-
const body = await postJson(`${API}/restart`, {})
|
|
245
|
-
setSave({
|
|
246
|
-
busy: false,
|
|
247
|
-
error: '',
|
|
248
|
-
ok: body.dryRun === true ? '当前是试运行模式,只记录了日志,没有真的重启' : '已派发重启,DSH 会在这条消息送达后关闭并重新打开',
|
|
249
|
-
})
|
|
250
|
-
} catch (error) {
|
|
251
|
-
setSave({ busy: false, error: String(error?.message ?? error), ok: '' })
|
|
252
|
-
} finally {
|
|
253
|
-
setRestarting(false)
|
|
254
|
-
}
|
|
255
|
-
}, [])
|
|
256
|
-
|
|
257
239
|
const status = state.status
|
|
258
240
|
const config = state.config
|
|
259
241
|
const badge = config === null
|
|
@@ -299,13 +281,7 @@ window.__ModuleLoader__.load({
|
|
|
299
281
|
? [h('span', { key: 'k', style: styles.key }, '急停'), h('span', { key: 'v', style: styles.err }, 'disabled 文件存在:插件不会做任何事')]
|
|
300
282
|
: null,
|
|
301
283
|
),
|
|
302
|
-
h(
|
|
303
|
-
'div',
|
|
304
|
-
{ style: styles.bar },
|
|
305
|
-
h('button', { type: 'button', style: styles.btnPrimary, onClick: restartNow, disabled: restarting || config === null }, restarting ? '正在派发…' : '立即重启'),
|
|
306
|
-
h('button', { type: 'button', style: styles.btn, onClick: load }, '刷新'),
|
|
307
|
-
status?.desktopHost === false ? h('span', { style: styles.err }, '当前宿主不是 Windows 桌面端,重启不可用') : null,
|
|
308
|
-
),
|
|
284
|
+
status?.desktopHost === false ? h('p', { style: styles.err }, '当前宿主不是 Windows 桌面端,重启不可用') : null,
|
|
309
285
|
save.error ? h('p', { style: styles.err }, `操作失败:${save.error}`) : null,
|
|
310
286
|
save.ok ? h('p', { style: styles.ok }, save.ok) : null,
|
|
311
287
|
)
|
|
@@ -322,12 +298,6 @@ window.__ModuleLoader__.load({
|
|
|
322
298
|
checked: config.enabled !== false,
|
|
323
299
|
onChange: (value) => change({ enabled: value }),
|
|
324
300
|
}),
|
|
325
|
-
h(SwitchRow, {
|
|
326
|
-
label: '试运行(dryRun)',
|
|
327
|
-
hint: '只写日志、不真的重启。用来确认它会在什么时候动手。',
|
|
328
|
-
checked: config.dryRun === true,
|
|
329
|
-
onChange: (value) => change({ dryRun: value }),
|
|
330
|
-
}),
|
|
331
301
|
h(SwitchRow, {
|
|
332
302
|
label: '重启后最大化窗口',
|
|
333
303
|
hint: '守护进程拉起应用后,把主窗口最大化。',
|
|
@@ -336,10 +306,19 @@ window.__ModuleLoader__.load({
|
|
|
336
306
|
}),
|
|
337
307
|
h(SwitchRow, {
|
|
338
308
|
label: '等所有会话空闲再重启',
|
|
339
|
-
hint: '
|
|
309
|
+
hint: '有回答或任务在跑时一直等它们结束,避免截断。',
|
|
340
310
|
checked: config.waitForIdle !== false,
|
|
341
311
|
onChange: (value) => change({ waitForIdle: value }),
|
|
342
312
|
}),
|
|
313
|
+
h(NumberRow, {
|
|
314
|
+
label: '空闲后安静',
|
|
315
|
+
hint: '最后一次会话活动之后安静这么久再重启,防止刚发出、回合还没起来的消息被吞掉。',
|
|
316
|
+
value: Math.round((config.idleSettleMs ?? 10000) / 1000),
|
|
317
|
+
min: 0,
|
|
318
|
+
max: 300,
|
|
319
|
+
suffix: '秒',
|
|
320
|
+
onChange: (value) => change({ idleSettleMs: Math.max(0, Math.round(value)) * 1000 }),
|
|
321
|
+
}),
|
|
343
322
|
h(NumberRow, {
|
|
344
323
|
label: '文件稳定判定',
|
|
345
324
|
hint: '插件文件停止变化多久才算安装完成。',
|
|
@@ -358,16 +337,6 @@ window.__ModuleLoader__.load({
|
|
|
358
337
|
suffix: '秒',
|
|
359
338
|
onChange: (value) => change({ cooldownMs: Math.max(0, Math.round(value)) * 1000 }),
|
|
360
339
|
}),
|
|
361
|
-
h(NumberRow, {
|
|
362
|
-
label: '空闲后安静',
|
|
363
|
-
hint: '最后一次会话活动之后安静这么久再重启,防止刚发出、回合还没起来的消息被吞掉。',
|
|
364
|
-
value: Math.round((config.idleSettleMs ?? 10000) / 1000),
|
|
365
|
-
min: 0,
|
|
366
|
-
max: 300,
|
|
367
|
-
suffix: '秒',
|
|
368
|
-
onChange: (value) => change({ idleSettleMs: Math.max(0, Math.round(value)) * 1000 }),
|
|
369
|
-
}),
|
|
370
|
-
h('p', { style: styles.note }, '改动即时保存、即时生效,不需要重启 DSH。'),
|
|
371
340
|
)
|
|
372
341
|
|
|
373
342
|
return h(
|
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
|
/** 重启后把主窗口最大化(开关在左侧「自动重启」面板里)。 */
|
|
@@ -206,6 +210,19 @@ function watchdogInstalled() {
|
|
|
206
210
|
return entry !== '' && existsSync(entry)
|
|
207
211
|
}
|
|
208
212
|
|
|
213
|
+
/**
|
|
214
|
+
* True only for the real user home. Tests and throwaway harnesses point DSH_HOME
|
|
215
|
+
* at a temp directory; opening a real console installer from there would be a
|
|
216
|
+
* surprise, so the offer is limited to the actual desktop user.
|
|
217
|
+
*/
|
|
218
|
+
function isRealUserHome() {
|
|
219
|
+
try {
|
|
220
|
+
return resolve(dshHome()) === resolve(join(homedir(), '.dsh'))
|
|
221
|
+
} catch {
|
|
222
|
+
return false
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
209
226
|
/** Ask the resident watchdog to perform the restart; it survives our death. */
|
|
210
227
|
function dispatchToWatchdog(state, cfg, reason) {
|
|
211
228
|
const exe = resolveExe(cfg)
|
|
@@ -238,7 +255,30 @@ function dispatchToWatchdog(state, cfg, reason) {
|
|
|
238
255
|
return { ok: true, dryRun: false, via: 'watchdog', delayMs: request.delayMs }
|
|
239
256
|
}
|
|
240
257
|
|
|
241
|
-
/**
|
|
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
|
+
}
|
|
242
282
|
function offerWatchdogInstall() {
|
|
243
283
|
const installer = watchdogInstallerPath()
|
|
244
284
|
if (!existsSync(installer)) {
|
|
@@ -346,6 +386,8 @@ function sanitizeConfig(raw) {
|
|
|
346
386
|
cfg.cooldownMs = num(raw.cooldownMs, cfg.cooldownMs, 0, 3600000)
|
|
347
387
|
cfg.maxPerHour = num(raw.maxPerHour, cfg.maxPerHour, 0, 100)
|
|
348
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)
|
|
349
391
|
cfg.restartDelayMs = num(raw.restartDelayMs, cfg.restartDelayMs, 0, 600000)
|
|
350
392
|
cfg.logMaxBytes = num(raw.logMaxBytes, cfg.logMaxBytes, 4096, 64 * 1024 * 1024)
|
|
351
393
|
for (const key of ['exePath', 'supervisorScript', 'powershellPath', 'profileDir']) {
|
|
@@ -662,7 +704,9 @@ function createState(profileDir) {
|
|
|
662
704
|
busy: new Set(),
|
|
663
705
|
lastReason: '',
|
|
664
706
|
lastSkipLog: 0,
|
|
665
|
-
|
|
707
|
+
watchdogMissingLogged: false,
|
|
708
|
+
lastWatchdogCheckAt: 0,
|
|
709
|
+
lastWatchdogOfferAt: 0,
|
|
666
710
|
}
|
|
667
711
|
}
|
|
668
712
|
|
|
@@ -676,6 +720,8 @@ function tick(baseConfig) {
|
|
|
676
720
|
const now = Date.now()
|
|
677
721
|
state.restarts = state.restarts.filter((t) => now - t < 3600000)
|
|
678
722
|
|
|
723
|
+
watchdogHealthCheck(state, cfg, now)
|
|
724
|
+
|
|
679
725
|
// A restart is already on its way; the process will disappear shortly.
|
|
680
726
|
if (state.dispatchedAt > 0 && now - state.dispatchedAt < 60000) return
|
|
681
727
|
|
|
@@ -913,14 +959,24 @@ export function apply(ctx, config) {
|
|
|
913
959
|
if (initialResult.phase === 'stopped') {
|
|
914
960
|
logLine('info', '上次重启正在进行(守护进程已停掉旧进程,等它拉起新窗口)')
|
|
915
961
|
try {
|
|
916
|
-
|
|
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
|
|
917
967
|
const final = readLastResult()
|
|
918
968
|
if (final !== null && (final.phase === 'relaunched' || final.phase === 'relaunched-no-window')) {
|
|
919
969
|
logLine('info', `上次重启结果:${describeResult(final)}`)
|
|
920
|
-
|
|
970
|
+
return
|
|
971
|
+
}
|
|
972
|
+
if (attempts >= 14) {
|
|
921
973
|
logLine('warn', `上次重启没有确认完成:${final === null ? 'missing' : describeResult(final)}`)
|
|
974
|
+
return
|
|
922
975
|
}
|
|
923
|
-
|
|
976
|
+
const timer = setTimeout(poll, 1500)
|
|
977
|
+
timer.unref?.()
|
|
978
|
+
}
|
|
979
|
+
const timer = setTimeout(poll, 1500)
|
|
924
980
|
timer.unref?.()
|
|
925
981
|
} catch {
|
|
926
982
|
// diagnostics only
|
|
@@ -933,21 +989,18 @@ export function apply(ctx, config) {
|
|
|
933
989
|
|
|
934
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()}`)
|
|
935
991
|
|
|
936
|
-
// Resident watchdog: keep its files current
|
|
937
|
-
//
|
|
938
|
-
// 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.
|
|
939
994
|
ensureWatchdogFiles()
|
|
940
995
|
const heartbeat = readWatchdogHeartbeat()
|
|
941
996
|
if (heartbeat !== null) {
|
|
942
997
|
logLine('info', `后台看门狗运行中:pid=${heartbeat.pid} 心跳=${heartbeat.at}`)
|
|
943
|
-
} else if (
|
|
944
|
-
if (state.installerOffered === false) {
|
|
945
|
-
state.installerOffered = true
|
|
946
|
-
offerWatchdogInstall()
|
|
947
|
-
}
|
|
948
|
-
} else {
|
|
998
|
+
} else if (watchdogInstalled()) {
|
|
949
999
|
logLine('warn', '后台看门狗已安装但当前没有心跳(可能刚登录还没起来),本次重启会用 WMI 兜底')
|
|
1000
|
+
} else {
|
|
1001
|
+
logLine('info', '后台看门狗未安装:会自动打开安装窗口')
|
|
950
1002
|
}
|
|
1003
|
+
watchdogHealthCheck(state, cfg)
|
|
951
1004
|
|
|
952
1005
|
// Track busy sessions so a restart never cuts an answer.
|
|
953
1006
|
ctx.effect(() => ctx.on('api-session/status', (sessionId, running) => {
|
|
@@ -1010,4 +1063,7 @@ export const _internals = {
|
|
|
1010
1063
|
sanitizeConfig,
|
|
1011
1064
|
parseJsonLoose,
|
|
1012
1065
|
isDesktopHost,
|
|
1066
|
+
watchdogAlive,
|
|
1067
|
+
readWatchdogHeartbeat,
|
|
1068
|
+
watchdogInstalled,
|
|
1013
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.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"title": "自动重启",
|
|
6
6
|
"description": "装/更插件后自动重启 DSH 并回到原会话:检测 profile 插件文件变化,一直等所有会话空闲且最近活动安静,由常驻后台看门狗(登录自启、不碰 WMI)或 WMI 守护先杀主进程再拉起(可最大化窗口),带冷却、试运行、日志和右侧原生侧边栏面板。",
|