[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()) {
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);
}
}
}
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
return serviceType !== 'UI';
}
-function isGloablService() {
+function isGlobalService() {
return serviceType === 'DAEMON';
}
});
console.log('serviceType : '+serviceType)
- new DeviceAPIRouter(id, isGloablService());
+ new DeviceAPIRouter(id, isGlobalService());
if (isServiceApplication()) {
registerExtensionResolver(id);
if (app.onRequest !== undefined) {
app.onRequest();
}
- if (isGloablService()) {
+ if (isGlobalService()) {
wrt.finishStartingService(id);
}
} catch (e) {
+++ /dev/null
-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
-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})`);
});
}
-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);
+}
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);
});
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) => {