[Service] StopService when appid is empty
[platform/framework/web/wrtjs.git] / wrt_app / service / service_runner.ts
index b4b3fe1..1955c53 100644 (file)
@@ -69,7 +69,12 @@ let checkLauncherAlive = (id: string) => {
 export function start(id: string, filename: string) {
   let ids = id.split(':');
   let serviceId = ids[0];
-  let packageId = wrt.getPackageId(serviceId);
+  let packageId = wrt.getPackageId(id);
+  if (!packageId) {
+    console.debug(`${id}'s pkgid is empty, so stop service`);
+    requestStopService(id);
+    return;
+  }
   wrt.setServiceAppId(id);
   wrt.security?.dropThreadPrivilege(packageId, serviceId);
 
@@ -106,12 +111,13 @@ export function start(id: string, filename: string) {
     if (app.onRequest !== undefined) {
       app.onRequest();
     }
-    if (isGlobalService()) {
-      wrt.finishStartingService(id);
-    }
   } catch (e) {
     console.debug(`exception on start: ${e}`);
     requestStopService(id);
+  } finally {
+    if (isGlobalService()) {
+      wrt.finishStartingService(id);
+    }
   }
 }
 
@@ -131,20 +137,21 @@ export function stop(id: string) {
 
 function run() {
   let id = workerData.id;
+  if (!id) {
+    console.debug('workerData.id is empty!');
+    process.exit();
+  }
+
   Object.defineProperty(global, 'internalId', {
     value: id,
     writable: false
   });
 
-  // FIXME: this should be 'wrt.tv?.serviceMount(id)' after Tizen 6.5 release
-  (wrt.tv as any)?.serviceMount(id);
-
+  wrt.tv?.serviceMount(id);
   let filename = workerData.filename;
   start(id, filename);
 
-  if (!parentPort)
-    return;
-  parentPort.on('message', (message) => {
+  parentPort?.on('message', (message) => {
     console.debug(`Received message type : ${message.type}`);
     if (message.type === 'wake') {
       app?.onRequest();
@@ -153,7 +160,8 @@ function run() {
       setTimeout(() => {
         XWalkExtension.cleanup();
         parentPort?.postMessage("will-terminate");
-        (wrt.tv as any)?.serviceUmount(id);
+        parentPort?.close();
+        wrt.tv?.serviceUmount(id);
       }, message.delay);
     }
   });