Commit d5fdbfd1 authored by Nilanjan Daw's avatar Nilanjan Daw

Isolate streamlined wiith bugs

Isolate streamline working but unable to get startup signal, initial requests failing
parent 753d6993
{"id":"10.130.150.246","master_node":"10.129.6.5"} {"id":"192.168.31.51","master_node":"10.129.6.5"}
\ No newline at end of file \ No newline at end of file
'use strict'; 'use strict';
const isolateBackend = require('./isolate') // const isolateBackend = require('./isolate')
const fs = require('fs') const fs = require('fs')
const { spawn } = require('child_process'); const { spawn } = require('child_process');
const { Worker, isMainThread, workerData } = require('worker_threads');
const registry_url = "10.129.6.5:5000/" const registry_url = "10.129.6.5:5000/"
const events = require('events'); const events = require('events');
const workerEvent = new events.EventEmitter(); const workerEvent = new events.EventEmitter();
function runIsolate(filename) { function runIsolate(local_repository, functionHash, port) {
let filename = local_repository + functionHash + ".js"
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let timeStart = Date.now()
let { isolate, context } = isolateBackend.createIsolate();
fs.readFile(filename, 'utf-8', (err, data) => { const worker = new Worker(filename);
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
console.log("worker exited");
workerEvent.emit('end', port, "isolate");
})
workerEvent.emit('start', functionHash, port, "isolate")
resolve()
// let timeStart = Date.now()
// let { isolate, context } = isolateBackend.createIsolate();
// fs.readFile(filename, 'utf-8', (err, data) => {
if (err) // if (err)
reject(err); // reject(err);
context.evalClosure(data).then(result => { // context.evalClosure(data).then(result => {
let timeDifference = Math.ceil((Date.now() - timeStart)) // let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("isolate time taken: ", timeDifference); // console.log("isolate time taken: ", timeDifference);
resolve(result.result); // resolve(result.result);
}).catch(err => { reject(err) }) // }).catch(err => { reject(err) })
}); // });
}); });
} }
......
...@@ -42,7 +42,7 @@ libSupport.makeTopic(node_id).then(() => { ...@@ -42,7 +42,7 @@ libSupport.makeTopic(node_id).then(() => {
let port = message.port let port = message.port
if (message.type === "execute") { if (message.type === "execute") {
console.log("function_id", function_id); console.log("function_id", function_id);
libSupport.download(host_url + "/repository/" + functionHash + ".js", local_repository + functionHash).then(() => { libSupport.download(host_url + "/repository/" + functionHash + ".js", local_repository + functionHash + ".js").then(() => {
startWorker(local_repository, functionHash, function_id, producer, runtime, port) startWorker(local_repository, functionHash, function_id, producer, runtime, port)
}) })
...@@ -59,16 +59,17 @@ function startWorker(local_repository, functionHash,function_id, producer, runti ...@@ -59,16 +59,17 @@ function startWorker(local_repository, functionHash,function_id, producer, runti
usedPort.set(port, functionHash) usedPort.set(port, functionHash)
fs.writeFileSync('./local_repository/config.json', JSON.stringify({port})); fs.writeFileSync('./local_repository/config.json', JSON.stringify({port}));
if (runtime === "isolate") if (runtime === "isolate")
execute.runIsolate(local_repository + functionHash).then(result => { execute.runIsolate(local_repository, functionHash, port)
producer.send([{ // .then(result => {
topic: "response", // producer.send([{
messages: JSON.stringify({ // topic: "response",
status: "success", // messages: JSON.stringify({
result, // status: "success",
function_id // result,
}) // function_id
}], () => { }) // })
}) // }], () => { })
// })
else if (runtime === "process") else if (runtime === "process")
execute.runProcess(local_repository, functionHash, port) execute.runProcess(local_repository, functionHash, port)
// .then(result => { // .then(result => {
......
const { Worker, isMainThread, workerData } = require('worker_threads'); const { Worker, isMainThread, workerData } = require('worker_threads');
function runService() { function runService() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (isMainThread) { if (isMainThread) {
const worker = new Worker('./local_repository/49ce6b6f383591cf86a667a118ff0d2c.js'); const worker = new Worker('./local_repository/5a6bd79bcd13b95441c733827cd6b016.js');
worker.on('message', resolve); worker.on('message', resolve);
worker.on('error', reject); worker.on('error', reject);
worker.on('exit', (code) => { worker.on('exit', (code) => {
...@@ -16,11 +17,4 @@ function runService() { ...@@ -16,11 +17,4 @@ function runService() {
}) })
} }
async function run() { runService().catch(err => console.error(err))
const result = await runService( \ No newline at end of file
)
console.log(result);
}
run().catch(err => console.error(err))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment