From c8dc1adb9c9395d33eb6ec7bcf012c2f40cd3dbe Mon Sep 17 00:00:00 2001 From: Mia <49593536+mia-pi-git@users.noreply.github.com> Date: Sun, 15 Aug 2021 15:42:58 -0500 Subject: [PATCH] Chat: Actually spawn child processes SQL() to be called in something instantiated directly in the file, or at the top level, or the PM will not work. --- server/chat.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/server/chat.ts b/server/chat.ts index f332334992..d2004b2e60 100644 --- a/server/chat.ts +++ b/server/chat.ts @@ -1690,13 +1690,12 @@ export const Chat = new class { * All chat plugins share one database. * Chat.databaseReadyPromise will be truthy if the database is not yet ready. */ - database: SQLDatabaseManager | null = null; + database = SQL(module, {file: ('Config' in global && Config.nofswriting) ? ':memory:' : PLUGIN_DATABASE_PATH}); databaseReadyPromise: Promise | null = null; async prepareDatabase() { if (process.send) return; // We don't need a database in a subprocess that requires Chat. if (!Config.usesqlite) return; - this.database = SQL(module, {file: ('Config' in global && Config.nofswriting) ? ':memory:' : PLUGIN_DATABASE_PATH}); // check if we have the db_info table, which will always be present unless the schema needs to be initialized let statement = await this.database.prepare( `SELECT count(*) AS hasDBInfo FROM sqlite_master WHERE type = 'table' AND name = 'db_info'` @@ -2500,3 +2499,20 @@ export interface Monitor { condition?: string; monitor?: MonitorHandler; } + +if (Chat.database.isParentProcess) { + Chat.database.spawn(Config.chatdbprocesses || 1); +} else { + global.Monitor = { + crashlog(error: Error, source = 'A chat child process', details: AnyObject | null = null) { + const repr = JSON.stringify([error.name, error.message, source, details]); + process.send!(`THROW\n@!!@${repr}\n${error.stack}`); + }, + }; + process.on('uncaughtException', err => { + Monitor.crashlog(err, 'A chat database process'); + }); + process.on('unhandledRejection', err => { + Monitor.crashlog(err as Error, 'A chat database process'); + }); +}