[Service][TV] Refactor for wasm-caching shutdown 22/244522/7
authorDongHyun Song <dh81.song@samsung.com>
Mon, 21 Sep 2020 08:13:22 +0000 (17:13 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Thu, 24 Sep 2020 04:16:05 +0000 (04:16 +0000)
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 <dh81.song@samsung.com>
wrt_app/common/service_manager.ts
wrt_app/service/builtins/wasm_builder.ts

index b9daf4e..1ce2ea5 100644 (file)
@@ -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
index aa8a00f..7ec30a3 100644 (file)
@@ -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) {