@cloud411716/fancy-webnovel 4.17.1 → 4.17.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/lib/material-db.js +31 -18
- package/package.json +1 -1
package/lib/material-db.js
CHANGED
|
@@ -133,8 +133,9 @@ export async function ensureMaterialDatabase(root) {
|
|
|
133
133
|
/**
|
|
134
134
|
* Remove the workspace material database.
|
|
135
135
|
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
136
|
+
* Drop the known tables one by one first, then drop the database through the
|
|
137
|
+
* registry connection. This avoids a long-lived database-level DDL conflict
|
|
138
|
+
* on large material libraries.
|
|
138
139
|
*
|
|
139
140
|
* @param {string} root workspace directory
|
|
140
141
|
* @returns {Promise<{removed: boolean, database: string}>}
|
|
@@ -149,30 +150,42 @@ export async function removeMaterialDatabase(root) {
|
|
|
149
150
|
throw new Error('素材库数据库名不合法:' + database);
|
|
150
151
|
}
|
|
151
152
|
const registry = workspace.surreal?.registry_database || 'fancy_registry';
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
password: secrets.surreal?.password,
|
|
158
|
-
});
|
|
159
|
-
await db.use({ namespace, database: registry });
|
|
153
|
+
const retryable = (err) => {
|
|
154
|
+
const message = err && err.message ? err.message : String(err);
|
|
155
|
+
return /read or write conflict|transaction can be retried|failed to commit transaction|timed out/i.test(message);
|
|
156
|
+
};
|
|
157
|
+
const runWithRetry = async (label, fn) => {
|
|
160
158
|
let lastError = null;
|
|
161
|
-
const
|
|
162
|
-
for (const delay of delays) {
|
|
159
|
+
for (const delay of [0, 1000, 2500, 5000]) {
|
|
163
160
|
if (delay) await new Promise((resolve) => setTimeout(resolve, delay));
|
|
164
161
|
try {
|
|
165
|
-
await
|
|
166
|
-
return
|
|
162
|
+
await fn();
|
|
163
|
+
return;
|
|
167
164
|
} catch (err) {
|
|
168
|
-
|
|
169
|
-
const retryable = /read or write conflict|transaction can be retried|failed to commit transaction/i.test(message);
|
|
170
|
-
if (!retryable) throw err;
|
|
165
|
+
if (!retryable(err)) throw err;
|
|
171
166
|
lastError = err;
|
|
172
167
|
}
|
|
173
168
|
}
|
|
174
|
-
throw new Error('
|
|
169
|
+
throw new Error(label + '时数据库事务冲突,可能有其他素材/抖音任务正在读写。请等任务结束后重试。原始错误:'
|
|
175
170
|
+ (lastError && lastError.message ? lastError.message : String(lastError)));
|
|
171
|
+
};
|
|
172
|
+
const db = new Surreal({ engines: { ...createRemoteEngines() } });
|
|
173
|
+
try {
|
|
174
|
+
await db.connect(rpcUrl(workspace));
|
|
175
|
+
await db.signin({
|
|
176
|
+
username: secrets.surreal?.username || 'root',
|
|
177
|
+
password: secrets.surreal?.password,
|
|
178
|
+
});
|
|
179
|
+
// Drop the large material table first. Asking SurrealDB to drop the whole
|
|
180
|
+
// database in one DDL transaction can stay in a read/write conflict for a
|
|
181
|
+
// long time on a library with >100k records.
|
|
182
|
+
await db.use({ namespace, database });
|
|
183
|
+
for (const table of ['material', 'douyin_job', 'douyin_meta', 'douyin_account']) {
|
|
184
|
+
await runWithRetry('删除素材表 ' + table, () => db.query('REMOVE TABLE IF EXISTS ' + table + ';'));
|
|
185
|
+
}
|
|
186
|
+
await db.use({ namespace, database: registry });
|
|
187
|
+
await runWithRetry('删除素材库', () => db.query('REMOVE DATABASE IF EXISTS ' + database + ';'));
|
|
188
|
+
return { removed: true, database };
|
|
176
189
|
} finally {
|
|
177
190
|
try { await db.close(); } catch { /* ignore */ }
|
|
178
191
|
}
|