@cloud411716/fancy-webnovel 4.17.0 → 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.
Files changed (2) hide show
  1. package/lib/material-db.js +30 -3
  2. package/package.json +1 -1
@@ -133,8 +133,9 @@ export async function ensureMaterialDatabase(root) {
133
133
  /**
134
134
  * Remove the workspace material database.
135
135
  *
136
- * Connect through the registry database (never through the target database)
137
- * and issue `REMOVE DATABASE IF EXISTS`.
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,6 +150,25 @@ export async function removeMaterialDatabase(root) {
149
150
  throw new Error('素材库数据库名不合法:' + database);
150
151
  }
151
152
  const registry = workspace.surreal?.registry_database || 'fancy_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) => {
158
+ let lastError = null;
159
+ for (const delay of [0, 1000, 2500, 5000]) {
160
+ if (delay) await new Promise((resolve) => setTimeout(resolve, delay));
161
+ try {
162
+ await fn();
163
+ return;
164
+ } catch (err) {
165
+ if (!retryable(err)) throw err;
166
+ lastError = err;
167
+ }
168
+ }
169
+ throw new Error(label + '时数据库事务冲突,可能有其他素材/抖音任务正在读写。请等任务结束后重试。原始错误:'
170
+ + (lastError && lastError.message ? lastError.message : String(lastError)));
171
+ };
152
172
  const db = new Surreal({ engines: { ...createRemoteEngines() } });
153
173
  try {
154
174
  await db.connect(rpcUrl(workspace));
@@ -156,8 +176,15 @@ export async function removeMaterialDatabase(root) {
156
176
  username: secrets.surreal?.username || 'root',
157
177
  password: secrets.surreal?.password,
158
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
+ }
159
186
  await db.use({ namespace, database: registry });
160
- await db.query('REMOVE DATABASE IF EXISTS ' + database + ';');
187
+ await runWithRetry('删除素材库', () => db.query('REMOVE DATABASE IF EXISTS ' + database + ';'));
161
188
  return { removed: true, database };
162
189
  } finally {
163
190
  try { await db.close(); } catch { /* ignore */ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud411716/fancy-webnovel",
3
- "version": "4.17.0",
3
+ "version": "4.17.2",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "exports": {