[Service] Refactor builtin-service with node worker 53/244053/12
authorDongHyun Song <dh81.song@samsung.com>
Mon, 14 Sep 2020 10:02:37 +0000 (19:02 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Thu, 24 Sep 2020 04:07:06 +0000 (13:07 +0900)
This applies builtin-service to execute on node worker same as
service application with refactoring worker creation/termination.

Change-Id: I105a4e3f91409deb9805ac2ca08e4e62a162c2fa
Signed-off-by: DongHyun Song <dh81.song@samsung.com>
wrt_app/common/service_manager.ts
wrt_app/common/service_runner.ts
wrt_app/service/builtins/builtin_handler.ts [deleted file]
wrt_app/service/builtins/wasm_builder.ts
wrt_app/service/main.ts

index ea6e1df..b9daf4e 100644 (file)
@@ -5,13 +5,48 @@ interface WorkerMap {
   [id: string]: any;
 }
 let workers: WorkerMap = {};
-let serviceType: string = wrt.getServiceModel();;
+let serviceType: string = wrt.getServiceModel();
 let runner: any;
 
+function isGlobalService() {
+  return serviceType === 'DAEMON';
+}
+
 function isStandalone() {
   return serviceType === 'STANDALONE';
 }
 
+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, {
+    workerData: {
+      id,
+      filename
+    }
+  });
+}
+
+function terminateWorker(id: string, delay: number) {
+  if (!workers[id]) {
+    console.log(`This worker is already terminated. ${id}`);
+    return;
+  }
+  workers[id].postMessage('stopService');
+  let terminate = () => {
+    workers[id].terminate();
+    delete workers[id];
+    let runningServices = Object.keys(workers).length;
+    console.log('Running services : ' + runningServices);
+    if (runningServices === 0 && isGlobalService()) {
+      process.exit();
+    }
+  }
+  setTimeout(() => terminate(), delay);
+}
+
 export function startService(id: string, filename: string) {
   console.log(`startService - ${id}`);
   if (isStandalone()) {
@@ -19,8 +54,8 @@ export function startService(id: string, filename: string) {
     runner.start(id, filename);
   } else {
     if (isMainThread) {
-      let startService = __dirname + '/service_runner.js';
-      workers[id] = new Worker(startService, { workerData: { id: id, filename: filename } });
+      let startService = `${__dirname}/service_runner.js`;
+      createWorker(id, startService, filename);
     }
   }
 }
@@ -35,15 +70,22 @@ export function stopService(id: string) {
     runner.stop(id);
     setTimeout(() => process.exit(), 500);
   } else {
-    workers[id].postMessage('stopService');
-    setTimeout(() => {
-      workers[id].terminate();
-      delete workers[id];
-      let runningServices = Object.keys(workers).length;
-      console.log('Running services : ' + runningServices);
-      if (runningServices === 0 && serviceType !== 'UI') {
-        process.exit();
-      }
-    }, 500);
+    terminateWorker(id, 500);
   }
 }
+
+export function handleBuiltinService(serviceId: string, serviceName: string) {
+  if (!serviceName) {
+    return;
+  }
+  let need_stop = (serviceName.substr(0, 5) === 'stop_');
+  if (need_stop) {
+    terminateWorker(serviceId, 0);
+  } else {
+    if (isMainThread) {
+      console.log(`Builtin service is ${serviceName}`);
+      let startService = `${__dirname}/../service/builtins/${serviceName}.js`;
+      createWorker(serviceId, startService, '');
+    }
+  }
+}
\ No newline at end of file
index 8e1c032..7ea5bca 100644 (file)
@@ -10,7 +10,7 @@ function isServiceApplication() {
   return serviceType !== 'UI';
 }
 
-function isGloablService() {
+function isGlobalService() {
   return serviceType === 'DAEMON';
 }
 
@@ -42,7 +42,7 @@ export function start(id: string, filename: string) {
   });
 
   console.log('serviceType : '+serviceType)
-  new DeviceAPIRouter(id, isGloablService());
+  new DeviceAPIRouter(id, isGlobalService());
 
   if (isServiceApplication()) {
     registerExtensionResolver(id);
@@ -61,7 +61,7 @@ export function start(id: string, filename: string) {
     if (app.onRequest !== undefined) {
       app.onRequest();
     }
-    if (isGloablService()) {
+    if (isGlobalService()) {
       wrt.finishStartingService(id);
     }
   } catch (e) {
diff --git a/wrt_app/service/builtins/builtin_handler.ts b/wrt_app/service/builtins/builtin_handler.ts
deleted file mode 100644 (file)
index de9c8a8..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-import * as vm from 'vm';
-
-let sandbox: vm.Context = {
-  console: console,
-  require: require,
-  services: {}
-};
-
-export function handleService(service_name: string, service_id: string) {
-  if (!service_name)
-    return;
-
-  let need_stop = (service_name.substr(0, 5) === 'stop_');
-  if (need_stop) {
-    service_name = service_name.substr(5);
-  }
-  let builtin_service = `./${service_name}.js`;
-  console.log(`Builtin service is ${builtin_service}`);
-  let options = {
-    filename: service_id,
-  };
-  if (need_stop) {
-    let code = `services['${service_id}'].stop('${service_id}')`;
-    vm.runInContext(code, sandbox);
-  } else {
-    let code = `services['${service_id}'] = require('${builtin_service}');`;
-    code += `services['${service_id}'].run('${service_id}')`;
-    vm.runInNewContext(code, sandbox, options);
-  }
-}
\ No newline at end of file
index b61ed86..aa8a00f 100644 (file)
@@ -1,5 +1,7 @@
-import * as fs from 'fs';
+import '../../common/init';
+import { isMainThread, workerData } from 'worker_threads';
 import { wrt } from '../../browser/wrt';
+import * as fs from 'fs';
 
 async function compileWasmForCaching(file_path: string) {
   console.log(`Requesting WASM compilation for building a cache, file_path:(${file_path})`);
@@ -25,7 +27,6 @@ export function run(app_id: string) {
   });
 }
 
-export function stop(app_id: string) {
-  // TODO, stop caching will be implemented here
-  console.log(`wasm_builder.js suspended, app_id:(${app_id})`);
-}
\ No newline at end of file
+if (!isMainThread) {
+  run(workerData.id);
+}
index 6fa2f41..b0c371a 100755 (executable)
@@ -19,7 +19,6 @@
 import '../common/init';
 import { wrt } from '../browser/wrt';
 import * as ServiceManager from '../common/service_manager';
-import * as BuiltinService from './builtins/builtin_handler';
 
 wrt.on('start-service', (event: any, internal_id: string) => {
   console.log('start service app : ' + internal_id);
@@ -34,8 +33,8 @@ wrt.on('stop-service', (event: any, internal_id: string) => {
 });
 
 wrt.on('builtin-service', (event: any, internal_id: string, service_name: string) => {
-  console.log(`service_name: ${service_name}`);
-  BuiltinService.handleService(service_name, internal_id);
+  console.log(`id: ${internal_id}, service_name: ${service_name}`);
+  ServiceManager.handleBuiltinService(internal_id, service_name);
 });
 
 wrt.on('quit', (event: any) => {