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"}
\ No newline at end of file
{"id":"192.168.31.51","master_node":"10.129.6.5"}
\ No newline at end of file
'use strict';
const isolateBackend = require('./isolate')
// const isolateBackend = require('./isolate')
const fs = require('fs')
const { spawn } = require('child_process');
const { Worker, isMainThread, workerData } = require('worker_threads');
const registry_url = "10.129.6.5:5000/"
const events = require('events');
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) => {
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();
if (err)
reject(err);
// fs.readFile(filename, 'utf-8', (err, data) => {
context.evalClosure(data).then(result => {
// if (err)
// reject(err);
// context.evalClosure(data).then(result => {
let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("isolate time taken: ", timeDifference);
resolve(result.result);
// let timeDifference = Math.ceil((Date.now() - timeStart))
// console.log("isolate time taken: ", timeDifference);
// resolve(result.result);
}).catch(err => { reject(err) })
});
// }).catch(err => { reject(err) })
// });
});
}
......
......@@ -42,7 +42,7 @@ libSupport.makeTopic(node_id).then(() => {
let port = message.port
if (message.type === "execute") {
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)
})
......@@ -59,16 +59,17 @@ function startWorker(local_repository, functionHash,function_id, producer, runti
usedPort.set(port, functionHash)
fs.writeFileSync('./local_repository/config.json', JSON.stringify({port}));
if (runtime === "isolate")
execute.runIsolate(local_repository + functionHash).then(result => {
producer.send([{
topic: "response",
messages: JSON.stringify({
status: "success",
result,
function_id
})
}], () => { })
})
execute.runIsolate(local_repository, functionHash, port)
// .then(result => {
// producer.send([{
// topic: "response",
// messages: JSON.stringify({
// status: "success",
// result,
// function_id
// })
// }], () => { })
// })
else if (runtime === "process")
execute.runProcess(local_repository, functionHash, port)
// .then(result => {
......
const { Worker, isMainThread, workerData } = require('worker_threads');
function runService() {
return new Promise((resolve, reject) => {
if (isMainThread) {
const worker = new Worker('./local_repository/49ce6b6f383591cf86a667a118ff0d2c.js');
const worker = new Worker('./local_repository/5a6bd79bcd13b95441c733827cd6b016.js');
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
......@@ -10,17 +11,10 @@ function runService() {
reject(new Error(`Worker stopped with exit code ${code}`));
console.log("worker exited");
})
})
}
})
}
async function run() {
const result = await runService(
)
console.log(result);
}
run().catch(err => console.error(err))
\ No newline at end of file
runService().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