From b6a74f93575992e34c406ae477fb00ba95f012f0 Mon Sep 17 00:00:00 2001 From: DongHyun Song Date: Mon, 21 Sep 2020 17:13:22 +0900 Subject: [PATCH] [Service][TV] Refactor for wasm-caching shutdown 1) Calls delayShutdown() every wasm cache creation. - If built-in service is implemented with async functions, after run() is done and there is no running services, node main will be terminated by itself 1 minute later. delayShutdown() can reset the timeout to 1 min. 2) After finish wasm-caching, notify parent to terminate node worker - When wasm-caching is finished, node worker is able to terminate by stop messaging Change-Id: I23b3587d3f3746a451b517c7cd7a70c5f9593302 Signed-off-by: DongHyun Song --- wrt_app/common/service_manager.ts | 13 ++++++------- wrt_app/service/builtins/wasm_builder.ts | 7 +++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/wrt_app/common/service_manager.ts b/wrt_app/common/service_manager.ts index b9daf4e..1ce2ea5 100644 --- a/wrt_app/common/service_manager.ts +++ b/wrt_app/common/service_manager.ts @@ -17,16 +17,12 @@ function isStandalone() { } function createWorker(id: string, startService: string, filename: string) { - if (workers[id]) { - console.log(`This worker is already running. ${id}`); - return; - } - workers[id] = new Worker(startService, { + return workers[id] ?? (workers[id] = new Worker(startService, { workerData: { id, filename } - }); + })); } function terminateWorker(id: string, delay: number) { @@ -85,7 +81,10 @@ export function handleBuiltinService(serviceId: string, serviceName: string) { if (isMainThread) { console.log(`Builtin service is ${serviceName}`); let startService = `${__dirname}/../service/builtins/${serviceName}.js`; - createWorker(serviceId, startService, ''); + let worker = createWorker(serviceId, startService, ''); + worker.on('stop', () => { + terminateWorker(serviceId, 0); + }); } } } \ No newline at end of file diff --git a/wrt_app/service/builtins/wasm_builder.ts b/wrt_app/service/builtins/wasm_builder.ts index aa8a00f..7ec30a3 100644 --- a/wrt_app/service/builtins/wasm_builder.ts +++ b/wrt_app/service/builtins/wasm_builder.ts @@ -1,5 +1,5 @@ import '../../common/init'; -import { isMainThread, workerData } from 'worker_threads'; +import { isMainThread, parentPort, workerData } from 'worker_threads'; import { wrt } from '../../browser/wrt'; import * as fs from 'fs'; @@ -19,12 +19,15 @@ export function run(app_id: string) { let tv = wrt.tv as NativeWRTjs.TVExtension; tv.setWasmFlags(); tv.setDiskCache(app_id); - tv.delayShutdown(); let files = tv.getWasmFiles(app_id); console.log(files); files.forEach((file_path: string) => { + tv.delayShutdown(); compileWasmForCaching(file_path); }); + if (parentPort) { + parentPort.postMessage('stop'); + } } if (!isMainThread) { -- 2.7.4