@leiyi-works/dsh-auto-restart 0.2.4 → 0.3.0
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 +122 -116
- package/client.js +16 -1
- package/index.js +132 -49
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,116 +1,122 @@
|
|
|
1
|
-
# dsh-auto-restart · 自动重启
|
|
2
|
-
|
|
3
|
-
装完/更新完插件后,不用再自己关掉 DSH 再打开:它会发现插件文件变了,等所有会话空闲,然后把整个桌面应用重启一次,回到原来的会话,**窗口默认最大化**。
|
|
4
|
-
|
|
5
|
-
只支持 **Windows 桌面端**(Electron 外壳 + Host 子进程的形态)。`dsh web` / headless 不会重启(插件会写日志说明不支持)。
|
|
6
|
-
|
|
7
|
-
左侧菜单有一个「自动重启」入口:点一下先回到会话,再在**右侧原生侧边栏**打开面板(和「临时目录」等插件一致,不遮挡会话);状态、上次重启回执、开关与参数都在那里改,改完即时生效。右侧边栏不可用时才退回全页面板。
|
|
8
|
-
|
|
9
|
-
## 它怎么判断"插件变了"
|
|
10
|
-
|
|
11
|
-
盯着当前 profile 的这几处,每 2 秒取一次指纹:
|
|
12
|
-
|
|
13
|
-
- `node_modules/` 顶层条目,以及 `@scope/` 下面的条目(时间、大小、增删)
|
|
14
|
-
- `node_modules-link/`(如果存在)
|
|
15
|
-
- `package.json` 里的 `dependencies` 与 `dsh.profile.bundles`(内容签名)
|
|
16
|
-
|
|
17
|
-
指纹变了并且**稳定 10 秒**没再变,才算一次安装/更新结束。这样 dshmarket、官方插件管理器、手动 `pnpm add`、改自己插件的文件都能覆盖到,而云同步恢复同样的内容(内容签名不变)不会误触发。
|
|
18
|
-
|
|
19
|
-
## 它怎么重启
|
|
20
|
-
|
|
21
|
-
1. 等所有会话空闲(`api-session/status` + `agent/status
|
|
22
|
-
2. 最后一次会话活动(发消息、回合开始/结束)之后再安静 10 秒,避免刚发出、回合还没起来的消息被重启吞掉、文字回到输入框;
|
|
23
|
-
3. 等插件管理器安静 3 秒(不打断进行中的安装/更新);
|
|
24
|
-
4. **优先交给常驻后台看门狗**:看门狗只在登录 Windows 时由「启动文件夹」里的引线自动启动,插件把请求写进 `~/.dsh/dsh-auto-restart/watchdog-request.json`,由它先杀 Electron 主进程、清残留、拉起应用并最大化——**不碰 WMI,也不会触发安全软件的 WMI 提示**;
|
|
25
|
-
5. 看门狗没在跑时(未安装/被关掉)才退回 WMI 守护进程:写一个隐藏窗口的快捷方式,交给 explorer.exe 启动守护进程(脱离 DSH 的 job object),由它完成同样的「先杀主进程 → 清残留 → 拉起 → 最大化」。
|
|
26
|
-
|
|
27
|
-
### 后台看门狗(零弹窗模式)
|
|
28
|
-
|
|
29
|
-
首次启动时插件会检测:没装就自动弹一个**中文控制台窗口**,说明用途并等你按回车安装。安装做三件事:把脚本拷到 `~/.dsh/dsh-auto-restart/`、在启动文件夹写一个隐藏引线、立刻启动看门狗。之后每次登录 Windows 它自动起来,所有自动重启都不再经过 WMI。
|
|
30
|
-
|
|
31
|
-
- 面板里只显示看门狗状态(运行中/未安装/已安装无心跳/脚本已更新待重装),**没有安装/卸载按钮**;
|
|
32
|
-
-
|
|
33
|
-
- **脚本更新检测**:心跳里带脚本哈希;插件升级后如果后台还跑着旧脚本,会再弹一次安装窗口把看门狗重启到新脚本(安装器会先停掉旧进程);
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
-
|
|
41
|
-
- `
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
|
63
|
-
|
|
64
|
-
|
|
|
65
|
-
|
|
|
66
|
-
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
|
76
|
-
|
|
77
|
-
| `
|
|
78
|
-
| `
|
|
79
|
-
| `
|
|
80
|
-
| `
|
|
81
|
-
| `
|
|
82
|
-
| `
|
|
83
|
-
| `
|
|
84
|
-
| `
|
|
85
|
-
| `
|
|
86
|
-
| `
|
|
87
|
-
| `
|
|
88
|
-
| `
|
|
89
|
-
| `
|
|
90
|
-
| `
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
-
|
|
116
|
-
|
|
1
|
+
# dsh-auto-restart · 自动重启
|
|
2
|
+
|
|
3
|
+
装完/更新完插件后,不用再自己关掉 DSH 再打开:它会发现插件文件变了,等所有会话空闲,然后把整个桌面应用重启一次,回到原来的会话,**窗口默认最大化**。
|
|
4
|
+
|
|
5
|
+
只支持 **Windows 桌面端**(Electron 外壳 + Host 子进程的形态)。`dsh web` / headless 不会重启(插件会写日志说明不支持)。
|
|
6
|
+
|
|
7
|
+
左侧菜单有一个「自动重启」入口:点一下先回到会话,再在**右侧原生侧边栏**打开面板(和「临时目录」等插件一致,不遮挡会话);状态、上次重启回执、开关与参数都在那里改,改完即时生效。右侧边栏不可用时才退回全页面板。
|
|
8
|
+
|
|
9
|
+
## 它怎么判断"插件变了"
|
|
10
|
+
|
|
11
|
+
盯着当前 profile 的这几处,每 2 秒取一次指纹:
|
|
12
|
+
|
|
13
|
+
- `node_modules/` 顶层条目,以及 `@scope/` 下面的条目(时间、大小、增删)
|
|
14
|
+
- `node_modules-link/`(如果存在)
|
|
15
|
+
- `package.json` 里的 `dependencies` 与 `dsh.profile.bundles`(内容签名)
|
|
16
|
+
|
|
17
|
+
指纹变了并且**稳定 10 秒**没再变,才算一次安装/更新结束。这样 dshmarket、官方插件管理器、手动 `pnpm add`、改自己插件的文件都能覆盖到,而云同步恢复同样的内容(内容签名不变)不会误触发。
|
|
18
|
+
|
|
19
|
+
## 它怎么重启
|
|
20
|
+
|
|
21
|
+
1. 等所有会话空闲(`api-session/status` + `agent/status`):最多等 `maxIdleWaitMs`(默认 **10 分钟**),超时就按计划重启并记录被中断的会话;设 `0` 表示一直等;
|
|
22
|
+
2. 最后一次会话活动(发消息、回合开始/结束)之后再安静 10 秒,避免刚发出、回合还没起来的消息被重启吞掉、文字回到输入框;
|
|
23
|
+
3. 等插件管理器安静 3 秒(不打断进行中的安装/更新);
|
|
24
|
+
4. **优先交给常驻后台看门狗**:看门狗只在登录 Windows 时由「启动文件夹」里的引线自动启动,插件把请求写进 `~/.dsh/dsh-auto-restart/watchdog-request.json`,由它先杀 Electron 主进程、清残留、拉起应用并最大化——**不碰 WMI,也不会触发安全软件的 WMI 提示**;
|
|
25
|
+
5. 看门狗没在跑时(未安装/被关掉)才退回 WMI 守护进程:写一个隐藏窗口的快捷方式,交给 explorer.exe 启动守护进程(脱离 DSH 的 job object),由它完成同样的「先杀主进程 → 清残留 → 拉起 → 最大化」。
|
|
26
|
+
|
|
27
|
+
### 后台看门狗(零弹窗模式)
|
|
28
|
+
|
|
29
|
+
首次启动时插件会检测:没装就自动弹一个**中文控制台窗口**,说明用途并等你按回车安装。安装做三件事:把脚本拷到 `~/.dsh/dsh-auto-restart/`、在启动文件夹写一个隐藏引线、立刻启动看门狗。之后每次登录 Windows 它自动起来,所有自动重启都不再经过 WMI。
|
|
30
|
+
|
|
31
|
+
- 面板里只显示看门狗状态(运行中/未安装/已安装无心跳/脚本已更新待重装),**没有安装/卸载按钮**;
|
|
32
|
+
- **每次 DSH 启动(含自动重启后)体检一次**:看门狗没在回答(进程没了/心跳过期)就自动打开安装窗口;不再做周期检查(避免工作中反复弹窗);这期间自动重启会退回 WMI 兜底;
|
|
33
|
+
- **脚本更新检测**:心跳里带脚本哈希;插件升级后如果后台还跑着旧脚本,会再弹一次安装窗口把看门狗重启到新脚本(安装器会先停掉旧进程);
|
|
34
|
+
- 急停(可选):在 `~/.dsh/dsh-auto-restart/` 里新建空文件 `disabled`,插件就不再动手(这和直接在插件列表里停用插件基本等价,留着只是应急用);删掉即恢复;
|
|
35
|
+
- 卸载:在 `~/.dsh/dsh-auto-restart/` 里执行 `powershell -File install-watchdog.ps1 -Uninstall`;
|
|
36
|
+
- 看门狗日志:`~/.dsh/dsh-auto-restart/watchdog.log`。
|
|
37
|
+
|
|
38
|
+
### 多机同步注意
|
|
39
|
+
|
|
40
|
+
- 看门狗本体和安装脚本在 `~/.dsh/dsh-auto-restart/`,会跟着云端同步走;**启动文件夹的引线不在 `~/.dsh` 里,不会被同步**——所以每台机器第一次都会弹安装窗口,各自装一次即可(路径全部运行时由 `%USERPROFILE%` / `%APPDATA%` 计算,不同用户名不影响);
|
|
41
|
+
- 其它机器同步过来的旧重启请求(`watchdog-request.json` / `restart-request.json`)会在启动时按 exe 路径校验后清理,不会被本机看门狗误执行;
|
|
42
|
+
- `disabled`(急停文件)和 `config.json` 会同步:一处急停等于全机队急停、一处改配置全机队生效;如果不想要这个行为,把这两个文件加进云端同步的排除项。
|
|
43
|
+
|
|
44
|
+
> 少数安全软件(如 360)对「PowerShell 操作 WMI」有行为规则:装了看门狗后重启不走 WMI,就不会再因此弹窗;安装看门狗那一次仍可能被提示一次,允许即可。
|
|
45
|
+
|
|
46
|
+
会话记录在磁盘上,重启后回到同一个会话。
|
|
47
|
+
|
|
48
|
+
### 重启后自动继续
|
|
49
|
+
|
|
50
|
+
被重启打断的会话(重启派发时正忙的那些)会在新进程启动后各收到一条 **「继续」**:用宿主服务 `sessionController.prompt()` 作为用户消息发回去。可通过 `continueInterrupted` 关闭、`continueText` 改文字;在等用户做选择/授权的会话(`pendingInput` 非空)会跳过,避免替你回答。适合"等 npm 发布窗口/等版本对齐"这类后台等待任务。**没有每小时次数上限**,只有 60 秒冷却防止连环重启。
|
|
51
|
+
|
|
52
|
+
## 安装后第一次
|
|
53
|
+
|
|
54
|
+
第一次安装需要手动重启一次 DSH(托盘「退出」再打开),让 Host 半边的代码加载。之后就不用再管了。
|
|
55
|
+
|
|
56
|
+
**首次默认 `dryRun: true`**:只写日志、不真重启。确认日志里看到「检测到插件文件变化」和「[dry-run] 本应重启」之后,在左侧「自动重启」面板里关掉试运行即可(也可以在 `config.json` 里把 `dryRun` 改成 `false`,改完即时生效,不用重启)。
|
|
57
|
+
|
|
58
|
+
配置文件:`%USERPROFILE%\.dsh\dsh-auto-restart\config.json`(也可以放 `~/...` 形式的可移植路径)。
|
|
59
|
+
|
|
60
|
+
## 左侧面板
|
|
61
|
+
|
|
62
|
+
| 控件 | 说明 |
|
|
63
|
+
|---|---|
|
|
64
|
+
| 启用自动重启 | 总开关 |
|
|
65
|
+
| 试运行(dryRun) | 只记日志不重启 |
|
|
66
|
+
| 重启后最大化窗口 | 守护进程拉起应用后把主窗口最大化 |
|
|
67
|
+
| 等所有会话空闲再重启 | 有回答在跑时一直等,没有上限 |
|
|
68
|
+
| 文件稳定判定 | 默认 10 秒 |
|
|
69
|
+
| 两次重启冷却 | 默认 60 秒 |
|
|
70
|
+
| 空闲后安静 | 默认 10 秒 |
|
|
71
|
+
| 立即重启 | 手动触发一次(有会话在跑时会被拒绝) |
|
|
72
|
+
|
|
73
|
+
## 配置
|
|
74
|
+
|
|
75
|
+
| 键 | 默认 | 说明 |
|
|
76
|
+
|---|---|---|
|
|
77
|
+
| `enabled` | `true` | 总开关 |
|
|
78
|
+
| `dryRun` | `true` | 只记日志不重启;验证后改 `false` |
|
|
79
|
+
| `stableMs` | `10000` | 文件停止变化多久算安装完成 |
|
|
80
|
+
| `pollIntervalMs` | `2000` | 轮询间隔 |
|
|
81
|
+
| `managerQuietMs` | `3000` | 插件管理器最后一次活动后至少安静多久 |
|
|
82
|
+
| `cooldownMs` | `60000` | 两次重启最小间隔(跨重启持久化) |
|
|
83
|
+
| `maxPerHour` | `0` | 每小时上限;`0` = 不限制 |
|
|
84
|
+
| `waitForIdle` | `true` | 等会话空闲再重启 |
|
|
85
|
+
| `idleSettleMs` | `10000` | 最后一次会话活动后安静多久再重启;防止刚发出、回合还没起来的消息被吞掉 |
|
|
86
|
+
| `maxIdleWaitMs` | `600000` | 等空闲最多等多久,超过就按计划重启(0 = 一直等) |
|
|
87
|
+
| `continueInterrupted` | `true` | 重启后是否给被中断的会话自动发"继续" |
|
|
88
|
+
| `continueText` | `继续` | 自动继续发送的文本 |
|
|
89
|
+
| `restartDelayMs` | `2000` | 守护进程收到请求后等多久动手 |
|
|
90
|
+
| `maximizeOnRestart` | `true` | 重启后最大化主窗口 |
|
|
91
|
+
| `exePath` | `''` | 应用可执行文件,留空自动推导 |
|
|
92
|
+
| `launchArgs` | `[]` | 拉起应用时的附加参数 |
|
|
93
|
+
| `supervisorScript` | `''` | 自定义守护脚本路径 |
|
|
94
|
+
| `powershellPath` | `''` | 自定义 powershell.exe |
|
|
95
|
+
| `profileDir` | `''` | 自定义 profile 目录 |
|
|
96
|
+
| `logMaxBytes` | `1048576` | 日志滚动阈值 |
|
|
97
|
+
|
|
98
|
+
## 急停
|
|
99
|
+
|
|
100
|
+
在 `%USERPROFILE%\.dsh\dsh-auto-restart\` 下新建一个空文件 `disabled`,它立刻什么都不做(每 2 秒检查一次)。删掉就恢复。
|
|
101
|
+
|
|
102
|
+
## 状态与日志
|
|
103
|
+
|
|
104
|
+
- 日志:`%USERPROFILE%\.dsh\dsh-auto-restart\auto-restart.log`
|
|
105
|
+
- 上次重启回执:`restart-result.json`;上次请求:`last-restart.json`
|
|
106
|
+
- 状态:`GET http://127.0.0.1:<Host 端口>/auto-restart/status`(只接受本机回环)
|
|
107
|
+
- 配置:`GET/POST /auto-restart/config`
|
|
108
|
+
|
|
109
|
+
## 卸载
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
dsh plugin --profile desktop remove @leiyi-works/dsh-auto-restart
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`%USERPROFILE%\.dsh\dsh-auto-restart\` 里的日志和配置可以留也可以删。
|
|
116
|
+
|
|
117
|
+
## 边界
|
|
118
|
+
|
|
119
|
+
- 只重启应用,不动其它进程。
|
|
120
|
+
- 正在跑的任务会被重启打断——所以一直等它空闲,不会硬切;等多久都不会放弃,直到空闲。
|
|
121
|
+
- 不碰 `app.asar`,DSH 升级不受影响。
|
|
122
|
+
- 桌面端正式构建没有可用重启入口(官方菜单项只在 development 构建出现),所以只能用外部守护进程的方式;这也是为什么需要一次引导重启。
|
package/client.js
CHANGED
|
@@ -312,6 +312,21 @@ window.__ModuleLoader__.load({
|
|
|
312
312
|
checked: config.waitForIdle !== false,
|
|
313
313
|
onChange: (value) => change({ waitForIdle: value }),
|
|
314
314
|
}),
|
|
315
|
+
h(NumberRow, {
|
|
316
|
+
label: '等空闲上限',
|
|
317
|
+
hint: '忙超过这么久就按计划重启,不再无限等(重启后自动给被中断的会话发“继续”);0 表示一直等。',
|
|
318
|
+
value: Math.round((config.maxIdleWaitMs ?? 600000) / 60000),
|
|
319
|
+
min: 0,
|
|
320
|
+
max: 240,
|
|
321
|
+
suffix: '分钟',
|
|
322
|
+
onChange: (value) => change({ maxIdleWaitMs: Math.max(0, Math.round(value)) * 60000 }),
|
|
323
|
+
}),
|
|
324
|
+
h(SwitchRow, {
|
|
325
|
+
label: '重启后自动继续',
|
|
326
|
+
hint: '给被这次重启打断的会话自动发一条“继续”;在等用户做选择/授权的会话会跳过。',
|
|
327
|
+
checked: config.continueInterrupted !== false,
|
|
328
|
+
onChange: (value) => change({ continueInterrupted: value }),
|
|
329
|
+
}),
|
|
315
330
|
h(NumberRow, {
|
|
316
331
|
label: '空闲后安静',
|
|
317
332
|
hint: '最后一次会话活动之后安静这么久再重启,防止刚发出、回合还没起来的消息被吞掉。',
|
|
@@ -352,7 +367,7 @@ window.__ModuleLoader__.load({
|
|
|
352
367
|
save.busy ? h('p', { style: styles.note }, '正在保存…') : null,
|
|
353
368
|
statusCard,
|
|
354
369
|
settingsCard,
|
|
355
|
-
h('p', { style: styles.note }, '配置文件:' + (status?.configPath ?? '—')
|
|
370
|
+
h('p', { style: styles.note }, '配置文件:' + (status?.configPath ?? '—')),
|
|
356
371
|
),
|
|
357
372
|
)
|
|
358
373
|
}
|
package/index.js
CHANGED
|
@@ -74,15 +74,19 @@ const DEFAULTS = Object.freeze({
|
|
|
74
74
|
maxPerHour: 0,
|
|
75
75
|
/** 有会话在跑时一直等它们空闲(没有上限,不会硬切)。 */
|
|
76
76
|
waitForIdle: true,
|
|
77
|
+
/** 等空闲最多等多久;超过就按计划重启(重启后会给被中断的会话发“继续”);0 = 一直等。 */
|
|
78
|
+
maxIdleWaitMs: 600000,
|
|
79
|
+
/** 重启后是否自动给被中断的会话发一条“继续”。 */
|
|
80
|
+
continueInterrupted: true,
|
|
81
|
+
/** 自动继续时发送的文本。 */
|
|
82
|
+
continueText: '继续',
|
|
83
|
+
/** 多久以内的中断记录还值得自动继续(毫秒)。 */
|
|
84
|
+
continueWindowMs: 1800000,
|
|
77
85
|
/**
|
|
78
86
|
* 最后一次会话活动(发消息、回合开始/结束)之后,再安静这么久才重启。
|
|
79
87
|
* 防止「刚按发送、回合还没起来」的瞬间被重启吞掉,导致输入框回填已发出的文字。
|
|
80
88
|
*/
|
|
81
89
|
idleSettleMs: 10000,
|
|
82
|
-
/** 多久检查一次后台看门狗是否活着(毫秒)。 */
|
|
83
|
-
watchdogCheckMs: 60000,
|
|
84
|
-
/** 看门狗不在时,重新打开安装窗口的最小间隔(毫秒)。 */
|
|
85
|
-
watchdogReinstallCooldownMs: 300000,
|
|
86
90
|
/** 守护进程收到请求后等多久动手(毫秒),留时间给当前回合收尾。 */
|
|
87
91
|
restartDelayMs: 2000,
|
|
88
92
|
/** 重启后把主窗口最大化(开关在左侧「自动重启」面板里)。 */
|
|
@@ -164,6 +168,90 @@ function watchdogLogPath() {
|
|
|
164
168
|
return join(dataDir(), 'watchdog.log')
|
|
165
169
|
}
|
|
166
170
|
|
|
171
|
+
/** Sessions that were busy when a restart was dispatched (for auto-continue). */
|
|
172
|
+
function interruptedPath() {
|
|
173
|
+
return join(dataDir(), 'interrupted.json')
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function recordInterrupted(state, reason) {
|
|
177
|
+
const file = interruptedPath()
|
|
178
|
+
const ids = [...state.busy]
|
|
179
|
+
if (ids.length === 0) {
|
|
180
|
+
try { rmSync(file, { force: true }) } catch {}
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
writeJsonAtomic(file, {
|
|
185
|
+
at: new Date().toISOString(),
|
|
186
|
+
reason,
|
|
187
|
+
ids,
|
|
188
|
+
exe: resolveExe(readConfig({})),
|
|
189
|
+
})
|
|
190
|
+
logLine('info', `已记录 ${ids.length} 个可能被中断的会话(重启后自动发“继续”):${ids.map((id) => String(id).slice(0, 18)).join(', ')}`)
|
|
191
|
+
} catch (error) {
|
|
192
|
+
logLine('warn', `记录被中断会话失败:${String(error?.message ?? error)}`)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* After a restart, tell the Sessions that were interrupted to keep going.
|
|
198
|
+
* Waiting-for-input sessions are skipped: resuming those would answer a question
|
|
199
|
+
* the user never answered.
|
|
200
|
+
*/
|
|
201
|
+
async function resumeInterrupted(ctx, cfg) {
|
|
202
|
+
const file = interruptedPath()
|
|
203
|
+
const record = readJsonFile(file)
|
|
204
|
+
if (record === null) return
|
|
205
|
+
try {
|
|
206
|
+
if (cfg.continueInterrupted === false || !Array.isArray(record.ids) || record.ids.length === 0) {
|
|
207
|
+
try { rmSync(file, { force: true }) } catch {}
|
|
208
|
+
return
|
|
209
|
+
}
|
|
210
|
+
const at = Date.parse(String(record.at))
|
|
211
|
+
const age = Date.now() - at
|
|
212
|
+
if (!Number.isFinite(at) || age < 0 || age > cfg.continueWindowMs) {
|
|
213
|
+
try { rmSync(file, { force: true }) } catch {}
|
|
214
|
+
logLine('info', '上次的中断记录太久远,忽略自动继续')
|
|
215
|
+
return
|
|
216
|
+
}
|
|
217
|
+
let controller = null
|
|
218
|
+
try { controller = ctx.get('sessionController') } catch { controller = null }
|
|
219
|
+
if (!controller || typeof controller.prompt !== 'function') {
|
|
220
|
+
logLine('warn', '拿不到 sessionController,无法自动给被中断的会话发“继续”')
|
|
221
|
+
return
|
|
222
|
+
}
|
|
223
|
+
for (let index = 0; index < record.ids.length; index += 1) {
|
|
224
|
+
const sessionId = String(record.ids[index])
|
|
225
|
+
const short = sessionId.slice(0, 18)
|
|
226
|
+
try {
|
|
227
|
+
let sessionState = null
|
|
228
|
+
try { sessionState = await controller.state({ sessionId }) } catch {}
|
|
229
|
+
if (sessionState && sessionState.running === true) {
|
|
230
|
+
logLine('info', `跳过自动继续 ${short}:会话已经在跑`)
|
|
231
|
+
continue
|
|
232
|
+
}
|
|
233
|
+
if (sessionState && Array.isArray(sessionState.pendingInput) && sessionState.pendingInput.length > 0) {
|
|
234
|
+
logLine('info', `跳过自动继续 ${short}:会话在等用户输入(${sessionState.pendingInput.join('/')})`)
|
|
235
|
+
continue
|
|
236
|
+
}
|
|
237
|
+
await controller.prompt({
|
|
238
|
+
requestId: `auto-restart-continue-${Date.now()}-${index}`,
|
|
239
|
+
sessionId,
|
|
240
|
+
mode: 'queue',
|
|
241
|
+
content: [{ type: 'text', text: String(cfg.continueText || '继续') }],
|
|
242
|
+
clientTimeZone: 'Asia/Shanghai',
|
|
243
|
+
})
|
|
244
|
+
logLine('info', `已给被中断的会话发“${cfg.continueText}”:${short}`)
|
|
245
|
+
} catch (error) {
|
|
246
|
+
logLine('warn', `给 ${short} 发“继续”失败:${String(error?.message ?? error)}`)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
try { rmSync(file, { force: true }) } catch {}
|
|
250
|
+
} catch (error) {
|
|
251
|
+
logLine('warn', `自动继续失败:${String(error?.message ?? error)}`)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
167
255
|
function startupEntryPath() {
|
|
168
256
|
const appData = process.env.APPDATA
|
|
169
257
|
if (typeof appData !== 'string' || appData.trim() === '') return ''
|
|
@@ -221,7 +309,7 @@ function readWatchdogHeartbeat() {
|
|
|
221
309
|
*/
|
|
222
310
|
function watchdogScriptStale(heartbeat) {
|
|
223
311
|
const current = watchdogScriptHash()
|
|
224
|
-
return heartbeat !== null && current !== '' && heartbeat.script !==
|
|
312
|
+
return heartbeat !== null && current !== '' && heartbeat.script !== current
|
|
225
313
|
}
|
|
226
314
|
|
|
227
315
|
function watchdogAlive() {
|
|
@@ -308,34 +396,27 @@ function cleanForeignRequests() {
|
|
|
308
396
|
}
|
|
309
397
|
|
|
310
398
|
/**
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
399
|
+
* Boot-time watchdog check: once per DSH start (and therefore once after every
|
|
400
|
+
* automatic restart) the plugin verifies the resident watchdog. Missing or running
|
|
401
|
+
* older code means a restart would fall back to WMI, so the console installer is
|
|
402
|
+
* opened. Deliberately not periodic: one check per boot is enough and keeps
|
|
403
|
+
* console windows from popping up while the user works.
|
|
314
404
|
*/
|
|
315
|
-
function
|
|
405
|
+
function bootWatchdogCheck(state) {
|
|
316
406
|
if (!isDesktopHost() || !isRealUserHome()) return
|
|
317
|
-
if (
|
|
318
|
-
state.
|
|
407
|
+
if (state.bootWatchdogChecked === true) return
|
|
408
|
+
state.bootWatchdogChecked = true
|
|
319
409
|
const heartbeat = readWatchdogHeartbeat()
|
|
320
|
-
if (heartbeat !== null) {
|
|
321
|
-
|
|
322
|
-
if (!watchdogScriptStale(heartbeat)) return
|
|
323
|
-
// Running code is older than the plugin on disk: the installer stops the old
|
|
324
|
-
// process and starts the new script.
|
|
325
|
-
if (now - state.lastWatchdogOfferAt < cfg.watchdogReinstallCooldownMs) return
|
|
326
|
-
state.lastWatchdogOfferAt = now
|
|
327
|
-
state.watchdogStaleLogged = true
|
|
328
|
-
logLine('warn', `后台看门狗运行的脚本已过期(${heartbeat.script.slice(0, 8)} ≠ ${watchdogScriptHash().slice(0, 8)}),重新打开安装窗口以更新`)
|
|
329
|
-
offerWatchdogInstall()
|
|
410
|
+
if (heartbeat !== null && !watchdogScriptStale(heartbeat)) {
|
|
411
|
+
logLine('info', `后台看门狗运行中:pid=${heartbeat.pid} 心跳=${heartbeat.at}`)
|
|
330
412
|
return
|
|
331
413
|
}
|
|
332
|
-
if (
|
|
333
|
-
|
|
334
|
-
if (
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
: '后台看门狗未安装,重新打开安装窗口')
|
|
414
|
+
if (heartbeat !== null) {
|
|
415
|
+
logLine('warn', `后台看门狗运行的脚本已过期(${heartbeat.script.slice(0, 8)} ≠ ${watchdogScriptHash().slice(0, 8)}),打开安装窗口更新`)
|
|
416
|
+
} else if (watchdogInstalled()) {
|
|
417
|
+
logLine('warn', '后台看门狗已安装但当前没有心跳,打开安装窗口重新启动')
|
|
418
|
+
} else {
|
|
419
|
+
logLine('info', '后台看门狗未安装:打开安装窗口')
|
|
339
420
|
}
|
|
340
421
|
offerWatchdogInstall()
|
|
341
422
|
}
|
|
@@ -440,6 +521,10 @@ function sanitizeConfig(raw) {
|
|
|
440
521
|
cfg.enabled = bool(raw.enabled, cfg.enabled)
|
|
441
522
|
cfg.dryRun = bool(raw.dryRun, cfg.dryRun)
|
|
442
523
|
cfg.waitForIdle = bool(raw.waitForIdle, cfg.waitForIdle)
|
|
524
|
+
cfg.maxIdleWaitMs = num(raw.maxIdleWaitMs, cfg.maxIdleWaitMs, 0, 86400000)
|
|
525
|
+
cfg.continueInterrupted = bool(raw.continueInterrupted, cfg.continueInterrupted)
|
|
526
|
+
cfg.continueWindowMs = num(raw.continueWindowMs, cfg.continueWindowMs, 60000, 86400000)
|
|
527
|
+
if (typeof raw.continueText === 'string' && raw.continueText.trim() !== '') cfg.continueText = raw.continueText.slice(0, 200)
|
|
443
528
|
cfg.maximizeOnRestart = bool(raw.maximizeOnRestart, cfg.maximizeOnRestart)
|
|
444
529
|
cfg.pollIntervalMs = num(raw.pollIntervalMs, cfg.pollIntervalMs, 500, 60000)
|
|
445
530
|
cfg.stableMs = num(raw.stableMs, cfg.stableMs, 0, 600000)
|
|
@@ -447,8 +532,6 @@ function sanitizeConfig(raw) {
|
|
|
447
532
|
cfg.cooldownMs = num(raw.cooldownMs, cfg.cooldownMs, 0, 3600000)
|
|
448
533
|
cfg.maxPerHour = num(raw.maxPerHour, cfg.maxPerHour, 0, 100)
|
|
449
534
|
cfg.idleSettleMs = num(raw.idleSettleMs, cfg.idleSettleMs, 0, 600000)
|
|
450
|
-
cfg.watchdogCheckMs = num(raw.watchdogCheckMs, cfg.watchdogCheckMs, 10000, 3600000)
|
|
451
|
-
cfg.watchdogReinstallCooldownMs = num(raw.watchdogReinstallCooldownMs, cfg.watchdogReinstallCooldownMs, 60000, 86400000)
|
|
452
535
|
cfg.restartDelayMs = num(raw.restartDelayMs, cfg.restartDelayMs, 0, 600000)
|
|
453
536
|
cfg.logMaxBytes = num(raw.logMaxBytes, cfg.logMaxBytes, 4096, 64 * 1024 * 1024)
|
|
454
537
|
for (const key of ['exePath', 'supervisorScript', 'powershellPath', 'profileDir']) {
|
|
@@ -661,6 +744,7 @@ function dispatchRestart(state, cfg, reason, options = {}) {
|
|
|
661
744
|
// Preferred path: a resident watchdog started outside DSH's process tree; it
|
|
662
745
|
// performs the restart without any WMI call. Falls back to the WMI launcher
|
|
663
746
|
// when the watchdog is not running.
|
|
747
|
+
recordInterrupted(state, reason)
|
|
664
748
|
if (watchdogAlive()) return dispatchToWatchdog(state, cfg, reason)
|
|
665
749
|
|
|
666
750
|
const delay = Math.max(0, Number(cfg.restartDelayMs) || 0)
|
|
@@ -766,8 +850,7 @@ function createState(profileDir) {
|
|
|
766
850
|
lastReason: '',
|
|
767
851
|
lastSkipLog: 0,
|
|
768
852
|
watchdogMissingLogged: false,
|
|
769
|
-
|
|
770
|
-
lastWatchdogOfferAt: 0,
|
|
853
|
+
bootWatchdogChecked: false,
|
|
771
854
|
}
|
|
772
855
|
}
|
|
773
856
|
|
|
@@ -781,8 +864,6 @@ function tick(baseConfig) {
|
|
|
781
864
|
const now = Date.now()
|
|
782
865
|
state.restarts = state.restarts.filter((t) => now - t < 3600000)
|
|
783
866
|
|
|
784
|
-
watchdogHealthCheck(state, cfg, now)
|
|
785
|
-
|
|
786
867
|
// A restart is already on its way; the process will disappear shortly.
|
|
787
868
|
if (state.dispatchedAt > 0 && now - state.dispatchedAt < 60000) return
|
|
788
869
|
|
|
@@ -839,11 +920,16 @@ function tick(baseConfig) {
|
|
|
839
920
|
}
|
|
840
921
|
|
|
841
922
|
if (cfg.waitForIdle && state.busy.size > 0) {
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
923
|
+
const busyForMs = state.waitingSince > 0 ? now - state.waitingSince : 0
|
|
924
|
+
const overLimit = cfg.maxIdleWaitMs > 0 && busyForMs >= cfg.maxIdleWaitMs
|
|
925
|
+
if (!overLimit) {
|
|
926
|
+
if (state.waitingSince === 0) {
|
|
927
|
+
state.waitingSince = now
|
|
928
|
+
logLine('info', `有 ${state.busy.size} 个会话在跑,等它们空闲后再重启(最多等 ${cfg.maxIdleWaitMs > 0 ? Math.round(cfg.maxIdleWaitMs / 60000) + ' 分钟' : '一直等'})`)
|
|
929
|
+
}
|
|
930
|
+
return
|
|
845
931
|
}
|
|
846
|
-
|
|
932
|
+
logLine('warn', `等空闲已超过 ${Math.round(cfg.maxIdleWaitMs / 60000)} 分钟(${state.busy.size} 个会话还在跑),按计划重启;重启后会给它们发“继续”`)
|
|
847
933
|
}
|
|
848
934
|
state.waitingSince = 0
|
|
849
935
|
|
|
@@ -941,6 +1027,9 @@ function installRoutes(ctx, baseConfig) {
|
|
|
941
1027
|
logPath: watchdogLogPath(),
|
|
942
1028
|
},
|
|
943
1029
|
waitForIdle: cfg.waitForIdle,
|
|
1030
|
+
maxIdleWaitMs: cfg.maxIdleWaitMs,
|
|
1031
|
+
continueInterrupted: cfg.continueInterrupted,
|
|
1032
|
+
continueText: cfg.continueText,
|
|
944
1033
|
busySessions: state ? [...state.busy] : [],
|
|
945
1034
|
pending: state ? state.pendingSince > 0 : false,
|
|
946
1035
|
baseline: state?.baseline?.slice(0, 10) ?? null,
|
|
@@ -1051,19 +1140,13 @@ export function apply(ctx, config) {
|
|
|
1051
1140
|
|
|
1052
1141
|
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()}`)
|
|
1053
1142
|
|
|
1054
|
-
// Resident watchdog: keep its files current,
|
|
1055
|
-
//
|
|
1143
|
+
// Resident watchdog: keep its files current, drop requests synced from another
|
|
1144
|
+
// machine, then check the watchdog once for this boot (no periodic checks).
|
|
1056
1145
|
ensureWatchdogFiles()
|
|
1057
1146
|
cleanForeignRequests()
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
} else if (watchdogInstalled()) {
|
|
1062
|
-
logLine('warn', '后台看门狗已安装但当前没有心跳(可能刚登录还没起来),本次重启会用 WMI 兜底')
|
|
1063
|
-
} else {
|
|
1064
|
-
logLine('info', '后台看门狗未安装:会自动打开安装窗口')
|
|
1065
|
-
}
|
|
1066
|
-
watchdogHealthCheck(state, cfg)
|
|
1147
|
+
bootWatchdogCheck(state)
|
|
1148
|
+
// Sessions that a previous restart interrupted get one "继续" after boot.
|
|
1149
|
+
void resumeInterrupted(ctx, cfg)
|
|
1067
1150
|
|
|
1068
1151
|
// Track busy sessions so a restart never cuts an answer.
|
|
1069
1152
|
ctx.effect(() => ctx.on('api-session/status', (sessionId, running) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leiyi-works/dsh-auto-restart",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"title": "自动重启",
|
|
6
6
|
"description": "装/更插件后自动重启 DSH 并回到原会话:检测 profile 插件文件变化,一直等所有会话空闲且最近活动安静,由常驻后台看门狗(登录自启、不碰 WMI)或 WMI 守护先杀主进程再拉起(可最大化窗口),带冷却、试运行、日志和右侧原生侧边栏面板。",
|