dsh-aupdate 0.6.5 → 0.6.7
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 +27 -0
- package/client.js +95 -6
- package/index.js +176 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,6 +63,19 @@ https://github.com/CalmSun/dsh-aupdate
|
|
|
63
63
|
| `maxPerCycle` | `0` | 一轮最多装几个,0 表示不限 |
|
|
64
64
|
| `sessionIdleMinutes` | `2` | 会话静默多少分钟才算闲,0 表示只看子任务 |
|
|
65
65
|
| `notify` | `true` | 发现可更新版本时在右下角提醒,列出插件与版本 |
|
|
66
|
+
| `historyRetentionDays` | `3` | 更新记录保留天数,0 = 永久 |
|
|
67
|
+
| `logRetentionDays` | `3` | 运行日志保留天数,0 = 永久 |
|
|
68
|
+
|
|
69
|
+
## 定期清理
|
|
70
|
+
|
|
71
|
+
更新记录和运行日志默认只留 **3 天**,每次定时检查前清一次,DSH 启动时也清一次;
|
|
72
|
+
设置页另有「立即清理」。两个期限可分别选 1 / 3 / 7 / 30 / 90 天或「永久保留」。
|
|
73
|
+
|
|
74
|
+
**回滚记录和它撤销的那条更新必须同进同退**:回滚判定是从回滚记录反推哪些更新已消费的,
|
|
75
|
+
如果只按时间删、恰好删掉回滚记录而留下对应更新,那次已经回滚过的降级就会重新变得可回滚。
|
|
76
|
+
所以清理按「更新 + 撤销它的回滚」分组,以组内最新的时间决定去留,整组一起删或一起留。
|
|
77
|
+
|
|
78
|
+
日志按行首的 ISO 时间清理;解析不出时间的行一律保留,不因为格式不认识就删掉现场。
|
|
66
79
|
|
|
67
80
|
## 什么时候动手
|
|
68
81
|
|
|
@@ -291,6 +304,20 @@ A successful install clears the record.
|
|
|
291
304
|
| `maxPerCycle` | `0` | Installs per cycle; 0 means no limit |
|
|
292
305
|
| `sessionIdleMinutes` | `2` | Session must be quiet this long to count as idle; 0 judges by subagents only |
|
|
293
306
|
| `notify` | `true` | Announce available updates in the bottom-right corner, with plugin names and versions |
|
|
307
|
+
| `historyRetentionDays` | `3` | History retention in days; 0 = forever |
|
|
308
|
+
| `logRetentionDays` | `3` | Log retention in days; 0 = forever |
|
|
309
|
+
|
|
310
|
+
## Retention
|
|
311
|
+
|
|
312
|
+
Update history and the run log keep **3 days** by default. Cleanup runs before every
|
|
313
|
+
scheduled check and once at startup; the settings page also has an immediate Clean now
|
|
314
|
+
button. Each retention can be set to 1 / 3 / 7 / 30 / 90 days or Keep forever.
|
|
315
|
+
|
|
316
|
+
A rollback and the update it reverses are always removed together. Rollback eligibility is
|
|
317
|
+
derived from rollback records — deleting only the rollback would make an already-reverted
|
|
318
|
+
downgrade roll back again — so pruning groups them and decides by the newest timestamp in
|
|
319
|
+
the group. Log lines are filtered by their leading ISO timestamp; lines without a parseable
|
|
320
|
+
one are kept rather than lost.
|
|
294
321
|
|
|
295
322
|
### When it acts
|
|
296
323
|
|
package/client.js
CHANGED
|
@@ -103,6 +103,17 @@ window.__ModuleLoader__.load({
|
|
|
103
103
|
toastTitle: '{n} 个插件可以更新',
|
|
104
104
|
toastMore: '还有 {n} 个',
|
|
105
105
|
toastDismiss: '知道了',
|
|
106
|
+
keepTitle: '保留期限',
|
|
107
|
+
keepHint: '到期自动清掉旧的更新记录与运行日志;0 表示一直保留。回滚记录会和对应更新一起保留',
|
|
108
|
+
keepHistory: '更新记录保留',
|
|
109
|
+
keepLog: '运行日志保留',
|
|
110
|
+
keepDay: '天',
|
|
111
|
+
keepForever: '永久保留',
|
|
112
|
+
cleanNow: '立即清理',
|
|
113
|
+
cleaning: '清理中…',
|
|
114
|
+
cleanDone: '已清理 {n} 条',
|
|
115
|
+
cleanNothing: '没有需要清理的',
|
|
116
|
+
cleanFail: '清理失败',
|
|
106
117
|
history: '更新记录',
|
|
107
118
|
historyEmpty: '还没有安装过任何更新',
|
|
108
119
|
rollback: '回滚',
|
|
@@ -208,6 +219,17 @@ window.__ModuleLoader__.load({
|
|
|
208
219
|
toastTitle: '{n} plugin(s) can update',
|
|
209
220
|
toastMore: 'and {n} more',
|
|
210
221
|
toastDismiss: 'Got it',
|
|
222
|
+
keepTitle: 'Retention',
|
|
223
|
+
keepHint: 'Older update records and log lines are removed automatically; 0 means keep forever. A rollback is kept together with the update it reverses',
|
|
224
|
+
keepHistory: 'History retention',
|
|
225
|
+
keepLog: 'Log retention',
|
|
226
|
+
keepDay: 'days',
|
|
227
|
+
keepForever: 'Keep forever',
|
|
228
|
+
cleanNow: 'Clean now',
|
|
229
|
+
cleaning: 'Cleaning…',
|
|
230
|
+
cleanDone: 'Removed {n} entries',
|
|
231
|
+
cleanNothing: 'Nothing to clean',
|
|
232
|
+
cleanFail: 'Cleanup failed',
|
|
211
233
|
history: 'Update history',
|
|
212
234
|
historyEmpty: 'No update has been installed yet',
|
|
213
235
|
rollback: 'Roll back',
|
|
@@ -372,6 +394,17 @@ window.__ModuleLoader__.load({
|
|
|
372
394
|
return response.json()
|
|
373
395
|
}
|
|
374
396
|
|
|
397
|
+
/** 手动清理:服务端按当前保留期限删,回报删了多少。 */
|
|
398
|
+
async function cleanup() {
|
|
399
|
+
const response = await fetch(endpoint('cleanup') + langQuery(), {
|
|
400
|
+
method: 'POST',
|
|
401
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
402
|
+
body: '{}',
|
|
403
|
+
})
|
|
404
|
+
if (!response.ok) throw new Error('cleanup ' + response.status)
|
|
405
|
+
return response.json()
|
|
406
|
+
}
|
|
407
|
+
|
|
375
408
|
async function rollback(name) {
|
|
376
409
|
const response = await fetch(endpoint('rollback') + langQuery(), {
|
|
377
410
|
method: 'POST',
|
|
@@ -617,6 +650,8 @@ window.__ModuleLoader__.load({
|
|
|
617
650
|
const [rolling, setRolling] = useState('')
|
|
618
651
|
const [rollHint, setRollHint] = useState('')
|
|
619
652
|
const [confirming, setConfirming] = useState('')
|
|
653
|
+
const [cleaning, setCleaning] = useState(false)
|
|
654
|
+
const [cleanHint, setCleanHint] = useState('')
|
|
620
655
|
|
|
621
656
|
const load = useCallback(async () => {
|
|
622
657
|
setPhase('loading')
|
|
@@ -715,6 +750,25 @@ window.__ModuleLoader__.load({
|
|
|
715
750
|
}
|
|
716
751
|
}, [running])
|
|
717
752
|
|
|
753
|
+
const doCleanup = useCallback(async () => {
|
|
754
|
+
if (cleaning) return
|
|
755
|
+
setCleaning(true)
|
|
756
|
+
setCleanHint('')
|
|
757
|
+
try {
|
|
758
|
+
const result = await cleanup()
|
|
759
|
+
const total = Number(result?.history ?? 0) + Number(result?.log ?? 0)
|
|
760
|
+
setCleanHint(total > 0 ? T.cleanDone.replace('{n}', String(total)) : T.cleanNothing)
|
|
761
|
+
try {
|
|
762
|
+
const next = await getState(LANG)
|
|
763
|
+
setState(next)
|
|
764
|
+
} catch {}
|
|
765
|
+
} catch {
|
|
766
|
+
setCleanHint(T.cleanFail)
|
|
767
|
+
} finally {
|
|
768
|
+
setCleaning(false)
|
|
769
|
+
}
|
|
770
|
+
}, [cleaning])
|
|
771
|
+
|
|
718
772
|
const doRollback = useCallback(async (target) => {
|
|
719
773
|
if (rolling !== '' || running) return
|
|
720
774
|
// 回滚会换掉正在跑的代码,属于不可轻点的操作,先要一次确认。
|
|
@@ -832,14 +886,46 @@ window.__ModuleLoader__.load({
|
|
|
832
886
|
h(SwitchRow, { label: T.allowMajor, hint: T.allowMajorHint, value: draft.allowMajor, onChange: (v) => patch('allowMajor', v) }),
|
|
833
887
|
h(SwitchRow, { label: T.allowPrerelease, hint: T.allowPrereleaseHint, value: draft.allowPrerelease, onChange: (v) => patch('allowPrerelease', v) }),
|
|
834
888
|
),
|
|
835
|
-
h('div', { style: S.
|
|
836
|
-
h('
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
889
|
+
h('div', { style: S.card },
|
|
890
|
+
h('span', { style: S.label }, T.keepTitle),
|
|
891
|
+
h('span', { style: S.hint }, T.keepHint),
|
|
892
|
+
h(SelectRow, {
|
|
893
|
+
label: T.keepHistory,
|
|
894
|
+
value: String(draft.historyRetentionDays ?? 3),
|
|
895
|
+
onChange: (v) => patch('historyRetentionDays', Number(v)),
|
|
896
|
+
options: [
|
|
897
|
+
{ value: '1', label: '1 ' + T.keepDay },
|
|
898
|
+
{ value: '3', label: '3 ' + T.keepDay },
|
|
899
|
+
{ value: '7', label: '7 ' + T.keepDay },
|
|
900
|
+
{ value: '30', label: '30 ' + T.keepDay },
|
|
901
|
+
{ value: '90', label: '90 ' + T.keepDay },
|
|
902
|
+
{ value: '0', label: T.keepForever },
|
|
903
|
+
],
|
|
904
|
+
}),
|
|
905
|
+
h(SelectRow, {
|
|
906
|
+
label: T.keepLog,
|
|
907
|
+
value: String(draft.logRetentionDays ?? 3),
|
|
908
|
+
onChange: (v) => patch('logRetentionDays', Number(v)),
|
|
909
|
+
options: [
|
|
910
|
+
{ value: '1', label: '1 ' + T.keepDay },
|
|
911
|
+
{ value: '3', label: '3 ' + T.keepDay },
|
|
912
|
+
{ value: '7', label: '7 ' + T.keepDay },
|
|
913
|
+
{ value: '30', label: '30 ' + T.keepDay },
|
|
914
|
+
{ value: '90', label: '90 ' + T.keepDay },
|
|
915
|
+
{ value: '0', label: T.keepForever },
|
|
916
|
+
],
|
|
917
|
+
}),
|
|
918
|
+
h('div', { style: S.actions },
|
|
919
|
+
h('button', { type: 'button', style: S.btn, disabled: cleaning, onClick: () => void doCleanup() }, cleaning ? T.cleaning : T.cleanNow),
|
|
920
|
+
cleanHint ? h('span', { style: S.status }, cleanHint) : null,
|
|
921
|
+
),
|
|
840
922
|
),
|
|
841
923
|
h('div', { style: S.card },
|
|
842
|
-
|
|
924
|
+
// 按钮和它动作的对象放在一起:读「上一次」的人最想做的事就是再跑一次。
|
|
925
|
+
h('div', { style: S.row },
|
|
926
|
+
h('span', { style: S.label }, T.lastRun),
|
|
927
|
+
h('button', { type: 'button', style: S.btn, disabled: running, onClick: () => void fire() }, running ? T.running : T.runNow),
|
|
928
|
+
),
|
|
843
929
|
last === null || last === undefined
|
|
844
930
|
? h('span', { style: S.status }, T.never)
|
|
845
931
|
: h('div', { style: S.meta },
|
|
@@ -850,6 +936,7 @@ window.__ModuleLoader__.load({
|
|
|
850
936
|
: T.upToDate),
|
|
851
937
|
h('span', null, last.reason || last.status || ''),
|
|
852
938
|
),
|
|
939
|
+
runHint ? h('span', { style: S.status }, runHint) : null,
|
|
853
940
|
),
|
|
854
941
|
// 更新记录:谁、从哪到哪、什么时候、成没成,以及回滚入口。
|
|
855
942
|
h('div', { style: S.card },
|
|
@@ -880,6 +967,8 @@ window.__ModuleLoader__.load({
|
|
|
880
967
|
rollHint ? h('span', { style: S.status }, rollHint) : null,
|
|
881
968
|
),
|
|
882
969
|
h(LogCard, null),
|
|
970
|
+
// 没有保存按钮:改动自己在 350ms 后落盘,这里只报它落没落成。
|
|
971
|
+
saveHint ? h('span', { style: S.status }, saveHint) : null,
|
|
883
972
|
h('span', { style: S.hint }, T.scope),
|
|
884
973
|
)
|
|
885
974
|
}
|
package/index.js
CHANGED
|
@@ -125,6 +125,7 @@ const LOG_FIELD_LABELS = {
|
|
|
125
125
|
allowMajor: '跨大版本', allowPrerelease: '预发布', scheduleMode: '调度方式',
|
|
126
126
|
dailyAt: '每天时刻', everyHours: '每几小时', everyDays: '每几天',
|
|
127
127
|
suppressHours: '失败抑制', maxPerCycle: '单轮上限', sessionIdleMinutes: '会话静默',
|
|
128
|
+
historyRetentionDays: '更新记录保留', logRetentionDays: '运行日志保留',
|
|
128
129
|
},
|
|
129
130
|
en: {
|
|
130
131
|
lang: 'display language', enabled: 'scheduled check', autoApply: 'auto-install',
|
|
@@ -134,6 +135,7 @@ const LOG_FIELD_LABELS = {
|
|
|
134
135
|
scheduleMode: 'schedule', dailyAt: 'time of day', everyHours: 'every N hours',
|
|
135
136
|
everyDays: 'every N days', suppressHours: 'failure suppression', maxPerCycle: 'per-cycle cap',
|
|
136
137
|
sessionIdleMinutes: 'session idle',
|
|
138
|
+
historyRetentionDays: 'history retention', logRetentionDays: 'log retention',
|
|
137
139
|
},
|
|
138
140
|
}
|
|
139
141
|
|
|
@@ -187,6 +189,11 @@ function makeLogTemplates(lang) {
|
|
|
187
189
|
toolScopeEnded: (msg) => (zh ? `工具作用域已结束:${msg}` : `Tool scope ended: ${msg}`),
|
|
188
190
|
disabledBySettings: () => (zh ? '设置里已关闭自动更新,定时器待命' : 'Auto-update is off in settings; scheduler parked'),
|
|
189
191
|
disabledByConfig: () => (zh ? '配置里已关闭自动更新,定时器待命,设置页仍然可用' : 'Auto-update is off in config; scheduler parked, settings page stays live'),
|
|
192
|
+
// 清理日志按对象报数,几个数字一处说完,免得刷三行。
|
|
193
|
+
cleaned: (kind, rows, days) => (zh
|
|
194
|
+
? `清理${kind}:删掉 ${rows} 条超过 ${days} 天的`
|
|
195
|
+
: `Cleaned ${kind}: removed ${rows} entr${rows === 1 ? 'y' : 'ies'} older than ${days} day(s)`),
|
|
196
|
+
cleanSkipped: (kind) => (zh ? `${kind}没有需要清理的` : `Nothing to clean in ${kind}`),
|
|
190
197
|
ready: (minutes, autoApply, hotReload) => (zh
|
|
191
198
|
? `已就绪:每 ${minutes} 分钟检查一次,自动安装=${onOff(autoApply)},就地生效=${onOff(hotReload)}`
|
|
192
199
|
: `Ready: every ${minutes} min, autoApply=${onOff(autoApply)}, hotReload=${onOff(hotReload)}`),
|
|
@@ -475,11 +482,27 @@ const OVERRIDEABLE = [
|
|
|
475
482
|
'lang',
|
|
476
483
|
// 效率闸门:一轮最多装几个、会话多静才算闲。
|
|
477
484
|
'maxPerCycle', 'sessionIdleMinutes',
|
|
485
|
+
// 更新记录 / 运行日志 / 快照的保留期限(天)。0 = 永不清理。
|
|
486
|
+
'historyRetentionDays', 'logRetentionDays',
|
|
478
487
|
]
|
|
479
488
|
|
|
480
489
|
/** 四种调度模式。minutes 是原来的「间隔 N 分钟」,其余照抄竞品。 */
|
|
481
490
|
const SCHEDULE_MODES = ['minutes', 'hours', 'days', 'daily']
|
|
482
491
|
|
|
492
|
+
/**
|
|
493
|
+
* 保留期限。默认 3 天——记录主要是「刚才装坏了什么」的现场,放太久只会占空间、
|
|
494
|
+
* 还会让更新记录栏翻很久。0 表示永不自动清理。
|
|
495
|
+
*
|
|
496
|
+
* 这是天数不是毫秒:设置页传 3 就是 3 天,写入时钳到 [0, 3650]。
|
|
497
|
+
*/
|
|
498
|
+
const RETENTION_CHOICES = [1, 3, 7, 30, 90, 0]
|
|
499
|
+
|
|
500
|
+
function retentionOf(config, key, fallback) {
|
|
501
|
+
const value = Number(config?.[key])
|
|
502
|
+
if (!Number.isFinite(value)) return fallback
|
|
503
|
+
return Math.min(Math.max(Math.round(value), 0), 3650)
|
|
504
|
+
}
|
|
505
|
+
|
|
483
506
|
/**
|
|
484
507
|
* 界面语言。auto = 跟随系统(中文环境给中文,其余给英文)。
|
|
485
508
|
*
|
|
@@ -502,6 +525,8 @@ const NUMBER_BOUNDS = {
|
|
|
502
525
|
suppressHours: [0, 720],
|
|
503
526
|
maxPerCycle: [0, 50],
|
|
504
527
|
sessionIdleMinutes: [0, 120],
|
|
528
|
+
historyRetentionDays: [0, 3650],
|
|
529
|
+
logRetentionDays: [0, 3650],
|
|
505
530
|
}
|
|
506
531
|
|
|
507
532
|
let overrideCache = { stamp: null, value: {} }
|
|
@@ -664,6 +689,9 @@ function settingsOf(config) {
|
|
|
664
689
|
maxPerCycle: numberSetting(config, 'maxPerCycle', 0, 0, 50),
|
|
665
690
|
// 会话目录多少分钟内没有写入才算「闲」。0 = 不看会话写入,只看子任务。
|
|
666
691
|
sessionIdleMinutes: numberSetting(config, 'sessionIdleMinutes', SESSION_IDLE_MINUTES, 0, 120),
|
|
692
|
+
// 更新记录 / 运行日志的保留天数。0 = 永不清理,默认 3 天。
|
|
693
|
+
historyRetentionDays: retentionOf(config, 'historyRetentionDays', 3),
|
|
694
|
+
logRetentionDays: retentionOf(config, 'logRetentionDays', 3),
|
|
667
695
|
// 说明:这两个键不进 Config schema(见 OVERRIDEABLE 上的注释),
|
|
668
696
|
// 所以只能通过设置页/覆盖层改,改坏了不会影响插件加载。
|
|
669
697
|
}
|
|
@@ -1376,6 +1404,111 @@ function pruneSnapshots() {
|
|
|
1376
1404
|
} catch {}
|
|
1377
1405
|
}
|
|
1378
1406
|
|
|
1407
|
+
/**
|
|
1408
|
+
* 按保留天数清理更新记录。
|
|
1409
|
+
*
|
|
1410
|
+
* 不能简单地「按 at 删老行」:pickRollbackTarget 是从回滚记录反推哪些更新已消费的。
|
|
1411
|
+
* 若把回滚记录删了、留下对应更新行,那次已经回滚过的降级就会重新变得可回滚——
|
|
1412
|
+
* 用户点一下,等于把一个已经被撤销的更新又执行一次。
|
|
1413
|
+
*
|
|
1414
|
+
* 所以按「一组」判新旧:一条更新 + 所有撤销它的回滚记录算一组,组内最新的时间
|
|
1415
|
+
* 决定这一组去留。同时删,就不会留下半组。
|
|
1416
|
+
*/
|
|
1417
|
+
function pruneHistory(days, now) {
|
|
1418
|
+
if (!(days > 0)) return 0
|
|
1419
|
+
const rows = readHistory()
|
|
1420
|
+
if (rows.length === 0) return 0
|
|
1421
|
+
const cutoff = now.getTime() - days * 86400_000
|
|
1422
|
+
const stampOf = (row) => {
|
|
1423
|
+
const at = Date.parse(String(row?.at ?? ''))
|
|
1424
|
+
return Number.isFinite(at) ? at : 0
|
|
1425
|
+
}
|
|
1426
|
+
const keyOf = (row) => [row?.name ?? '', row?.from ?? '', row?.to ?? '', row?.profile ?? ''].join('\u0000')
|
|
1427
|
+
|
|
1428
|
+
// 组 key 用「被更新的那次」的 (包, from, to, profile)。
|
|
1429
|
+
// 回滚行的 from/to 是反的(from=新版本, to=旧版本),换回来就落到同一个 key。
|
|
1430
|
+
// 这样一条更新和撤销它的回滚一定同进同退,不会出现半组。
|
|
1431
|
+
const groups = new Map()
|
|
1432
|
+
for (const row of rows) {
|
|
1433
|
+
const isRollback = row?.tag === 'rollback'
|
|
1434
|
+
const key = keyOf(isRollback
|
|
1435
|
+
? { name: row.name, from: row.to, to: row.from, profile: row.profile }
|
|
1436
|
+
: row)
|
|
1437
|
+
const group = groups.get(key) ?? { rows: [], newest: 0 }
|
|
1438
|
+
group.rows.push(row)
|
|
1439
|
+
group.newest = Math.max(group.newest, stampOf(row))
|
|
1440
|
+
groups.set(key, group)
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
const survivors = []
|
|
1444
|
+
for (const group of groups.values()) {
|
|
1445
|
+
if (group.newest >= cutoff) survivors.push(...group.rows)
|
|
1446
|
+
}
|
|
1447
|
+
const removed = rows.length - survivors.length
|
|
1448
|
+
if (removed > 0) {
|
|
1449
|
+
// 保持原有顺序(新→旧),组被打散过,按时间重排一次即可。
|
|
1450
|
+
survivors.sort((a, b) => stampOf(b) - stampOf(a))
|
|
1451
|
+
saveHistory(survivors)
|
|
1452
|
+
}
|
|
1453
|
+
return removed
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
/**
|
|
1457
|
+
* 按保留天数清理运行日志。
|
|
1458
|
+
*
|
|
1459
|
+
* 日志是「一行一条」的纯文本,每行以 [ISO 时间] 开头。逐行解析时间,只保留
|
|
1460
|
+
* cutoff 之后的行;解析不出时间的行(旧格式里偶有)一律保留——宁可多留,
|
|
1461
|
+
* 不因为格式不认识就删掉用户的现场。
|
|
1462
|
+
*
|
|
1463
|
+
* 写之前先 flush,否则待写队列里的新行还没落盘,写完被旧内容覆盖。
|
|
1464
|
+
*/
|
|
1465
|
+
function pruneLog(days, now) {
|
|
1466
|
+
if (!(days > 0)) return 0
|
|
1467
|
+
try {
|
|
1468
|
+
flushLogNow()
|
|
1469
|
+
} catch {}
|
|
1470
|
+
const file = logPath()
|
|
1471
|
+
let raw
|
|
1472
|
+
try {
|
|
1473
|
+
raw = readFileSync(file, 'utf8')
|
|
1474
|
+
} catch {
|
|
1475
|
+
return 0
|
|
1476
|
+
}
|
|
1477
|
+
const cutoff = now.getTime() - days * 86400_000
|
|
1478
|
+
const lines = raw.split('\n')
|
|
1479
|
+
if (lines.length > 0 && lines[lines.length - 1] === '') lines.pop()
|
|
1480
|
+
const kept = []
|
|
1481
|
+
let removed = 0
|
|
1482
|
+
for (const line of lines) {
|
|
1483
|
+
const match = /^\[([^\]]+)\]/u.exec(line)
|
|
1484
|
+
if (match === null) {
|
|
1485
|
+
kept.push(line)
|
|
1486
|
+
continue
|
|
1487
|
+
}
|
|
1488
|
+
const at = Date.parse(match[1])
|
|
1489
|
+
if (Number.isFinite(at) && at < cutoff) removed += 1
|
|
1490
|
+
else kept.push(line)
|
|
1491
|
+
}
|
|
1492
|
+
if (removed === 0) return 0
|
|
1493
|
+
try {
|
|
1494
|
+
writeFileSync(file, kept.length > 0 ? kept.join('\n') + '\n' : '', 'utf8')
|
|
1495
|
+
} catch {
|
|
1496
|
+
return 0
|
|
1497
|
+
}
|
|
1498
|
+
return removed
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
/** 一次清理:记录、日志、快照目录。按各自设置独立开关,0 表示不动。 */
|
|
1502
|
+
function pruneAll(settings, now) {
|
|
1503
|
+
const historyDays = settings.historyRetentionDays
|
|
1504
|
+
const logDays = settings.logRetentionDays
|
|
1505
|
+
if (!(historyDays > 0) && !(logDays > 0)) return
|
|
1506
|
+
const removedHistory = pruneHistory(historyDays, now)
|
|
1507
|
+
const removedLog = pruneLog(logDays, now)
|
|
1508
|
+
if (removedHistory > 0) writeLog('cleaned', ['更新记录', removedHistory, historyDays])
|
|
1509
|
+
if (removedLog > 0) writeLog('cleaned', ['运行日志', removedLog, logDays])
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1379
1512
|
function profileLockPath(profileDir) {
|
|
1380
1513
|
return join(profileDir, '.aupdate-install.lock')
|
|
1381
1514
|
}
|
|
@@ -2021,6 +2154,11 @@ function settingsSnapshot(config, ctxRef, lang) {
|
|
|
2021
2154
|
failurePath: failurePath(),
|
|
2022
2155
|
// 动手前留的现场:设置页把它显示出来,出问题时有地方可查。
|
|
2023
2156
|
snapshotPath: snapshotRoot(),
|
|
2157
|
+
// 保留期限也放在 state 里:设置页显示当前选择,并且按它算出「下次会清掉什么」。
|
|
2158
|
+
retention: {
|
|
2159
|
+
historyDays: settings.historyRetentionDays,
|
|
2160
|
+
logDays: settings.logRetentionDays,
|
|
2161
|
+
},
|
|
2024
2162
|
// 为什么这一轮没动手:设置页把这两个直接显示出来,用户不用翻日志猜。
|
|
2025
2163
|
// 尤其 sessionIdleMinutes —— 它太短会挡住大半夜的更新,太长又不该挡,
|
|
2026
2164
|
// 得让人一眼看到「现在到底算不算闲」。
|
|
@@ -2103,6 +2241,25 @@ function registerRoutes(scope, ctxRef) {
|
|
|
2103
2241
|
sendJson(res, outcome.ok ? 200 : 409, { ...outcome, reason: renderReason(outcome.reason, url.searchParams.get('lang')) })
|
|
2104
2242
|
return
|
|
2105
2243
|
}
|
|
2244
|
+
if (suffix === '/cleanup') {
|
|
2245
|
+
// 用户手动点「立即清理」:用当前设置跑一遍,回报删了多少条。
|
|
2246
|
+
const live = settingsOf(ctxRef.config)
|
|
2247
|
+
const now = new Date()
|
|
2248
|
+
const removedHistory = pruneHistory(live.historyRetentionDays, now)
|
|
2249
|
+
const removedLog = pruneLog(live.logRetentionDays, now)
|
|
2250
|
+
if (removedHistory > 0) writeLog('cleaned', ['更新记录', removedHistory, live.historyRetentionDays])
|
|
2251
|
+
if (removedLog > 0) writeLog('cleaned', ['运行日志', removedLog, live.logRetentionDays])
|
|
2252
|
+
const lang = logLangOf(url.searchParams.get('lang'))
|
|
2253
|
+
sendJson(res, 200, {
|
|
2254
|
+
history: removedHistory,
|
|
2255
|
+
log: removedLog,
|
|
2256
|
+
days: { history: live.historyRetentionDays, log: live.logRetentionDays },
|
|
2257
|
+
message: removedHistory === 0 && removedLog === 0
|
|
2258
|
+
? LOG_TEMPLATES[lang].cleanSkipped('更新记录 / 运行日志')
|
|
2259
|
+
: LOG_TEMPLATES[lang].cleaned(removedHistory + removedLog > 0 ? '记录与日志' : '', removedHistory + removedLog, Math.max(live.historyRetentionDays, live.logRetentionDays)),
|
|
2260
|
+
})
|
|
2261
|
+
return
|
|
2262
|
+
}
|
|
2106
2263
|
if (suffix === '/run') {
|
|
2107
2264
|
const body = await readBody(req)
|
|
2108
2265
|
const mode = body?.mode === 'check' ? 'check' : 'update'
|
|
@@ -2140,7 +2297,7 @@ function registerRoutes(scope, ctxRef) {
|
|
|
2140
2297
|
}
|
|
2141
2298
|
}
|
|
2142
2299
|
|
|
2143
|
-
export { compareVersions, upgradeVerdict, pickRollbackTarget, readHistory, historyPath, distTagFor, cliRunner, dependencyList, installedVersion, registryTags, hotApply, runCycle, settingsOf, readOverrides, writeOverrides, takeProfileLock, releaseProfileLock, sessionAgeMinutes, activityReasons, settingsSnapshot, snapshotProfile, snapshotRoot, pruneSnapshots, nextDelayMinutes, readLogTail, logPath }
|
|
2300
|
+
export { compareVersions, upgradeVerdict, pickRollbackTarget, readHistory, historyPath, distTagFor, cliRunner, dependencyList, installedVersion, registryTags, hotApply, runCycle, settingsOf, readOverrides, writeOverrides, takeProfileLock, releaseProfileLock, sessionAgeMinutes, activityReasons, settingsSnapshot, snapshotProfile, snapshotRoot, pruneSnapshots, nextDelayMinutes, readLogTail, logPath, pruneAll, pruneHistory, pruneLog }
|
|
2144
2301
|
|
|
2145
2302
|
export function apply(ctx, config) {
|
|
2146
2303
|
const ctxRef = { ctx, config }
|
|
@@ -2217,6 +2374,23 @@ export function apply(ctx, config) {
|
|
|
2217
2374
|
void go()
|
|
2218
2375
|
}
|
|
2219
2376
|
|
|
2377
|
+
/**
|
|
2378
|
+
* 定期清理。
|
|
2379
|
+
*
|
|
2380
|
+
* 放在每次定时器触发前,而不是另起一个定时器:调度器本来就按当前设置走,
|
|
2381
|
+
* 清理跟着它走就不会出现「两个节奏各跑各的、改了设置只有一个生效」。
|
|
2382
|
+
* 启动时也跑一次(见下面的 pruneAll(settings, new Date())),保证长时间不开
|
|
2383
|
+
* DSH 的用户一进来就看不到过期记录。
|
|
2384
|
+
*/
|
|
2385
|
+
const runCleanup = () => {
|
|
2386
|
+
try {
|
|
2387
|
+
pruneAll(settingsOf(ctxRef.config), new Date())
|
|
2388
|
+
} catch (error) {
|
|
2389
|
+
writeLog('routeFailed', [oneLine(error?.message ?? error, 200)])
|
|
2390
|
+
}
|
|
2391
|
+
}
|
|
2392
|
+
runCleanup()
|
|
2393
|
+
|
|
2220
2394
|
/**
|
|
2221
2395
|
* 定时器改成「每次重排」,而不是一次性 setInterval。
|
|
2222
2396
|
*
|
|
@@ -2258,6 +2432,7 @@ export function apply(ctx, config) {
|
|
|
2258
2432
|
ctxRef.pendingMinutes = minutes
|
|
2259
2433
|
const handle = setTimeout(() => {
|
|
2260
2434
|
scheduled = null
|
|
2435
|
+
runCleanup()
|
|
2261
2436
|
runSafely('update')
|
|
2262
2437
|
scheduleNext()
|
|
2263
2438
|
}, Math.max(minutes, 0) * 60000)
|
package/package.json
CHANGED