diff --git a/ingest/index.ts b/ingest/index.ts index c8df4ee..121ddc3 100644 --- a/ingest/index.ts +++ b/ingest/index.ts @@ -109,7 +109,14 @@ export async function updateDb( const mailPaths = getLoginMails(prefix); const doUpdate = async () => { - const res = await updatePaths(bookPaths, collectionPaths, resourcePaths, mailPaths, db, buildId, prevBuild, prefix, moved, relaxed); + let res: number | boolean = false; + try { + res = await updatePaths(bookPaths, collectionPaths, resourcePaths, mailPaths, db, buildId, prevBuild, prefix, moved, relaxed); + } catch (e) { + console.error(`Error updating ${prefix}:`, e); + await db.exec("ROLLBACK").catch(() => {}); + return false; + } if (res !== false) { prevBuild = new Date(); } @@ -119,8 +126,7 @@ export async function updateDb( return res !== false; } - anyErrors = anyErrors || ! - await doUpdate(); + anyErrors = anyErrors || !await doUpdate(); if (dev) { getDevWebSocketServer(); diff --git a/package.json b/package.json index 1dcd3c8..4f0088f 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "slugify": "^1.6.6", "sqlite": "^5.1.1", "sqlite3": "^5.1.7", + "tree-kill": "^1.2.2", "unified": "^11.0.5", "unist-util-visit": "^5.0.0", "uuid": "^14.0.0", diff --git a/scripts/ingest.ts b/scripts/ingest.ts index 5fe76b7..10f7757 100644 --- a/scripts/ingest.ts +++ b/scripts/ingest.ts @@ -3,7 +3,7 @@ dotenv.config({ path: ".env.local" }); import { program } from "commander"; import readline from "readline"; -import fs from "fs"; +import kill from "tree-kill"; import { updateDb } from "@/ingest"; import { rebuildDatabase } from "@/ingest/createDb"; @@ -17,6 +17,7 @@ const startNext = () => { { stdio: "inherit", shell: true, + detached: true, env: process.env } ); @@ -80,10 +81,14 @@ const ask = (question: string): Promise => { if (dev) { const nextProcess = startNext(); - const shutdown = () => { - nextProcess.kill("SIGTERM"); - process.exit(0); - }; + const shutdown = (reason?: unknown) => { + if (reason !== undefined) { + console.error("Error:", reason); + } + kill(nextProcess.pid!, "SIGTERM", () => process.exit()); + } + process.on("uncaughtException", shutdown); + process.on("unhandledRejection", shutdown); process.on("SIGINT", shutdown); process.on("SIGTERM", shutdown); } diff --git a/yarn.lock b/yarn.lock index 633c7f8..983bc0b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6499,6 +6499,11 @@ toggle-selection@^1.0.6: resolved "https://registry.yarnpkg.com/traverse/-/traverse-0.3.9.tgz#717b8f220cc0bb7b44e40514c22b2e8bbc70d8b9" integrity sha512-iawgk0hLP3SxGKDfnDJf8wTz4p2qImnyihM5Hh/sGvQ3K37dPi/w8sRhdNIxYA1TwFwc5mDhIJq+O0RsvXBKdQ== +tree-kill@^1.2.2: + version "1.2.2" + resolved "https://registry.yarnpkg.com/tree-kill/-/tree-kill-1.2.2.tgz#4ca09a9092c88b73a7cdc5e8a01b507b0790a0cc" + integrity sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A== + trim-lines@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/trim-lines/-/trim-lines-3.0.1.tgz#d802e332a07df861c48802c04321017b1bd87338"