[id: string]: any;
}
let workers: WorkerMap = {};
-let runner: any;
-global.serviceType = wrt.getServiceModel();
+Object.defineProperty(global, 'serviceType', {
+ value: wrt.getServiceModel(),
+ writable: false
+});
-function isStandalone() {
- return global.serviceType === 'STANDALONE';
-}
-
-function isGlobalService() {
- return global.serviceType === 'DAEMON';
+function isServiceApplication() {
+ return global['serviceType'] !== 'UI';
}
function createWorker(id: string, startService: string, filename: string) {
- return workers[id] ?? (workers[id] = new Worker(startService, {
+ if (workers[id])
+ return;
+
+ workers[id] = new Worker(startService, {
workerData: {
id,
filename
}
- }));
+ });
+ workers[id].on('exit', (code: number) => {
+ delete workers[id];
+ let runningServices = Object.keys(workers).length;
+ console.log(`exit code(${code}), remain services(${runningServices})`);
+ if (runningServices === 0 && isServiceApplication()) {
+ setTimeout(() => process.exit(), 500);
+ }
+ });
}
function terminateWorker(id: string, delay: number) {
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);
+ console.log(`${id} will shutdown after ${delay}ms`);
+ workers[id].postMessage({ type: 'stop', delay });
}
export function startService(id: string, filename: string) {
console.log(`startService - ${id}`);
- if (isStandalone()) {
- runner = require('../common/service_runner');
- runner.start(id, filename);
- } else {
- if (isMainThread) {
- let startService = `${__dirname}/service_runner.js`;
- createWorker(id, startService, filename);
- }
- }
+ let startService = `${__dirname}/service_runner.js`;
+ createWorker(id, startService, filename);
}
export function stopService(id: string) {
console.log(`stopService - ${id}`);
- if (isStandalone()) {
- if (!runner) {
- console.log('runner instance is null in standalone mode');
- return;
- }
- runner.stop(id);
- setTimeout(() => process.exit(), 500);
- } else {
- terminateWorker(id, 500);
- }
+ terminateWorker(id, 500);
}
export function handleBuiltinService(serviceId: string, serviceName: string) {
}
let need_stop = (serviceName.substr(0, 5) === 'stop_');
if (need_stop) {
- terminateWorker(serviceId, 0);
+ workers[serviceId].terminate();
} else {
- if (isMainThread) {
- console.log(`Builtin service is ${serviceName}`);
- let startService = `${__dirname}/../service/builtins/${serviceName}.js`;
- let worker = createWorker(serviceId, startService, '');
- worker.on('stop', () => {
- terminateWorker(serviceId, 0);
- });
- }
+ console.log(`Builtin service is ${serviceName}`);
+ let startService = `${__dirname}/../service/builtins/${serviceName}.js`;
+ createWorker(serviceId, startService, '');
}
}
\ No newline at end of file
import { isMainThread, parentPort, workerData } from 'worker_threads';
import { wrt } from '../browser/wrt';
-global.serviceType = wrt.getServiceModel();
+Object.defineProperty(global, 'serviceType', {
+ value: wrt.getServiceModel(),
+ writable: false
+});
function isServiceApplication() {
- return global.serviceType !== 'UI';
+ return global['serviceType'] !== 'UI';
}
function isGlobalService() {
- return global.serviceType === 'DAEMON';
+ return global['serviceType'] === 'DAEMON';
}
function registerExtensionResolver(id: string) {
}
}
+function requestStopService(id: string) {
+ setTimeout(() => wrt.stopService(id), 500);
+}
+
let app: any = null;
export function start(id: string, filename: string) {
XWalkExtension.initialize();
XWalkExtension.setRuntimeMessageHandler((type, data) => {
if (type === 'tizen://exit') {
console.log(`${id} will be closed by ${type}`);
- setTimeout(() => wrt.stopService(id), 500);
+ requestStopService(id);
}
});
- console.log(`serviceType : ${global.serviceType}`)
+ console.log(`serviceType : ${global['serviceType']}`)
new DeviceAPIRouter(id, isGlobalService());
if (isServiceApplication()) {
}
} catch (e) {
console.log(`exception on start: ${e}`);
- setTimeout(() => wrt.stopService(id), 500);
+ requestStopService(id);
}
}
if (!parentPort)
return;
- parentPort.on('message', (msg) => {
- console.log(`message received : ${msg}`);
- if (msg === 'stopService') {
+ parentPort.on('message', (message) => {
+ console.log(`Received message type : ${message.type}`);
+ if (message.type === 'stop') {
stop(id);
- XWalkExtension.cleanup();
+ setTimeout(() => {
+ XWalkExtension.cleanup();
+ process.exit()
+ }, message.delay);
}
});
}
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})`);
+function compileWasmForCaching(files: string[]) {
try {
- let source = fs.readFileSync(file_path);
- let file = new Uint8Array(source);
- await WebAssembly.compile(file);
+ files.forEach(async file_path => {
+ console.log(`Requesting WASM compilation for building a cache, file_path:(${file_path})`);
+ let source = fs.readFileSync(file_path);
+ let file = new Uint8Array(source);
+ await WebAssembly.compile(file);
+ });
} catch (e) {
console.error(`An error occurred while compiling a wasm module. error:(${e})`);
}
tv.setDiskCache(app_id);
let files = tv.getWasmFiles(app_id);
console.log(files);
- files.forEach((file_path: string) => {
- tv.delayShutdown();
- compileWasmForCaching(file_path);
- });
- if (parentPort) {
- parentPort.postMessage('stop');
- }
+ tv.delayShutdown();
+ compileWasmForCaching(files);
+ process.exit();
}
if (!isMainThread) {
import * as ServiceManager from '../common/service_manager';
wrt.on('start-service', (event: any, internal_id: string) => {
- console.log('start service app : ' + internal_id);
+ console.log(`start service app : ${internal_id}`);
ServiceManager.startService(internal_id, '');
});
wrt.on('stop-service', (event: any, internal_id: string) => {
+ console.log(`stop service app : ${internal_id}`);
ServiceManager.stopService(internal_id);
- if (wrt.getServiceModel() === 'STANDALONE') {
- setTimeout(() => {process.exit()}, 10);
- }
});
wrt.on('builtin-service', (event: any, internal_id: string, service_name: string) => {