You need to sign in or sign up before continuing.
Commit fd39db67 authored by nilanjandaw's avatar nilanjandaw

decoupling daemons almost done

Issue with file server remains, unable to download file from master node
parent 6ddbd13c
test/ *repository/
bitnami* bitnami*
node_modules node_modules
package-lock.json package-lock.json
......
...@@ -11,14 +11,13 @@ function runIsolate(filename) { ...@@ -11,14 +11,13 @@ function runIsolate(filename) {
if (err) if (err)
reject(err); reject(err);
isolate.compileScript(data).then(script => {
script.run(context) context.evalClosure(data).then(result => {
.then(result => {
console.log(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);
});
}).catch(err => { reject(err) }) }).catch(err => { reject(err) })
}); });
}); });
...@@ -26,6 +25,7 @@ function runIsolate(filename) { ...@@ -26,6 +25,7 @@ function runIsolate(filename) {
} }
function runProcess(filename) { function runProcess(filename) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let timeStart = Date.now() let timeStart = Date.now()
const process = spawn('node', [filename]); const process = spawn('node', [filename]);
......
const mqtt = require('mqtt') const mqtt = require('mqtt')
const client = mqtt.connect('mqtt://localhost') const client = mqtt.connect('mqtt://localhost')
const libSupport = require('../dispatcher/lib') const libSupport = require('./lib')
const execute = require('./execute') const execute = require('./execute')
const fs = require('fs')
const node_id = "20sez54hq8" const node_id = "20sez54hq8"
const local_repository = __dirname + "/local_repository/"
const host_url = 'http://127.0.0.1:8080/'
client.on('connect', function () { client.on('connect', function () {
client.subscribe(node_id, function (err) { client.subscribe(node_id, function (err) {
if (!err) { if (!err) {
...@@ -21,37 +23,73 @@ client.on('message', function (topic, message) { ...@@ -21,37 +23,73 @@ client.on('message', function (topic, message) {
let runtime = message.runtime let runtime = message.runtime
let functionHash = message.functionHash let functionHash = message.functionHash
let function_id = message.function_id let function_id = message.function_id
console.log("function_id", function_id);
if (message.type === "execute") { if (message.type === "execute") {
if (runtime === "isolate") console.log("function_id", function_id);
execute.runIsolate('../dispatcher/test/' + functionHash).then( result => { if (!fs.existsSync(local_repository + functionHash)) {
client.publish("response", JSON.stringify({ libSupport.download(host_url + functionHash, local_repository + functionHash).then(() => {
status: "success", if (runtime === "isolate")
result, execute.runIsolate(local_repository + functionHash).then(result => {
function_id client.publish("response", JSON.stringify({
})) status: "success",
}) result,
else if (runtime === "process") function_id
execute.runProcess('../dispatcher/test/' + functionHash).then( result => { }))
client.publish("response", JSON.stringify({ })
status: "success", else if (runtime === "process")
result, execute.runProcess(local_repository + functionHash).then(result => {
function_id client.publish("response", JSON.stringify({
})) status: "success",
result,
function_id
}))
})
else if (runtime === "container")
execute.runContainer(functionHash).then(result => {
client.publish("response", JSON.stringify({
status: "success",
result,
function_id
}))
})
else {
client.publish("response", JSON.stringify({ status: "unknown runtime" }))
return
}
}) })
else if (runtime === "container") } else {
execute.runContainer(functionHash).then(result => { console.log("in else", local_repository + functionHash);
client.publish("response", JSON.stringify({
status: "success", if (runtime === "isolate")
result, execute.runIsolate(local_repository + functionHash).then(result => {
function_id client.publish("response", JSON.stringify({
})) status: "success",
}) result,
else { function_id
client.publish("response", JSON.stringify({ status: "unknown runtime" })) }))
return })
else if (runtime === "process")
execute.runProcess(local_repository + functionHash).then(result => {
client.publish("response", JSON.stringify({
status: "success",
result,
function_id
}))
})
else if (runtime === "container")
execute.runContainer(functionHash).then(result => {
client.publish("response", JSON.stringify({
status: "success",
result,
function_id
}))
})
else {
client.publish("response", JSON.stringify({ status: "unknown runtime" }))
return
}
} }
} }
} }
......
var http = require('http');
var fs = require('fs');
var download = function (url, dest, cb) {
return new Promise((resolve, reject) => {
var file = fs.createWriteStream(dest);
var request = http.get(url, function (response) {
response.pipe(file);
file.on('finish', function () {
file.close(cb); // close() is async, call cb after close completes.
resolve();
});
}).on('error', function (err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message);
reject(err);
});
})
};
module.exports.download = download
\ 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