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*
node_modules
package-lock.json
......
......@@ -11,14 +11,13 @@ function runIsolate(filename) {
if (err)
reject(err);
isolate.compileScript(data).then(script => {
script.run(context)
.then(result => {
console.log(result);
let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("isolate time taken: ", timeDifference);
resolve(result);
});
context.evalClosure(data).then(result => {
let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("isolate time taken: ", timeDifference);
resolve(result.result);
}).catch(err => { reject(err) })
});
});
......@@ -26,6 +25,7 @@ function runIsolate(filename) {
}
function runProcess(filename) {
return new Promise((resolve, reject) => {
let timeStart = Date.now()
const process = spawn('node', [filename]);
......
const mqtt = require('mqtt')
const client = mqtt.connect('mqtt://localhost')
const libSupport = require('../dispatcher/lib')
const libSupport = require('./lib')
const execute = require('./execute')
const fs = require('fs')
const node_id = "20sez54hq8"
const local_repository = __dirname + "/local_repository/"
const host_url = 'http://127.0.0.1:8080/'
client.on('connect', function () {
client.subscribe(node_id, function (err) {
if (!err) {
......@@ -21,37 +23,73 @@ client.on('message', function (topic, message) {
let runtime = message.runtime
let functionHash = message.functionHash
let function_id = message.function_id
console.log("function_id", function_id);
if (message.type === "execute") {
if (runtime === "isolate")
execute.runIsolate('../dispatcher/test/' + functionHash).then( result => {
client.publish("response", JSON.stringify({
status: "success",
result,
function_id
}))
})
else if (runtime === "process")
execute.runProcess('../dispatcher/test/' + functionHash).then( result => {
client.publish("response", JSON.stringify({
status: "success",
result,
function_id
}))
console.log("function_id", function_id);
if (!fs.existsSync(local_repository + functionHash)) {
libSupport.download(host_url + functionHash, local_repository + functionHash).then(() => {
if (runtime === "isolate")
execute.runIsolate(local_repository + functionHash).then(result => {
client.publish("response", JSON.stringify({
status: "success",
result,
function_id
}))
})
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
}
})
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 {
console.log("in else", local_repository + functionHash);
if (runtime === "isolate")
execute.runIsolate(local_repository + functionHash).then(result => {
client.publish("response", JSON.stringify({
status: "success",
result,
function_id
}))
})
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