luckytri 1.0.0 → 1.0.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/.github/SECURITY.md +4 -4
- package/README.en.md +252 -250
- package/README.md +252 -250
- package/bin/luckytri.js +5 -1
- package/docs/en/CHANGELOG.md +8 -0
- package/docs/en/connect.md +18 -18
- package/docs/en/guide.md +202 -216
- package/docs/en/npm-release.md +25 -25
- package/docs/zh/CHANGELOG.md +8 -0
- package/docs/zh/connect.md +17 -17
- package/docs/zh/guide.md +202 -216
- package/docs/zh/npm-release.md +33 -33
- package/package.json +110 -108
- package/plugins/sdk/index.d.ts +155 -155
- package/plugins/sdk/testing.js +58 -58
- package/plugins/weather/index.js +82 -82
- package/plugins/weather/luckytri-plugin.json +29 -29
- package/public/app/assets/ChatsPage-BhlJHseG.js +3 -0
- package/public/app/assets/Empty-DP6WMGHi.js +2 -0
- package/public/app/assets/Field-CtbdqaiB.js +1 -0
- package/public/app/assets/HeartPage-32BeTPn2.js +1 -0
- package/public/app/assets/LifePage-CxSm0XKI.js +1 -0
- package/public/app/assets/MemoryPage-BeKBcPCY.js +1 -0
- package/public/app/assets/NaturePage-NEPYl2HS.js +3 -0
- package/public/app/assets/PeoplePage-DxtpekwM.js +1 -0
- package/public/app/assets/PluginsPage-CVVQwEBI.css +1 -0
- package/public/app/assets/PluginsPage-DksEZbUU.js +2 -0
- package/public/app/assets/{SystemPage-CAMPoYqM.css → SystemPage-BkRu0Rvx.css} +1 -1
- package/public/app/assets/SystemPage-BvzJlHuY.js +1 -0
- package/public/app/assets/TimePage-7-awH5XL.js +3 -0
- package/public/app/assets/index-4rItIkD-.js +4 -0
- package/public/app/assets/{index-YDz1ree1.css → index-DY4e15Kh.css} +1 -1
- package/public/app/assets/{knowledge-DG0e0uKw.js → knowledge-C6_HvoyB.js} +1 -1
- package/public/app/assets/media-SMfTT-yc.js +1 -0
- package/public/app/index.html +3 -3
- package/public/guide.en.html +72 -99
- package/public/guide.html +44 -86
- package/public/tutorial.en.md +25 -39
- package/public/tutorial.md +25 -39
- package/scripts/backup.js +311 -311
- package/scripts/browser.js +41 -41
- package/scripts/import-data.js +128 -0
- package/scripts/launch.js +101 -94
- package/scripts/plugin.js +150 -150
- package/scripts/reset-password.js +22 -0
- package/scripts/runtime.js +25 -25
- package/scripts/service.js +56 -47
- package/scripts/setup.js +50 -50
- package/scripts/start-server.js +41 -41
- package/scripts/stop.js +36 -36
- package/server/app.js +112 -96
- package/server/auth.js +217 -0
- package/server/channels/contract.js +57 -57
- package/server/channels/hub.js +165 -165
- package/server/channels/index.js +28 -25
- package/server/channels/onebot/index.js +335 -336
- package/server/channels/onebot/token.js +22 -3
- package/server/core/capabilities.js +208 -208
- package/server/core/events.js +64 -60
- package/server/core/image-preprocessor.js +208 -208
- package/server/core/model-manager.js +1213 -1213
- package/server/core/net-guard.js +49 -49
- package/server/core/orchestrator.js +569 -569
- package/server/core/perceivers.js +91 -91
- package/server/core/persona-manager.js +243 -243
- package/server/core/repository.js +293 -293
- package/server/core/response-validator.js +508 -508
- package/server/core/turn-processor.js +630 -630
- package/server/core/turn.js +236 -236
- package/server/core/vision-manager.js +633 -633
- package/server/i18n/errors.js +548 -497
- package/server/i18n/labels.js +649 -653
- package/server/index.js +151 -102
- package/server/knowledge/manager.js +386 -386
- package/server/knowledge/schema.js +12 -8
- package/server/local-access.js +32 -0
- package/server/mind/anticipations.js +507 -507
- package/server/mind/api.js +703 -703
- package/server/mind/budget.js +152 -152
- package/server/mind/context-selection.js +61 -61
- package/server/mind/day-planner.js +324 -324
- package/server/mind/evidence.js +148 -148
- package/server/mind/index.js +445 -445
- package/server/mind/life/diary.js +786 -786
- package/server/mind/life/index.js +1049 -1049
- package/server/mind/observations.js +84 -84
- package/server/mind/own-day.js +324 -324
- package/server/mind/own-voice.js +318 -318
- package/server/mind/senses.js +53 -53
- package/server/mind/time/activity-clock.js +196 -196
- package/server/mind/time/availability.js +69 -69
- package/server/mind/time/commitment-review.js +198 -198
- package/server/mind/time/executor.js +512 -512
- package/server/mind/time/experiences.js +128 -128
- package/server/mind/time/games.js +562 -562
- package/server/mind/time/index.js +739 -739
- package/server/mind/time/intent.js +166 -166
- package/server/mind/time/kinds.js +171 -171
- package/server/mind/time/plugin-activity.js +207 -207
- package/server/mind/time/tasks.js +587 -587
- package/server/mind/util.js +110 -110
- package/server/mind/view.js +273 -273
- package/server/paths.js +102 -102
- package/server/plugins/api.js +204 -204
- package/server/plugins/host.js +967 -967
- package/server/plugins/install.js +295 -295
- package/server/plugins/manifest.js +201 -201
- package/server/plugins/permissions.js +42 -42
- package/server/plugins/runner/guard.mjs +36 -36
- package/server/plugins/runner/worker.mjs +250 -250
- package/server/plugins/schema.js +91 -91
- package/server/plugins/zip.js +112 -112
- package/server/startup-lock.js +118 -118
- package/server/storage/archive.js +242 -241
- package/server/storage/backup-cleanup-worker.js +8 -8
- package/server/storage/backup-cleanup.js +415 -414
- package/server/storage/backup-lock.js +76 -76
- package/server/storage/backup-scheduler.js +131 -131
- package/server/storage/data-package.js +298 -0
- package/server/storage/data-transfer-api.js +50 -0
- package/server/storage/data-transfer-worker.js +18 -0
- package/server/storage/data-transfer.js +415 -0
- package/server/storage/store.js +202 -199
- package/server/studio/readiness.js +181 -190
- package/server/studio/settings.js +236 -236
- package/public/app/assets/ChatsPage-C-FcRapR.js +0 -3
- package/public/app/assets/Empty-CpP5kQHH.js +0 -2
- package/public/app/assets/Field-D4gY-INU.js +0 -1
- package/public/app/assets/HeartPage-CPy_thQd.js +0 -1
- package/public/app/assets/LifePage-CxWvwmSf.js +0 -1
- package/public/app/assets/MemoryPage-DC4rhQ68.js +0 -1
- package/public/app/assets/NaturePage-IvYw04Zq.js +0 -3
- package/public/app/assets/PeoplePage-Cqefm9-q.js +0 -1
- package/public/app/assets/PluginsPage-DKSaKvXb.js +0 -2
- package/public/app/assets/PluginsPage-HyBe5D8w.css +0 -1
- package/public/app/assets/SystemPage-CJmtJkrK.js +0 -1
- package/public/app/assets/TimePage-Cw44inM4.js +0 -3
- package/public/app/assets/index-BheRW-1t.js +0 -4
- package/public/app/assets/media-BzPDBm7e.js +0 -1
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
import {
|
|
2
|
-
closeSync,
|
|
3
|
-
existsSync,
|
|
4
|
-
mkdirSync,
|
|
5
|
-
openSync,
|
|
6
|
-
readFileSync,
|
|
7
|
-
rmSync,
|
|
8
|
-
statSync,
|
|
9
|
-
writeFileSync,
|
|
10
|
-
} from "node:fs";
|
|
11
|
-
import { join, resolve } from "node:path";
|
|
12
|
-
import { randomUUID } from "node:crypto";
|
|
13
|
-
|
|
14
|
-
const BUSY = "备份目录正在被使用,请稍后再试";
|
|
15
|
-
|
|
16
|
-
function alive(pid) {
|
|
17
|
-
if (!Number.isInteger(pid) || pid < 1) return false;
|
|
18
|
-
try {
|
|
19
|
-
process.kill(pid, 0);
|
|
20
|
-
return true;
|
|
21
|
-
} catch (error) {
|
|
22
|
-
return error.code !== "ESRCH";
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function withBackupLock(directory, run) {
|
|
27
|
-
const root = resolve(directory);
|
|
28
|
-
mkdirSync(root, { recursive: true });
|
|
29
|
-
const file = join(root, ".backup-operation.lock");
|
|
30
|
-
const token = randomUUID();
|
|
31
|
-
let acquired = false;
|
|
32
|
-
for (let attempt = 0; attempt < 2 && !acquired; attempt++) {
|
|
33
|
-
try {
|
|
34
|
-
const fd = openSync(file, "wx");
|
|
35
|
-
let written = false;
|
|
36
|
-
try {
|
|
37
|
-
writeFileSync(fd, JSON.stringify({ pid: process.pid, token }));
|
|
38
|
-
written = true;
|
|
39
|
-
} finally {
|
|
40
|
-
closeSync(fd);
|
|
41
|
-
if (!written) rmSync(file, { force: true });
|
|
42
|
-
}
|
|
43
|
-
acquired = true;
|
|
44
|
-
} catch (error) {
|
|
45
|
-
if (error.code !== "EEXIST") throw error;
|
|
46
|
-
const stat = statSync(file);
|
|
47
|
-
let owner;
|
|
48
|
-
try {
|
|
49
|
-
owner = JSON.parse(readFileSync(file, "utf8"));
|
|
50
|
-
} catch {
|
|
51
|
-
// A process may still be publishing its lock. Never steal a fresh one.
|
|
52
|
-
}
|
|
53
|
-
if (
|
|
54
|
-
(owner && !alive(owner.pid)) ||
|
|
55
|
-
(!owner && Date.now() - stat.mtimeMs > 6 * 3600000)
|
|
56
|
-
) {
|
|
57
|
-
const current = statSync(file);
|
|
58
|
-
if (current.ino === stat.ino && current.mtimeMs === stat.mtimeMs)
|
|
59
|
-
rmSync(file);
|
|
60
|
-
} else throw Error(BUSY);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (!acquired) throw Error(BUSY);
|
|
64
|
-
try {
|
|
65
|
-
return run();
|
|
66
|
-
} finally {
|
|
67
|
-
if (existsSync(file)) {
|
|
68
|
-
try {
|
|
69
|
-
const owner = JSON.parse(readFileSync(file, "utf8"));
|
|
70
|
-
if (owner.token === token) rmSync(file);
|
|
71
|
-
} catch {
|
|
72
|
-
// Leave a lock changed by another process alone.
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
closeSync,
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
openSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
statSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
} from "node:fs";
|
|
11
|
+
import { join, resolve } from "node:path";
|
|
12
|
+
import { randomUUID } from "node:crypto";
|
|
13
|
+
|
|
14
|
+
const BUSY = "备份目录正在被使用,请稍后再试";
|
|
15
|
+
|
|
16
|
+
function alive(pid) {
|
|
17
|
+
if (!Number.isInteger(pid) || pid < 1) return false;
|
|
18
|
+
try {
|
|
19
|
+
process.kill(pid, 0);
|
|
20
|
+
return true;
|
|
21
|
+
} catch (error) {
|
|
22
|
+
return error.code !== "ESRCH";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function withBackupLock(directory, run) {
|
|
27
|
+
const root = resolve(directory);
|
|
28
|
+
mkdirSync(root, { recursive: true });
|
|
29
|
+
const file = join(root, ".backup-operation.lock");
|
|
30
|
+
const token = randomUUID();
|
|
31
|
+
let acquired = false;
|
|
32
|
+
for (let attempt = 0; attempt < 2 && !acquired; attempt++) {
|
|
33
|
+
try {
|
|
34
|
+
const fd = openSync(file, "wx");
|
|
35
|
+
let written = false;
|
|
36
|
+
try {
|
|
37
|
+
writeFileSync(fd, JSON.stringify({ pid: process.pid, token }));
|
|
38
|
+
written = true;
|
|
39
|
+
} finally {
|
|
40
|
+
closeSync(fd);
|
|
41
|
+
if (!written) rmSync(file, { force: true });
|
|
42
|
+
}
|
|
43
|
+
acquired = true;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (error.code !== "EEXIST") throw error;
|
|
46
|
+
const stat = statSync(file);
|
|
47
|
+
let owner;
|
|
48
|
+
try {
|
|
49
|
+
owner = JSON.parse(readFileSync(file, "utf8"));
|
|
50
|
+
} catch {
|
|
51
|
+
// A process may still be publishing its lock. Never steal a fresh one.
|
|
52
|
+
}
|
|
53
|
+
if (
|
|
54
|
+
(owner && !alive(owner.pid)) ||
|
|
55
|
+
(!owner && Date.now() - stat.mtimeMs > 6 * 3600000)
|
|
56
|
+
) {
|
|
57
|
+
const current = statSync(file);
|
|
58
|
+
if (current.ino === stat.ino && current.mtimeMs === stat.mtimeMs)
|
|
59
|
+
rmSync(file);
|
|
60
|
+
} else throw Error(BUSY);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (!acquired) throw Error(BUSY);
|
|
64
|
+
try {
|
|
65
|
+
return run();
|
|
66
|
+
} finally {
|
|
67
|
+
if (existsSync(file)) {
|
|
68
|
+
try {
|
|
69
|
+
const owner = JSON.parse(readFileSync(file, "utf8"));
|
|
70
|
+
if (owner.token === token) rmSync(file);
|
|
71
|
+
} catch {
|
|
72
|
+
// Leave a lock changed by another process alone.
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
|
-
import { constants, setPriority } from "node:os";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
|
-
import { runtimePaths } from "../paths.js";
|
|
5
|
-
import {
|
|
6
|
-
autoBackupDue,
|
|
7
|
-
autoBackupOptions,
|
|
8
|
-
backupDirectory,
|
|
9
|
-
listAutoBackups,
|
|
10
|
-
} from "../../scripts/backup.js";
|
|
11
|
-
|
|
12
|
-
const SCRIPT = fileURLToPath(
|
|
13
|
-
new URL("../../scripts/backup.js", import.meta.url),
|
|
14
|
-
);
|
|
15
|
-
const MINUTE = 60000;
|
|
16
|
-
const HOUR = 60 * MINUTE;
|
|
17
|
-
|
|
18
|
-
// Her whole continuity lives in one database file. Once a day a copy is made in
|
|
19
|
-
// a separate process, so a slow disk never stalls a conversation, and only the
|
|
20
|
-
// newest few are kept. `BACKUP_INTERVAL_HOURS=0` turns it off.
|
|
21
|
-
export function createBackupScheduler({
|
|
22
|
-
env = process.env,
|
|
23
|
-
directory = backupDirectory(runtimePaths(env).database, env),
|
|
24
|
-
now = () => Date.now(),
|
|
25
|
-
start = spawn,
|
|
26
|
-
log = console,
|
|
27
|
-
firstCheckDelayMs = 2 * MINUTE,
|
|
28
|
-
checkEveryMs = 10 * MINUTE,
|
|
29
|
-
} = {}) {
|
|
30
|
-
const options = autoBackupOptions(env);
|
|
31
|
-
const source = runtimePaths(env).database;
|
|
32
|
-
let running = null;
|
|
33
|
-
let failures = 0;
|
|
34
|
-
let lastError = null;
|
|
35
|
-
let nextCheck = now() + firstCheckDelayMs;
|
|
36
|
-
return {
|
|
37
|
-
enabled: options.enabled,
|
|
38
|
-
get running() {
|
|
39
|
-
return !!running;
|
|
40
|
-
},
|
|
41
|
-
// What the management page shows: whether her continuity is being kept.
|
|
42
|
-
status() {
|
|
43
|
-
let backups = [];
|
|
44
|
-
try {
|
|
45
|
-
backups = listAutoBackups(directory, { source });
|
|
46
|
-
} catch (error) {
|
|
47
|
-
lastError ||= { at: now(), message: error.message };
|
|
48
|
-
}
|
|
49
|
-
const latest = backups[0];
|
|
50
|
-
return {
|
|
51
|
-
enabled: options.enabled,
|
|
52
|
-
intervalHours: options.intervalMs / HOUR,
|
|
53
|
-
keep: options.keep,
|
|
54
|
-
running: !!running,
|
|
55
|
-
count: backups.length,
|
|
56
|
-
latest: latest
|
|
57
|
-
? { name: latest.name, at: latest.time, bytes: latest.bytes }
|
|
58
|
-
: null,
|
|
59
|
-
lastError,
|
|
60
|
-
};
|
|
61
|
-
},
|
|
62
|
-
tick() {
|
|
63
|
-
if (!options.enabled || running || now() < nextCheck) return false;
|
|
64
|
-
nextCheck = now() + checkEveryMs;
|
|
65
|
-
let due;
|
|
66
|
-
try {
|
|
67
|
-
due = autoBackupDue(directory, {
|
|
68
|
-
now: now(),
|
|
69
|
-
intervalMs: options.intervalMs,
|
|
70
|
-
source,
|
|
71
|
-
});
|
|
72
|
-
} catch (error) {
|
|
73
|
-
log.error(`检查自动备份失败:${error.message}`);
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
if (!due) return false;
|
|
77
|
-
const child = start(process.execPath, [SCRIPT, "--auto"], {
|
|
78
|
-
env: {
|
|
79
|
-
...env,
|
|
80
|
-
LUCKYTRI_HOME: runtimePaths(env).home,
|
|
81
|
-
DB_PATH: source,
|
|
82
|
-
BACKUP_DIR: directory,
|
|
83
|
-
},
|
|
84
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
85
|
-
windowsHide: true,
|
|
86
|
-
});
|
|
87
|
-
running = child;
|
|
88
|
-
try {
|
|
89
|
-
// Below normal: the conversation keeps the disk and CPU it needs.
|
|
90
|
-
if (child.pid)
|
|
91
|
-
setPriority(child.pid, constants.priority.PRIORITY_BELOW_NORMAL);
|
|
92
|
-
} catch {
|
|
93
|
-
/* Not every platform lets a process lower another's priority. */
|
|
94
|
-
}
|
|
95
|
-
let output = "";
|
|
96
|
-
child.stdout?.on("data", (chunk) => (output += chunk));
|
|
97
|
-
child.stderr?.on("data", (chunk) => (output += chunk));
|
|
98
|
-
const done = (code, error) => {
|
|
99
|
-
if (running !== child) return;
|
|
100
|
-
running = null;
|
|
101
|
-
if (error || code) {
|
|
102
|
-
// A failing disk must not be hammered every few minutes: wait longer
|
|
103
|
-
// after each failure, up to six hours.
|
|
104
|
-
failures++;
|
|
105
|
-
nextCheck = now() + Math.min(6, failures) * HOUR;
|
|
106
|
-
const message = (error?.message || output).trim() || `退出码 ${code}`;
|
|
107
|
-
lastError = { at: now(), message: message.slice(0, 300) };
|
|
108
|
-
log.error(`自动备份失败:${message}`);
|
|
109
|
-
} else {
|
|
110
|
-
failures = 0;
|
|
111
|
-
lastError = null;
|
|
112
|
-
log.log(output.trim() || "自动备份完成");
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
child.once("error", (error) => done(1, error));
|
|
116
|
-
child.once("exit", (code) => done(code));
|
|
117
|
-
return true;
|
|
118
|
-
},
|
|
119
|
-
stop() {
|
|
120
|
-
const child = running;
|
|
121
|
-
running = null;
|
|
122
|
-
if (child) {
|
|
123
|
-
try {
|
|
124
|
-
child.kill();
|
|
125
|
-
} catch {
|
|
126
|
-
/* Already gone. */
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
};
|
|
131
|
-
}
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { constants, setPriority } from "node:os";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { runtimePaths } from "../paths.js";
|
|
5
|
+
import {
|
|
6
|
+
autoBackupDue,
|
|
7
|
+
autoBackupOptions,
|
|
8
|
+
backupDirectory,
|
|
9
|
+
listAutoBackups,
|
|
10
|
+
} from "../../scripts/backup.js";
|
|
11
|
+
|
|
12
|
+
const SCRIPT = fileURLToPath(
|
|
13
|
+
new URL("../../scripts/backup.js", import.meta.url),
|
|
14
|
+
);
|
|
15
|
+
const MINUTE = 60000;
|
|
16
|
+
const HOUR = 60 * MINUTE;
|
|
17
|
+
|
|
18
|
+
// Her whole continuity lives in one database file. Once a day a copy is made in
|
|
19
|
+
// a separate process, so a slow disk never stalls a conversation, and only the
|
|
20
|
+
// newest few are kept. `BACKUP_INTERVAL_HOURS=0` turns it off.
|
|
21
|
+
export function createBackupScheduler({
|
|
22
|
+
env = process.env,
|
|
23
|
+
directory = backupDirectory(runtimePaths(env).database, env),
|
|
24
|
+
now = () => Date.now(),
|
|
25
|
+
start = spawn,
|
|
26
|
+
log = console,
|
|
27
|
+
firstCheckDelayMs = 2 * MINUTE,
|
|
28
|
+
checkEveryMs = 10 * MINUTE,
|
|
29
|
+
} = {}) {
|
|
30
|
+
const options = autoBackupOptions(env);
|
|
31
|
+
const source = runtimePaths(env).database;
|
|
32
|
+
let running = null;
|
|
33
|
+
let failures = 0;
|
|
34
|
+
let lastError = null;
|
|
35
|
+
let nextCheck = now() + firstCheckDelayMs;
|
|
36
|
+
return {
|
|
37
|
+
enabled: options.enabled,
|
|
38
|
+
get running() {
|
|
39
|
+
return !!running;
|
|
40
|
+
},
|
|
41
|
+
// What the management page shows: whether her continuity is being kept.
|
|
42
|
+
status() {
|
|
43
|
+
let backups = [];
|
|
44
|
+
try {
|
|
45
|
+
backups = listAutoBackups(directory, { source });
|
|
46
|
+
} catch (error) {
|
|
47
|
+
lastError ||= { at: now(), message: error.message };
|
|
48
|
+
}
|
|
49
|
+
const latest = backups[0];
|
|
50
|
+
return {
|
|
51
|
+
enabled: options.enabled,
|
|
52
|
+
intervalHours: options.intervalMs / HOUR,
|
|
53
|
+
keep: options.keep,
|
|
54
|
+
running: !!running,
|
|
55
|
+
count: backups.length,
|
|
56
|
+
latest: latest
|
|
57
|
+
? { name: latest.name, at: latest.time, bytes: latest.bytes }
|
|
58
|
+
: null,
|
|
59
|
+
lastError,
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
tick() {
|
|
63
|
+
if (!options.enabled || running || now() < nextCheck) return false;
|
|
64
|
+
nextCheck = now() + checkEveryMs;
|
|
65
|
+
let due;
|
|
66
|
+
try {
|
|
67
|
+
due = autoBackupDue(directory, {
|
|
68
|
+
now: now(),
|
|
69
|
+
intervalMs: options.intervalMs,
|
|
70
|
+
source,
|
|
71
|
+
});
|
|
72
|
+
} catch (error) {
|
|
73
|
+
log.error(`检查自动备份失败:${error.message}`);
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (!due) return false;
|
|
77
|
+
const child = start(process.execPath, [SCRIPT, "--auto"], {
|
|
78
|
+
env: {
|
|
79
|
+
...env,
|
|
80
|
+
LUCKYTRI_HOME: runtimePaths(env).home,
|
|
81
|
+
DB_PATH: source,
|
|
82
|
+
BACKUP_DIR: directory,
|
|
83
|
+
},
|
|
84
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
85
|
+
windowsHide: true,
|
|
86
|
+
});
|
|
87
|
+
running = child;
|
|
88
|
+
try {
|
|
89
|
+
// Below normal: the conversation keeps the disk and CPU it needs.
|
|
90
|
+
if (child.pid)
|
|
91
|
+
setPriority(child.pid, constants.priority.PRIORITY_BELOW_NORMAL);
|
|
92
|
+
} catch {
|
|
93
|
+
/* Not every platform lets a process lower another's priority. */
|
|
94
|
+
}
|
|
95
|
+
let output = "";
|
|
96
|
+
child.stdout?.on("data", (chunk) => (output += chunk));
|
|
97
|
+
child.stderr?.on("data", (chunk) => (output += chunk));
|
|
98
|
+
const done = (code, error) => {
|
|
99
|
+
if (running !== child) return;
|
|
100
|
+
running = null;
|
|
101
|
+
if (error || code) {
|
|
102
|
+
// A failing disk must not be hammered every few minutes: wait longer
|
|
103
|
+
// after each failure, up to six hours.
|
|
104
|
+
failures++;
|
|
105
|
+
nextCheck = now() + Math.min(6, failures) * HOUR;
|
|
106
|
+
const message = (error?.message || output).trim() || `退出码 ${code}`;
|
|
107
|
+
lastError = { at: now(), message: message.slice(0, 300) };
|
|
108
|
+
log.error(`自动备份失败:${message}`);
|
|
109
|
+
} else {
|
|
110
|
+
failures = 0;
|
|
111
|
+
lastError = null;
|
|
112
|
+
log.log(output.trim() || "自动备份完成");
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
child.once("error", (error) => done(1, error));
|
|
116
|
+
child.once("exit", (code) => done(code));
|
|
117
|
+
return true;
|
|
118
|
+
},
|
|
119
|
+
stop() {
|
|
120
|
+
const child = running;
|
|
121
|
+
running = null;
|
|
122
|
+
if (child) {
|
|
123
|
+
try {
|
|
124
|
+
child.kill();
|
|
125
|
+
} catch {
|
|
126
|
+
/* Already gone. */
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|