From a135efff036de8ee9172d615947ca29edb3c2e72 Mon Sep 17 00:00:00 2001 From: aoirint Date: Wed, 9 Mar 2022 04:25:57 +0900 Subject: [PATCH 01/31] add runEngineAll --- src/background.ts | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/background.ts b/src/background.ts index 7bec5bb805..eae9bc10c7 100644 --- a/src/background.ts +++ b/src/background.ts @@ -348,10 +348,24 @@ const store = new Store<{ }); // engine -let willQuitEngine = false; -let engineProcess: ChildProcess; -async function runEngine() { - const engineInfo = engineInfos[0]; // TODO: 複数エンジン対応 +type EngineProcessContainer = { + willQuitEngine: boolean; + engineProcess?: ChildProcess; +} + +const engineProcessContainers: Record = {}; + +async function runEngineAll() { + log.info(`Starting ${engineInfos.length} engines...`); + + for (const engineInfo of engineInfos) { + log.info(`ENGINE ${engineInfo.key}: Start launching`); + await runEngine(engineInfo.key); + } +} + +async function runEngine(engineKey: string) { + const engineInfo = engineInfos.find((engineInfo) => engineInfo.key === engineKey) if (!engineInfo) throw new Error(`No such engineInfo registered: index == 0`); if (!engineInfo.executionEnabled) { @@ -364,7 +378,14 @@ async function runEngine() { return; } - willQuitEngine = false; + if (!(engineKey in engineProcessContainers)) { + engineProcessContainers[engineKey] = { + willQuitEngine: false, + }; + } + + const engineProcessContainer = engineProcessContainers[engineKey]; + engineProcessContainer.willQuitEngine = false; // 最初のエンジンモード if (!store.has("useGpu")) { @@ -399,9 +420,10 @@ async function runEngine() { log.info(`ENGINE path: ${enginePath}`); log.info(`ENGINE args: ${JSON.stringify(args)}`); - engineProcess = spawn(enginePath, args, { + const engineProcess = spawn(enginePath, args, { cwd: path.dirname(enginePath), }); + engineProcessContainer.engineProcess = engineProcess; engineProcess.stdout?.on("data", (data) => { log.info(`ENGINE: ${data.toString("utf-8")}`); @@ -415,7 +437,7 @@ async function runEngine() { log.info(`ENGINE: process terminated due to receipt of signal ${signal}`); log.info(`ENGINE: process exited with code ${code}`); - if (!willQuitEngine) { + if (!engineProcessContainer.willQuitEngine) { ipcMainSend(win, "DETECTED_ENGINE_ERROR"); dialog.showErrorBox( "音声合成エンジンエラー", From f81a77430d3f80a5262c12b448361e8fa9515afd Mon Sep 17 00:00:00 2001 From: aoirint Date: Wed, 9 Mar 2022 04:29:41 +0900 Subject: [PATCH 02/31] fix log --- src/background.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/background.ts b/src/background.ts index eae9bc10c7..de3e4826f5 100644 --- a/src/background.ts +++ b/src/background.ts @@ -378,6 +378,8 @@ async function runEngine(engineKey: string) { return; } + log.info(`Starting ENGINE ${engineKey} process`); + if (!(engineKey in engineProcessContainers)) { engineProcessContainers[engineKey] = { willQuitEngine: false, @@ -407,8 +409,7 @@ async function runEngine(engineKey: string) { } const useGpu = store.get("useGpu"); - log.info(`Starting ENGINE`); - log.info(`ENGINE mode: ${useGpu ? "GPU" : "CPU"}`); + log.info(`ENGINE ${engineKey} mode: ${useGpu ? "GPU" : "CPU"}`); // エンジンプロセスの起動 const enginePath = path.resolve( @@ -417,8 +418,8 @@ async function runEngine(engineKey: string) { ); const args = useGpu ? ["--use_gpu"] : []; - log.info(`ENGINE path: ${enginePath}`); - log.info(`ENGINE args: ${JSON.stringify(args)}`); + log.info(`ENGINE ${engineKey} path: ${enginePath}`); + log.info(`ENGINE ${engineKey} args: ${JSON.stringify(args)}`); const engineProcess = spawn(enginePath, args, { cwd: path.dirname(enginePath), @@ -426,16 +427,16 @@ async function runEngine(engineKey: string) { engineProcessContainer.engineProcess = engineProcess; engineProcess.stdout?.on("data", (data) => { - log.info(`ENGINE: ${data.toString("utf-8")}`); + log.info(`ENGINE ${engineKey} STDOUT: ${data.toString("utf-8")}`); }); engineProcess.stderr?.on("data", (data) => { - log.error(`ENGINE: ${data.toString("utf-8")}`); + log.error(`ENGINE ${engineKey} STDERR: ${data.toString("utf-8")}`); }); engineProcess.on("close", (code, signal) => { - log.info(`ENGINE: process terminated due to receipt of signal ${signal}`); - log.info(`ENGINE: process exited with code ${code}`); + log.info(`ENGINE ${engineKey} process terminated due to receipt of signal ${signal}`); + log.info(`ENGINE ${engineKey} process exited with code ${code}`); if (!engineProcessContainer.willQuitEngine) { ipcMainSend(win, "DETECTED_ENGINE_ERROR"); From 22e0168bf343ae075f55b5a6f8f466e71c13b39a Mon Sep 17 00:00:00 2001 From: aoirint Date: Wed, 9 Mar 2022 04:36:14 +0900 Subject: [PATCH 03/31] fix log --- src/background.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/background.ts b/src/background.ts index de3e4826f5..3901cf5550 100644 --- a/src/background.ts +++ b/src/background.ts @@ -351,7 +351,7 @@ const store = new Store<{ type EngineProcessContainer = { willQuitEngine: boolean; engineProcess?: ChildProcess; -} +}; const engineProcessContainers: Record = {}; @@ -365,20 +365,22 @@ async function runEngineAll() { } async function runEngine(engineKey: string) { - const engineInfo = engineInfos.find((engineInfo) => engineInfo.key === engineKey) - if (!engineInfo) throw new Error(`No such engineInfo registered: index == 0`); + const engineInfo = engineInfos.find( + (engineInfo) => engineInfo.key === engineKey + ); + if (!engineInfo) throw new Error(`No such engineInfo registered: key == ${engineKey}`); if (!engineInfo.executionEnabled) { - log.info("Skipped engineInfo execution: disabled"); + log.info(`ENGINE ${engineKey}: Skipped engineInfo execution: disabled`); return; } if (!engineInfo.executionFilePath) { - log.info("Skipped engineInfo execution: empty executionFilePath"); + log.info(`ENGINE ${engineKey}: Skipped engineInfo execution: empty executionFilePath`); return; } - log.info(`Starting ENGINE ${engineKey} process`); + log.info(`ENGINE ${engineKey}: Starting process`); if (!(engineKey in engineProcessContainers)) { engineProcessContainers[engineKey] = { @@ -435,8 +437,10 @@ async function runEngine(engineKey: string) { }); engineProcess.on("close", (code, signal) => { - log.info(`ENGINE ${engineKey} process terminated due to receipt of signal ${signal}`); - log.info(`ENGINE ${engineKey} process exited with code ${code}`); + log.info( + `ENGINE ${engineKey}: Process terminated due to receipt of signal ${signal}` + ); + log.info(`ENGINE ${engineKey}: Process exited with code ${code}`); if (!engineProcessContainer.willQuitEngine) { ipcMainSend(win, "DETECTED_ENGINE_ERROR"); From c5202dd3cb09e6edce41d4d7f617b13dd2368e6f Mon Sep 17 00:00:00 2001 From: aoirint Date: Wed, 9 Mar 2022 04:38:36 +0900 Subject: [PATCH 04/31] add killEngineAll --- src/background.ts | 64 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/src/background.ts b/src/background.ts index 3901cf5550..bc9f420040 100644 --- a/src/background.ts +++ b/src/background.ts @@ -368,7 +368,8 @@ async function runEngine(engineKey: string) { const engineInfo = engineInfos.find( (engineInfo) => engineInfo.key === engineKey ); - if (!engineInfo) throw new Error(`No such engineInfo registered: key == ${engineKey}`); + if (!engineInfo) + throw new Error(`No such engineInfo registered: key == ${engineKey}`); if (!engineInfo.executionEnabled) { log.info(`ENGINE ${engineKey}: Skipped engineInfo execution: disabled`); @@ -376,7 +377,9 @@ async function runEngine(engineKey: string) { } if (!engineInfo.executionFilePath) { - log.info(`ENGINE ${engineKey}: Skipped engineInfo execution: empty executionFilePath`); + log.info( + `ENGINE ${engineKey}: Skipped engineInfo execution: empty executionFilePath` + ); return; } @@ -452,39 +455,86 @@ async function runEngine(engineKey: string) { }); } +function killEngineAll({ + onFirstKillStart, + onAllKilled, + onError, +}: { + onFirstKillStart?: VoidFunction; + onAllKilled?: VoidFunction; + onError?: (engineKey: string, message: unknown) => void; +}) { + let anyKillStart = false; + + const numEngineProcess = Object.keys(engineProcessContainers).length; + let numEngineProcessKilled = 0; + + for (const [engineKey] of Object.entries(engineProcessContainers)) { + killEngine({ + engineKey, + onKillStart: () => { + if (!anyKillStart) { + anyKillStart = true; + onFirstKillStart?.(); + } + }, + onKilled: () => { + numEngineProcessKilled++; + log.info( + `ENGINE ${numEngineProcessKilled} / ${numEngineProcess} processes killed` + ); + + if (numEngineProcessKilled === numEngineProcess) { + onAllKilled?.(); + } + }, + onError: (message) => onError?.(engineKey, message), + }); + } +} + function killEngine({ + engineKey, onKillStart, onKilled, onError, }: { + engineKey: string; onKillStart?: VoidFunction; onKilled?: VoidFunction; onError?: (error: unknown) => void; }) { + const engineProcessContainer = engineProcessContainers[engineKey]; + if (!engineProcessContainer) { + onError?.(`No such engineProcessContainer: ${engineKey}`); + return; + } + + const engineProcess = engineProcessContainer.engineProcess; if (engineProcess == undefined) { // nop if no process started (already killed or not started yet) - log.info(`ENGINE process not started`); + log.info(`ENGINE ${engineKey}: Process not started`); return; } // considering the case that ENGINE process killed after checking process status engineProcess.once("close", () => { - log.info("ENGINE process closed"); + log.info(`ENGINE ${engineKey}: Process closed`); onKilled?.(); }); log.info( - `ENGINE last exit code: ${engineProcess.exitCode}, signal: ${engineProcess.signalCode}` + `ENGINE ${engineKey}: last exit code: ${engineProcess.exitCode}, signal: ${engineProcess.signalCode}` ); const engineNotExited = engineProcess.exitCode === null; const engineNotKilled = engineProcess.signalCode === null; if (engineNotExited && engineNotKilled) { - log.info(`Killing ENGINE process (PID=${engineProcess.pid})...`); + log.info(`ENGINE ${engineKey}: Killing process (PID=${engineProcess.pid})`); onKillStart?.(); - willQuitEngine = true; + engineProcessContainer.willQuitEngine = true; try { engineProcess.pid != undefined && treeKill(engineProcess.pid); } catch (error: unknown) { From 6e46ad9cab64c8833caca35f0fd4788c4bc650d0 Mon Sep 17 00:00:00 2001 From: aoirint Date: Wed, 9 Mar 2022 04:39:46 +0900 Subject: [PATCH 05/31] fix log --- src/background.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/background.ts b/src/background.ts index bc9f420040..29808f0b68 100644 --- a/src/background.ts +++ b/src/background.ts @@ -506,7 +506,7 @@ function killEngine({ }) { const engineProcessContainer = engineProcessContainers[engineKey]; if (!engineProcessContainer) { - onError?.(`No such engineProcessContainer: ${engineKey}`); + onError?.(`No such engineProcessContainer: key == ${engineKey}`); return; } From 31bfe9f8b9bf2b7fbed5a9b140aa2b47b98e0d7a Mon Sep 17 00:00:00 2001 From: aoirint Date: Wed, 9 Mar 2022 04:40:23 +0900 Subject: [PATCH 06/31] fix log --- src/background.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/background.ts b/src/background.ts index 29808f0b68..47cc19003c 100644 --- a/src/background.ts +++ b/src/background.ts @@ -538,10 +538,11 @@ function killEngine({ try { engineProcess.pid != undefined && treeKill(engineProcess.pid); } catch (error: unknown) { + log.error(`ENGINE ${engineKey}: Error during killing process`); onError?.(error); } } else { - log.info("ENGINE process already closed"); + log.info(`ENGINE ${engineKey}: Process already closed`); } } From eb919bd624ffafd9b102c861ef74a0bf15bce30c Mon Sep 17 00:00:00 2001 From: aoirint Date: Wed, 9 Mar 2022 04:53:00 +0900 Subject: [PATCH 07/31] add restartEngineAll --- src/background.ts | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/background.ts b/src/background.ts index 47cc19003c..b6f0b52974 100644 --- a/src/background.ts +++ b/src/background.ts @@ -546,45 +546,58 @@ function killEngine({ } } -async function restartEngine() { +async function restartEngineAll() { + for (const engineInfo of engineInfos) { + await restartEngine(engineInfo.key); + } +} + +async function restartEngine(engineKey: string) { await new Promise((resolve, reject) => { + const engineProcessContainer: EngineProcessContainer | undefined = + engineProcessContainers[engineKey]; + const engineProcess = engineProcessContainer?.engineProcess; + log.info( - `Restarting ENGINE (last exit code: ${engineProcess.exitCode}, signal: ${engineProcess.signalCode})` + `ENGINE ${engineKey}: Restarting process (last exit code: ${engineProcess?.exitCode}, signal: ${engineProcess?.signalCode})` ); // エンジンのプロセスがすでに終了している、またはkillされている場合 - const engineExited = engineProcess.exitCode !== null; - const engineKilled = engineProcess.signalCode !== null; + const engineExited = engineProcess?.exitCode !== null; + const engineKilled = engineProcess?.signalCode !== null; + // engineProcess === undefinedの場合true if (engineExited || engineKilled) { log.info( - "ENGINE process is not started yet or already killed. Starting ENGINE..." + `ENGINE ${engineKey}: Process is not started yet or already killed. Starting process...` ); - runEngine(); + runEngine(engineKey); resolve(); return; } // エンジンエラー時のエラーウィンドウ抑制用。 - willQuitEngine = true; + engineProcessContainer.willQuitEngine = true; // 「killに使用するコマンドが終了するタイミング」と「OSがプロセスをkillするタイミング」が違うので単純にtreeKillのコールバック関数でrunEngine()を実行すると失敗します。 // closeイベントはexitイベントよりも後に発火します。 const restartEngineOnProcessClosedCallback = () => { - log.info("ENGINE process killed. Restarting ENGINE..."); + log.info(`ENGINE ${engineKey}: Process killed. Restarting process...`); - runEngine(); + runEngine(engineKey); resolve(); }; engineProcess.once("close", restartEngineOnProcessClosedCallback); // treeKillのコールバック関数はコマンドが終了した時に呼ばれます。 - log.info(`Killing current ENGINE process (PID=${engineProcess.pid})...`); + log.info( + `ENGINE ${engineKey}: Killing current process (PID=${engineProcess.pid})...` + ); treeKill(engineProcess.pid, (error) => { // error変数の値がundefined以外であればkillコマンドが失敗したことを意味します。 if (error != null) { - log.error("Failed to kill ENGINE"); + log.error(`ENGINE ${engineKey}: Failed to kill process`); log.error(error); // killに失敗したとき、closeイベントが発生せず、once listenerが消費されない From 39b81df81d3b3f4e3246d351bf7b31eb9332a000 Mon Sep 17 00:00:00 2001 From: aoirint Date: Wed, 9 Mar 2022 04:59:20 +0900 Subject: [PATCH 08/31] call killEngineAll --- src/background.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/background.ts b/src/background.ts index b6f0b52974..97813d9443 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1002,8 +1002,12 @@ ipcMainHandle("ENGINE_INFOS", () => { * エンジンを再起動する。 * エンジンの起動が開始したらresolve、起動が失敗したらreject。 */ -ipcMainHandle("RESTART_ENGINE", async () => { - await restartEngine(); +ipcMainHandle("RESTART_ENGINE_ALL", async () => { + await restartEngineAll(); +}); + +ipcMainHandle("RESTART_ENGINE", async (_, { engineKey }) => { + await restartEngine(engineKey); }); ipcMainHandle("SAVING_SETTING", (_, { newData }) => { @@ -1150,20 +1154,20 @@ app.on("before-quit", (event) => { return; } - killEngine({ - onKillStart: () => { + log.info("Checking ENGINE status before app quit"); + killEngineAll({ + onFirstKillStart: () => { // executed synchronously to cancel before-quit event log.info("Interrupt app quit to kill ENGINE"); event.preventDefault(); }, - onKilled: () => { - // executed asynchronously to catch process closed event - log.info("ENGINE killed. Quitting app"); + onAllKilled: () => { + // executed asynchronously to catch all process closed event + log.info("All ENGINE killed. Quitting app"); app.quit(); // attempt to quit app again }, - onError: (error: unknown) => { - log.error("Error during killing ENGINE process"); - log.error(error); + onError: (engineKey, message) => { + console.error(`ENGINE ${engineKey}: Error during killing process: ${message}`); }, }); }); @@ -1191,7 +1195,7 @@ app.on("ready", async () => { } } - createWindow().then(() => runEngine()); + createWindow().then(() => runEngineAll()); }); app.on("second-instance", () => { From c8a7c22c137c65efd397af6183cea62a7c6f9815 Mon Sep 17 00:00:00 2001 From: aoirint Date: Wed, 9 Mar 2022 05:00:59 +0900 Subject: [PATCH 09/31] fix log unit --- src/background.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/background.ts b/src/background.ts index 97813d9443..fa125f3107 100644 --- a/src/background.ts +++ b/src/background.ts @@ -356,7 +356,7 @@ type EngineProcessContainer = { const engineProcessContainers: Record = {}; async function runEngineAll() { - log.info(`Starting ${engineInfos.length} engines...`); + log.info(`Starting ${engineInfos.length} engine/s...`); for (const engineInfo of engineInfos) { log.info(`ENGINE ${engineInfo.key}: Start launching`); From 977883198095638545f7d213fc39226d80d10256 Mon Sep 17 00:00:00 2001 From: aoirint Date: Wed, 9 Mar 2022 05:47:26 +0900 Subject: [PATCH 10/31] npm run fmt --- src/background.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/background.ts b/src/background.ts index fa125f3107..17da4a6159 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1167,7 +1167,9 @@ app.on("before-quit", (event) => { app.quit(); // attempt to quit app again }, onError: (engineKey, message) => { - console.error(`ENGINE ${engineKey}: Error during killing process: ${message}`); + console.error( + `ENGINE ${engineKey}: Error during killing process: ${message}` + ); }, }); }); From 2102710372984cba39838512d0e1dc8686140ca3 Mon Sep 17 00:00:00 2001 From: aoirint Date: Fri, 11 Mar 2022 01:13:46 +0900 Subject: [PATCH 11/31] assume to be killed --- src/background.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/background.ts b/src/background.ts index 17da4a6159..13ae334f70 100644 --- a/src/background.ts +++ b/src/background.ts @@ -488,7 +488,18 @@ function killEngineAll({ onAllKilled?.(); } }, - onError: (message) => onError?.(engineKey, message), + onError: (message) => { + onError?.(engineKey, message); + + // エディタを終了するため、エラーが起きてもエンジンプロセスをキルできたとみなして次のエンジンプロセスをキルする + numEngineProcessKilled++; + log.info( + `ENGINE ${engineKey}: process kill errored, but assume to have been killed` + ); + log.info( + `ENGINE ${numEngineProcessKilled} / ${numEngineProcess} processes killed` + ); + }, }); } } @@ -514,6 +525,7 @@ function killEngine({ if (engineProcess == undefined) { // nop if no process started (already killed or not started yet) log.info(`ENGINE ${engineKey}: Process not started`); + onKilled?.(); return; } @@ -543,6 +555,7 @@ function killEngine({ } } else { log.info(`ENGINE ${engineKey}: Process already closed`); + onKilled?.(); } } From c24c7e49baba9bb9e6d83f6bc9bdb44848a609ca Mon Sep 17 00:00:00 2001 From: aoirint Date: Fri, 11 Mar 2022 01:19:10 +0900 Subject: [PATCH 12/31] add memo comment --- src/background.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/background.ts b/src/background.ts index 13ae334f70..37d2a4aa91 100644 --- a/src/background.ts +++ b/src/background.ts @@ -515,6 +515,8 @@ function killEngine({ onKilled?: VoidFunction; onError?: (error: unknown) => void; }) { + // この関数では、結果を通知するためonKilledまたはonErrorを同期または非同期で必ず呼び出さなければならない + const engineProcessContainer = engineProcessContainers[engineKey]; if (!engineProcessContainer) { onError?.(`No such engineProcessContainer: key == ${engineKey}`); From 5c1cf7c1f9f3316e066069ff2576930bc6645116 Mon Sep 17 00:00:00 2001 From: aoirint Date: Fri, 11 Mar 2022 01:19:38 +0900 Subject: [PATCH 13/31] fix comment --- src/background.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/background.ts b/src/background.ts index 37d2a4aa91..bac772b6f7 100644 --- a/src/background.ts +++ b/src/background.ts @@ -515,7 +515,7 @@ function killEngine({ onKilled?: VoidFunction; onError?: (error: unknown) => void; }) { - // この関数では、結果を通知するためonKilledまたはonErrorを同期または非同期で必ず呼び出さなければならない + // この関数では、呼び出し元に結果を通知するためonKilledまたはonErrorを同期または非同期で必ず呼び出さなければならない const engineProcessContainer = engineProcessContainers[engineKey]; if (!engineProcessContainer) { From 086bd306f30ae2cae44f84f48568b31cd26bdb06 Mon Sep 17 00:00:00 2001 From: aoirint Date: Fri, 11 Mar 2022 01:24:53 +0900 Subject: [PATCH 14/31] call onAllKilled after final process kill errored --- src/background.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/background.ts b/src/background.ts index bac772b6f7..5f2b952bbf 100644 --- a/src/background.ts +++ b/src/background.ts @@ -499,6 +499,10 @@ function killEngineAll({ log.info( `ENGINE ${numEngineProcessKilled} / ${numEngineProcess} processes killed` ); + + if (numEngineProcessKilled === numEngineProcess) { + onAllKilled?.(); + } }, }); } From 9399dd1869f4500c21d77d8d1b29525ece16a1a6 Mon Sep 17 00:00:00 2001 From: aoirint Date: Fri, 11 Mar 2022 01:36:30 +0900 Subject: [PATCH 15/31] fix log --- src/background.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/background.ts b/src/background.ts index 5f2b952bbf..809b756f3e 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1181,12 +1181,12 @@ app.on("before-quit", (event) => { event.preventDefault(); }, onAllKilled: () => { - // executed asynchronously to catch all process closed event - log.info("All ENGINE killed. Quitting app"); + // executed asynchronously + log.info("All ENGINE process killed. Quitting app"); app.quit(); // attempt to quit app again }, onError: (engineKey, message) => { - console.error( + log.error( `ENGINE ${engineKey}: Error during killing process: ${message}` ); }, From db81c2baac80a8a44f2dc3a14adc0f5a1de71e07 Mon Sep 17 00:00:00 2001 From: aoirint Date: Fri, 11 Mar 2022 01:37:23 +0900 Subject: [PATCH 16/31] fix log --- src/background.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/background.ts b/src/background.ts index 809b756f3e..50af61ea0b 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1177,7 +1177,7 @@ app.on("before-quit", (event) => { killEngineAll({ onFirstKillStart: () => { // executed synchronously to cancel before-quit event - log.info("Interrupt app quit to kill ENGINE"); + log.info("Interrupt app quit to kill ENGINE processes"); event.preventDefault(); }, onAllKilled: () => { From d53c4e5f76ca51edba64ce4247d2b82ae360f44a Mon Sep 17 00:00:00 2001 From: aoirint Date: Fri, 11 Mar 2022 03:03:36 +0900 Subject: [PATCH 17/31] rewrite engine process kill logic to use promise --- src/background.ts | 234 ++++++++++++++++++++++++++-------------------- 1 file changed, 134 insertions(+), 100 deletions(-) diff --git a/src/background.ts b/src/background.ts index 50af61ea0b..2b40b69d82 100644 --- a/src/background.ts +++ b/src/background.ts @@ -455,114 +455,125 @@ async function runEngine(engineKey: string) { }); } -function killEngineAll({ - onFirstKillStart, - onAllKilled, - onError, -}: { - onFirstKillStart?: VoidFunction; - onAllKilled?: VoidFunction; - onError?: (engineKey: string, message: unknown) => void; -}) { - let anyKillStart = false; - - const numEngineProcess = Object.keys(engineProcessContainers).length; - let numEngineProcessKilled = 0; +type KillEngineProcessResult = { + engineKey: string; + succeeded: boolean; + error?: unknown; +}; - for (const [engineKey] of Object.entries(engineProcessContainers)) { - killEngine({ - engineKey, - onKillStart: () => { - if (!anyKillStart) { - anyKillStart = true; - onFirstKillStart?.(); - } - }, - onKilled: () => { - numEngineProcessKilled++; - log.info( - `ENGINE ${numEngineProcessKilled} / ${numEngineProcess} processes killed` - ); +type KillEngineProcessReturnVal = { + processKillStarted: boolean; + processKillPromise: Promise; +}; - if (numEngineProcessKilled === numEngineProcess) { - onAllKilled?.(); - } - }, - onError: (message) => { - onError?.(engineKey, message); +type KillEngineProcessAllReturnVal = { + anyProcessKillStarted: boolean; + processKillPromises: Promise[]; +}; - // エディタを終了するため、エラーが起きてもエンジンプロセスをキルできたとみなして次のエンジンプロセスをキルする - numEngineProcessKilled++; - log.info( - `ENGINE ${engineKey}: process kill errored, but assume to have been killed` - ); - log.info( - `ENGINE ${numEngineProcessKilled} / ${numEngineProcess} processes killed` - ); +function killEngineAll(): KillEngineProcessAllReturnVal { + let anyProcessKillStarted = false; - if (numEngineProcessKilled === numEngineProcess) { - onAllKilled?.(); - } - }, - }); + const processKillPromises: Promise[] = []; + + for (const [engineKey] of Object.entries(engineProcessContainers)) { + const { processKillStarted, processKillPromise } = killEngine(engineKey); + + anyProcessKillStarted = anyProcessKillStarted || processKillStarted; + processKillPromises.push(processKillPromise); } -} -function killEngine({ - engineKey, - onKillStart, - onKilled, - onError, -}: { - engineKey: string; - onKillStart?: VoidFunction; - onKilled?: VoidFunction; - onError?: (error: unknown) => void; -}) { - // この関数では、呼び出し元に結果を通知するためonKilledまたはonErrorを同期または非同期で必ず呼び出さなければならない + return { + anyProcessKillStarted, + processKillPromises, + }; +} +function killEngine(engineKey: string): KillEngineProcessReturnVal { const engineProcessContainer = engineProcessContainers[engineKey]; if (!engineProcessContainer) { - onError?.(`No such engineProcessContainer: key == ${engineKey}`); - return; + log.error(`No such engineProcessContainer: key == ${engineKey}`); + + return { + processKillStarted: false, + processKillPromise: new Promise((resolve) => + resolve({ + engineKey, + succeeded: true, + }) + ), + }; } const engineProcess = engineProcessContainer.engineProcess; if (engineProcess == undefined) { // nop if no process started (already killed or not started yet) log.info(`ENGINE ${engineKey}: Process not started`); - onKilled?.(); - return; + + return { + processKillStarted: false, + processKillPromise: new Promise((resolve) => + resolve({ + engineKey, + succeeded: true, + }) + ), + }; } - // considering the case that ENGINE process killed after checking process status - engineProcess.once("close", () => { - log.info(`ENGINE ${engineKey}: Process closed`); - onKilled?.(); - }); + const engineNotExited = engineProcess.exitCode === null; + const engineNotKilled = engineProcess.signalCode === null; log.info( `ENGINE ${engineKey}: last exit code: ${engineProcess.exitCode}, signal: ${engineProcess.signalCode}` ); - const engineNotExited = engineProcess.exitCode === null; - const engineNotKilled = engineProcess.signalCode === null; - - if (engineNotExited && engineNotKilled) { - log.info(`ENGINE ${engineKey}: Killing process (PID=${engineProcess.pid})`); - onKillStart?.(); - - engineProcessContainer.willQuitEngine = true; - try { - engineProcess.pid != undefined && treeKill(engineProcess.pid); - } catch (error: unknown) { - log.error(`ENGINE ${engineKey}: Error during killing process`); - onError?.(error); - } - } else { + const isAlive = engineNotExited && engineNotKilled; + if (!isAlive) { log.info(`ENGINE ${engineKey}: Process already closed`); - onKilled?.(); + + return { + processKillStarted: false, + processKillPromise: new Promise((resolve) => + resolve({ + engineKey, + succeeded: true, + }) + ), + }; } + + return { + processKillStarted: true, + processKillPromise: new Promise((resolve) => { + log.info( + `ENGINE ${engineKey}: Killing process (PID=${engineProcess.pid})` + ); + + // エラーダイアログを抑制 + engineProcessContainer.willQuitEngine = true; + + // プロセス終了時のイベントハンドラ + engineProcess.once("close", () => { + log.info(`ENGINE ${engineKey}: Process closed`); + resolve({ + engineKey, + succeeded: true, + }); + }); + + try { + engineProcess.pid != undefined && treeKill(engineProcess.pid); + } catch (error: unknown) { + log.error(`ENGINE ${engineKey}: Error during killing process`); + resolve({ + engineKey, + succeeded: false, + error, + }); + } + }), + }; } async function restartEngineAll() { @@ -1174,23 +1185,46 @@ app.on("before-quit", (event) => { } log.info("Checking ENGINE status before app quit"); - killEngineAll({ - onFirstKillStart: () => { - // executed synchronously to cancel before-quit event - log.info("Interrupt app quit to kill ENGINE processes"); - event.preventDefault(); - }, - onAllKilled: () => { - // executed asynchronously - log.info("All ENGINE process killed. Quitting app"); - app.quit(); // attempt to quit app again - }, - onError: (engineKey, message) => { - log.error( - `ENGINE ${engineKey}: Error during killing process: ${message}` + + const { anyProcessKillStarted, processKillPromises } = killEngineAll(); + + // すべてのエンジンプロセスが停止している + if (! anyProcessKillStarted) { + log.info("All ENGINE processes killed. Now quit app"); + return; + } + + // すべてのエンジンプロセスのキルを開始 + + // 同期的にbefore-quitイベントをキャンセル + log.info("Interrupt app quit to kill ENGINE processes"); + event.preventDefault(); + + const numEngineProcess = processKillPromises.length; + let numEngineProcessKilled = 0; + + // 非同期的にすべてのエンジンプロセスをキル + (async () => { + for (const processKillPromise of processKillPromises) { + const { engineKey, succeeded, error } = await processKillPromise; + + if (!succeeded) { + log.error( + `ENGINE ${engineKey}: Error during killing process: ${error}` + ); + // エディタを終了するため、エラーが起きてもエンジンプロセスをキルできたとみなす + } + + numEngineProcessKilled++; + log.info( + `ENGINE ${engineKey}: Process killed. ${numEngineProcessKilled} / ${numEngineProcess} processes killed` ); - }, - }); + } + + // アプリケーションの終了を再試行する + log.info("All ENGINE process kill operations done. Attempting to quit app again"); + app.quit(); // attempt to quit app again + })(); }); app.on("activate", () => { From 84f350dad4e2c599f07824d2b78e9aebba34bf1f Mon Sep 17 00:00:00 2001 From: aoirint Date: Fri, 11 Mar 2022 03:05:25 +0900 Subject: [PATCH 18/31] add comment --- src/background.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/background.ts b/src/background.ts index 2b40b69d82..10c82925c9 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1200,7 +1200,7 @@ app.on("before-quit", (event) => { log.info("Interrupt app quit to kill ENGINE processes"); event.preventDefault(); - const numEngineProcess = processKillPromises.length; + const numEngineProcess = processKillPromises.length; // assert == engineProcessContainers.length let numEngineProcessKilled = 0; // 非同期的にすべてのエンジンプロセスをキル From fb97d4a482c097260b411471db0bd82238a1ff5d Mon Sep 17 00:00:00 2001 From: aoirint Date: Fri, 11 Mar 2022 03:08:42 +0900 Subject: [PATCH 19/31] remove redundant comment --- src/background.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/background.ts b/src/background.ts index 10c82925c9..a77e3de3c0 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1223,7 +1223,7 @@ app.on("before-quit", (event) => { // アプリケーションの終了を再試行する log.info("All ENGINE process kill operations done. Attempting to quit app again"); - app.quit(); // attempt to quit app again + app.quit(); })(); }); From f421c36abd34c42bb7e1b970bad112167e2c4455 Mon Sep 17 00:00:00 2001 From: aoirint Date: Fri, 11 Mar 2022 04:05:49 +0900 Subject: [PATCH 20/31] npm run fmt --- src/background.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/background.ts b/src/background.ts index a77e3de3c0..050ec2042d 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1189,7 +1189,7 @@ app.on("before-quit", (event) => { const { anyProcessKillStarted, processKillPromises } = killEngineAll(); // すべてのエンジンプロセスが停止している - if (! anyProcessKillStarted) { + if (!anyProcessKillStarted) { log.info("All ENGINE processes killed. Now quit app"); return; } @@ -1222,7 +1222,9 @@ app.on("before-quit", (event) => { } // アプリケーションの終了を再試行する - log.info("All ENGINE process kill operations done. Attempting to quit app again"); + log.info( + "All ENGINE process kill operations done. Attempting to quit app again" + ); app.quit(); })(); }); From b681ec4551c8264171835979f6886e2b8ad0d616 Mon Sep 17 00:00:00 2001 From: aoirint Date: Sat, 12 Mar 2022 01:27:54 +0900 Subject: [PATCH 21/31] fix infinite loop when using external engine --- src/background.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/background.ts b/src/background.ts index 50af61ea0b..7931b48260 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1173,17 +1173,24 @@ app.on("before-quit", (event) => { return; } + let anyKillStart = false; + log.info("Checking ENGINE status before app quit"); killEngineAll({ onFirstKillStart: () => { + anyKillStart = true; + // executed synchronously to cancel before-quit event log.info("Interrupt app quit to kill ENGINE processes"); event.preventDefault(); }, onAllKilled: () => { // executed asynchronously - log.info("All ENGINE process killed. Quitting app"); - app.quit(); // attempt to quit app again + if (anyKillStart) { + log.info("All ENGINE process killed. Quitting app"); + app.quit(); // attempt to quit app again + } + // else: before-quit event is not cancelled }, onError: (engineKey, message) => { log.error( From acc0b020a48b50f040ce35edc8b72725c8e8ef1c Mon Sep 17 00:00:00 2001 From: aoirint Date: Mon, 27 Jun 2022 03:09:01 +0900 Subject: [PATCH 22/31] use promise.resolve/reject and undefined to return killing status --- src/background.ts | 141 +++++++++++++++------------------------------- 1 file changed, 45 insertions(+), 96 deletions(-) diff --git a/src/background.ts b/src/background.ts index 1114dc7bee..f5ff9160a6 100644 --- a/src/background.ts +++ b/src/background.ts @@ -498,70 +498,33 @@ async function runEngine(engineKey: string) { }); } -type KillEngineProcessResult = { - engineKey: string; - succeeded: boolean; - error?: unknown; -}; - -type KillEngineProcessReturnVal = { - processKillStarted: boolean; - processKillPromise: Promise; -}; - -type KillEngineProcessAllReturnVal = { - anyProcessKillStarted: boolean; - processKillPromises: Promise[]; -}; - -function killEngineAll(): KillEngineProcessAllReturnVal { - let anyProcessKillStarted = false; - - const processKillPromises: Promise[] = []; - - for (const [engineKey] of Object.entries(engineProcessContainers)) { - const { processKillStarted, processKillPromise } = killEngine(engineKey); - - anyProcessKillStarted = anyProcessKillStarted || processKillStarted; - processKillPromises.push(processKillPromise); - } - - return { - anyProcessKillStarted, - processKillPromises, - }; +// Promise | undefined +// Promise.resolve: エンジンプロセスのキルに成功した(非同期) +// Promise.reject: エンジンプロセスのキルに失敗した(非同期) +// undefined: エンジンプロセスのキルが開始されなかった=エンジンプロセスがすでに停止している(同期) +function killEngineAll(): Record | undefined> { + return Object.fromEntries( + Object.keys(engineProcessContainers).map((engineKey) => [ + engineKey, + killEngine(engineKey), + ]) + ); } -function killEngine(engineKey: string): KillEngineProcessReturnVal { +function killEngine(engineKey: string): Promise | undefined { const engineProcessContainer = engineProcessContainers[engineKey]; if (!engineProcessContainer) { log.error(`No such engineProcessContainer: key == ${engineKey}`); - return { - processKillStarted: false, - processKillPromise: new Promise((resolve) => - resolve({ - engineKey, - succeeded: true, - }) - ), - }; + return undefined; } const engineProcess = engineProcessContainer.engineProcess; - if (engineProcess == undefined) { + if (engineProcess === undefined) { // nop if no process started (already killed or not started yet) log.info(`ENGINE ${engineKey}: Process not started`); - return { - processKillStarted: false, - processKillPromise: new Promise((resolve) => - resolve({ - engineKey, - succeeded: true, - }) - ), - }; + return undefined; } const engineNotExited = engineProcess.exitCode === null; @@ -575,48 +538,28 @@ function killEngine(engineKey: string): KillEngineProcessReturnVal { if (!isAlive) { log.info(`ENGINE ${engineKey}: Process already closed`); - return { - processKillStarted: false, - processKillPromise: new Promise((resolve) => - resolve({ - engineKey, - succeeded: true, - }) - ), - }; + return new Promise((resolve) => resolve()); } - return { - processKillStarted: true, - processKillPromise: new Promise((resolve) => { - log.info( - `ENGINE ${engineKey}: Killing process (PID=${engineProcess.pid})` - ); + return new Promise((resolve, reject) => { + log.info(`ENGINE ${engineKey}: Killing process (PID=${engineProcess.pid})`); - // エラーダイアログを抑制 - engineProcessContainer.willQuitEngine = true; + // エラーダイアログを抑制 + engineProcessContainer.willQuitEngine = true; - // プロセス終了時のイベントハンドラ - engineProcess.once("close", () => { - log.info(`ENGINE ${engineKey}: Process closed`); - resolve({ - engineKey, - succeeded: true, - }); - }); + // プロセス終了時のイベントハンドラ + engineProcess.once("close", () => { + log.info(`ENGINE ${engineKey}: Process closed`); + resolve(); + }); - try { - engineProcess.pid != undefined && treeKill(engineProcess.pid); - } catch (error: unknown) { - log.error(`ENGINE ${engineKey}: Error during killing process`); - resolve({ - engineKey, - succeeded: false, - error, - }); - } - }), - }; + try { + engineProcess.pid != undefined && treeKill(engineProcess.pid); + } catch (error: unknown) { + log.error(`ENGINE ${engineKey}: Error during killing process`); + reject(error); + } + }); } async function restartEngineAll() { @@ -1272,10 +1215,17 @@ app.on("before-quit", (event) => { log.info("Checking ENGINE status before app quit"); - const { anyProcessKillStarted, processKillPromises } = killEngineAll(); + const allPromises = killEngineAll(); + const allPromiseEntries = Object.entries(allPromises); + const killingPromiseEntries = allPromiseEntries.filter( + ([, promise]) => promise !== undefined + ); + const killingPromises = Object.fromEntries(killingPromiseEntries); + + const numEngineProcess = Object.entries(killingPromises).length; // assert == engineProcessContainers.length // すべてのエンジンプロセスが停止している - if (!anyProcessKillStarted) { + if (numEngineProcess === 0) { log.info("All ENGINE processes killed. Now quit app"); return; } @@ -1286,15 +1236,14 @@ app.on("before-quit", (event) => { log.info("Interrupt app quit to kill ENGINE processes"); event.preventDefault(); - const numEngineProcess = processKillPromises.length; // assert == engineProcessContainers.length let numEngineProcessKilled = 0; // 非同期的にすべてのエンジンプロセスをキル (async () => { - for (const processKillPromise of processKillPromises) { - const { engineKey, succeeded, error } = await processKillPromise; - - if (!succeeded) { + for (const [engineKey, promise] of Object.entries(killingPromises)) { + try { + await promise; + } catch (error: unknown) { log.error( `ENGINE ${engineKey}: Error during killing process: ${error}` ); From eec0d0274e12b5b7f5cc61cf7dc1d6885897acb6 Mon Sep 17 00:00:00 2001 From: aoirint Date: Mon, 27 Jun 2022 03:21:09 +0900 Subject: [PATCH 23/31] return undefined if engine already killed --- src/background.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/background.ts b/src/background.ts index f5ff9160a6..639c98e101 100644 --- a/src/background.ts +++ b/src/background.ts @@ -538,7 +538,7 @@ function killEngine(engineKey: string): Promise | undefined { if (!isAlive) { log.info(`ENGINE ${engineKey}: Process already closed`); - return new Promise((resolve) => resolve()); + return undefined; } return new Promise((resolve, reject) => { From b8704d67ded081d4041a774145c6e52ab0964450 Mon Sep 17 00:00:00 2001 From: aoirint Date: Mon, 27 Jun 2022 03:22:53 +0900 Subject: [PATCH 24/31] shrink code --- src/background.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/background.ts b/src/background.ts index 639c98e101..1668760d91 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1216,11 +1216,9 @@ app.on("before-quit", (event) => { log.info("Checking ENGINE status before app quit"); const allPromises = killEngineAll(); - const allPromiseEntries = Object.entries(allPromises); - const killingPromiseEntries = allPromiseEntries.filter( - ([, promise]) => promise !== undefined + const killingPromises = Object.fromEntries( + Object.entries(allPromises).filter(([, promise]) => promise !== undefined) ); - const killingPromises = Object.fromEntries(killingPromiseEntries); const numEngineProcess = Object.entries(killingPromises).length; // assert == engineProcessContainers.length From c6dce693f6e33d4bbe00bf13f1c6d6835a3994fc Mon Sep 17 00:00:00 2001 From: aoirint Date: Mon, 27 Jun 2022 03:29:59 +0900 Subject: [PATCH 25/31] rename var --- src/background.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/background.ts b/src/background.ts index 1668760d91..4064bfd881 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1216,14 +1216,14 @@ app.on("before-quit", (event) => { log.info("Checking ENGINE status before app quit"); const allPromises = killEngineAll(); - const killingPromises = Object.fromEntries( + const killingProcessPromises = Object.fromEntries( Object.entries(allPromises).filter(([, promise]) => promise !== undefined) ); - const numEngineProcess = Object.entries(killingPromises).length; // assert == engineProcessContainers.length + const numLivingEngineProcess = Object.entries(killingProcessPromises).length; // すべてのエンジンプロセスが停止している - if (numEngineProcess === 0) { + if (numLivingEngineProcess === 0) { log.info("All ENGINE processes killed. Now quit app"); return; } @@ -1238,7 +1238,7 @@ app.on("before-quit", (event) => { // 非同期的にすべてのエンジンプロセスをキル (async () => { - for (const [engineKey, promise] of Object.entries(killingPromises)) { + for (const [engineKey, promise] of Object.entries(killingProcessPromises)) { try { await promise; } catch (error: unknown) { @@ -1250,7 +1250,7 @@ app.on("before-quit", (event) => { numEngineProcessKilled++; log.info( - `ENGINE ${engineKey}: Process killed. ${numEngineProcessKilled} / ${numEngineProcess} processes killed` + `ENGINE ${engineKey}: Process killed. ${numEngineProcessKilled} / ${numLivingEngineProcess} processes killed` ); } From 1bdf974efe24e9c7687dc2fc23196d38dccb1429 Mon Sep 17 00:00:00 2001 From: aoirint Date: Mon, 27 Jun 2022 04:11:25 +0900 Subject: [PATCH 26/31] killEngineAll now returns Record> to avoid type inferencing issue of array.filter --- src/background.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/background.ts b/src/background.ts index 4064bfd881..b24faa4bcc 100644 --- a/src/background.ts +++ b/src/background.ts @@ -498,19 +498,23 @@ async function runEngine(engineKey: string) { }); } +function killEngineAll(): Record> { + const killingProcessPromises: Record> = {}; + + for (const engineKey of Object.keys(engineProcessContainers)) { + const promise = killEngine(engineKey); + if (promise === undefined) continue; + + killingProcessPromises[engineKey] = promise; + } + + return killingProcessPromises; +} + // Promise | undefined // Promise.resolve: エンジンプロセスのキルに成功した(非同期) // Promise.reject: エンジンプロセスのキルに失敗した(非同期) // undefined: エンジンプロセスのキルが開始されなかった=エンジンプロセスがすでに停止している(同期) -function killEngineAll(): Record | undefined> { - return Object.fromEntries( - Object.keys(engineProcessContainers).map((engineKey) => [ - engineKey, - killEngine(engineKey), - ]) - ); -} - function killEngine(engineKey: string): Promise | undefined { const engineProcessContainer = engineProcessContainers[engineKey]; if (!engineProcessContainer) { @@ -1215,11 +1219,7 @@ app.on("before-quit", (event) => { log.info("Checking ENGINE status before app quit"); - const allPromises = killEngineAll(); - const killingProcessPromises = Object.fromEntries( - Object.entries(allPromises).filter(([, promise]) => promise !== undefined) - ); - + const killingProcessPromises = killEngineAll(); const numLivingEngineProcess = Object.entries(killingProcessPromises).length; // すべてのエンジンプロセスが停止している From 5ecc203dddb760b8fbe5ae3163f05f703daaf401 Mon Sep 17 00:00:00 2001 From: aoirint Date: Mon, 27 Jun 2022 04:39:30 +0900 Subject: [PATCH 27/31] asynchronized each engine process kill --- src/background.ts | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/background.ts b/src/background.ts index b24faa4bcc..5c83d6a7d5 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1238,22 +1238,37 @@ app.on("before-quit", (event) => { // 非同期的にすべてのエンジンプロセスをキル (async () => { - for (const [engineKey, promise] of Object.entries(killingProcessPromises)) { - try { - await promise; - } catch (error: unknown) { - log.error( - `ENGINE ${engineKey}: Error during killing process: ${error}` - ); - // エディタを終了するため、エラーが起きてもエンジンプロセスをキルできたとみなす - } + const waitingKilledPromises: Array> = []; - numEngineProcessKilled++; - log.info( - `ENGINE ${engineKey}: Process killed. ${numEngineProcessKilled} / ${numLivingEngineProcess} processes killed` + for (const [engineKey, promise] of Object.entries(killingProcessPromises)) { + // 各エンジンを非同期にキル + waitingKilledPromises.push( + new Promise((resolve) => { + (async () => { + try { + await promise; + } catch (error: unknown) { + // TODO: 各エンジンプロセスキルの失敗をUIに通知する + log.error( + `ENGINE ${engineKey}: Error during killing process: ${error}` + ); + // エディタを終了するため、エラーが起きてもエンジンプロセスをキルできたとみなす + } + + numEngineProcessKilled++; + log.info( + `ENGINE ${engineKey}: Process killed. ${numEngineProcessKilled} / ${numLivingEngineProcess} processes killed` + ); + + resolve(); + })(); + }) ); } + // すべてのエンジンプロセスキル処理が完了するまで待機 + await Promise.all(waitingKilledPromises); + // アプリケーションの終了を再試行する log.info( "All ENGINE process kill operations done. Attempting to quit app again" From 5ad4780bd889d786e00a9dbb35cc7a4657334eae Mon Sep 17 00:00:00 2001 From: aoirint Date: Mon, 27 Jun 2022 04:40:27 +0900 Subject: [PATCH 28/31] add comment --- src/background.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/background.ts b/src/background.ts index 5c83d6a7d5..c1f4e7b723 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1255,6 +1255,7 @@ app.on("before-quit", (event) => { // エディタを終了するため、エラーが起きてもエンジンプロセスをキルできたとみなす } + // TODO: 各エンジンプロセスキルの成功をUIに通知する numEngineProcessKilled++; log.info( `ENGINE ${engineKey}: Process killed. ${numEngineProcessKilled} / ${numLivingEngineProcess} processes killed` From 3fceda1bd359253f8fc7ed2e934c97bf3495c1a7 Mon Sep 17 00:00:00 2001 From: aoirint Date: Mon, 27 Jun 2022 04:45:34 +0900 Subject: [PATCH 29/31] strip redundant new promise --- src/background.ts | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/background.ts b/src/background.ts index c1f4e7b723..ebc1c4168e 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1243,27 +1243,23 @@ app.on("before-quit", (event) => { for (const [engineKey, promise] of Object.entries(killingProcessPromises)) { // 各エンジンを非同期にキル waitingKilledPromises.push( - new Promise((resolve) => { - (async () => { - try { - await promise; - } catch (error: unknown) { - // TODO: 各エンジンプロセスキルの失敗をUIに通知する - log.error( - `ENGINE ${engineKey}: Error during killing process: ${error}` - ); - // エディタを終了するため、エラーが起きてもエンジンプロセスをキルできたとみなす - } - - // TODO: 各エンジンプロセスキルの成功をUIに通知する - numEngineProcessKilled++; - log.info( - `ENGINE ${engineKey}: Process killed. ${numEngineProcessKilled} / ${numLivingEngineProcess} processes killed` + (async () => { + try { + await promise; + } catch (error: unknown) { + // TODO: 各エンジンプロセスキルの失敗をUIに通知する + log.error( + `ENGINE ${engineKey}: Error during killing process: ${error}` ); - - resolve(); - })(); - }) + // エディタを終了するため、エラーが起きてもエンジンプロセスをキルできたとみなす + } + + // TODO: 各エンジンプロセスキルの成功をUIに通知する + numEngineProcessKilled++; + log.info( + `ENGINE ${engineKey}: Process killed. ${numEngineProcessKilled} / ${numLivingEngineProcess} processes killed` + ); + })() ); } From 0dd389309e53e37a5b654ee56d1948394981e996 Mon Sep 17 00:00:00 2001 From: Hiroshiba Date: Sat, 23 Jul 2022 22:09:01 +0900 Subject: [PATCH 30/31] Update src/background.ts Co-authored-by: Segu <51497552+Segu-g@users.noreply.github.com> --- src/background.ts | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/background.ts b/src/background.ts index ebc1c4168e..8f3477a856 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1238,29 +1238,24 @@ app.on("before-quit", (event) => { // 非同期的にすべてのエンジンプロセスをキル (async () => { - const waitingKilledPromises: Array> = []; - - for (const [engineKey, promise] of Object.entries(killingProcessPromises)) { - // 各エンジンを非同期にキル - waitingKilledPromises.push( - (async () => { - try { - await promise; - } catch (error: unknown) { - // TODO: 各エンジンプロセスキルの失敗をUIに通知する - log.error( - `ENGINE ${engineKey}: Error during killing process: ${error}` - ); - // エディタを終了するため、エラーが起きてもエンジンプロセスをキルできたとみなす - } - - // TODO: 各エンジンプロセスキルの成功をUIに通知する + const waitingKilledPromises: Array> = Object.entries( + killingProcessPromises + ).map(([engineKey, promise]) => { + return promise + .catch((error) => { + // TODO: 各エンジンプロセスキルの失敗をUIに通知する + log.error( + `ENGINE ${engineKey}: Error during killing process: ${error}` + ); + // エディタを終了するため、エラーが起きてもエンジンプロセスをキルできたとみなす + }) + .finally(() => { numEngineProcessKilled++; log.info( `ENGINE ${engineKey}: Process killed. ${numEngineProcessKilled} / ${numLivingEngineProcess} processes killed` ); - })() - ); + }); + }); } // すべてのエンジンプロセスキル処理が完了するまで待機 From 2280ae993c7e4ccb31fdb3d720b71b667b27ae2a Mon Sep 17 00:00:00 2001 From: Hiroshiba Date: Sat, 23 Jul 2022 22:17:28 +0900 Subject: [PATCH 31/31] Update src/background.ts --- src/background.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/background.ts b/src/background.ts index 8f3477a856..772ac6f97d 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1256,7 +1256,6 @@ app.on("before-quit", (event) => { ); }); }); - } // すべてのエンジンプロセスキル処理が完了するまで待機 await Promise.all(waitingKilledPromises);