Commit 57ce14d2 authored by Nilanjan Daw's avatar Nilanjan Daw

Added support for dynamic idle time via HTTP requests

parent 50bf8185
{"id":"10.196.6.51","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
...@@ -5,7 +5,7 @@ let request = require('request') ...@@ -5,7 +5,7 @@ let request = require('request')
const process = require('process') const process = require('process')
const app = express() const app = express()
let port = 5000, resource_id, functionHash, runtime let port = 5000, resource_id, functionHash, runtime, idleTime = 30
resource_id = process.argv[2] resource_id = process.argv[2]
functionHash = process.argv[3] functionHash = process.argv[3]
...@@ -25,46 +25,54 @@ let kafka = require('kafka-node'), ...@@ -25,46 +25,54 @@ let kafka = require('kafka-node'),
app.use(bodyParser.urlencoded({ extended: true })) app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json()) app.use(bodyParser.json())
let lastRequest = Date.now()
let lastRequest = Date.now(), totalRequest = 0
app.post('/serverless/function/execute/', (req, res) => { app.post('/serverless/function/execute/', (req, res) => {
let payload = req.body let payload = req.body
lastRequest = Date.now() lastRequest = Date.now()
totalRequest++
executor(payload).then((result) => { executor(payload).then((result) => {
res.json(result) res.json(result)
}) })
}) })
app.post('/serverless/worker/timeout', (req, res) => {
idleTime = req.body.timeout
console.log("Idle time set to: ", idleTime);
})
function executor(payload) { function executor(payload) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
}) })
} }
app.post('/serverless/function/execute/2', (req, res) => {
console.log("2", JSON.stringify(req.headers))
res.send("done")
})
app.listen(port, () => { app.listen(port, () => {
console.log(`Resource ${resource_id} Server listening on port ${port}!`) console.log(`Resource ${resource_id} Server listening on port ${port}!`)
producer.send( producer.send(
[{ [{
topic: "deployed", topic: "deployed",
messages: JSON.stringify({ functionHash, portExternal: port, runtime, resource_id, entity_id: process.pid}), messages: JSON.stringify({ functionHash, portExternal: port,
runtime, resource_id, entity_id: process.pid}),
"status": true "status": true
}], () => { }) }], () => { })
}) })
function shouldDie() { function shouldDie() {
if (Date.now() - lastRequest > 30 * 1000) { if (Date.now() - lastRequest > idleTime * 1000) {
let message = JSON.stringify({
functionHash, portExternal: port,
runtime, resource_id, entity_id: process.pid,
total_request: totalRequest
})
console.log("Idle for too long. Exiting"); console.log("Idle for too long. Exiting");
producer.send( producer.send(
[{ [
topic: "removeWorker", {topic: "removeWorker", messages: message }
messages: JSON.stringify({ functionHash, portExternal: port, runtime, resource_id, entity_id: process.pid}) ], () => {
}], () => {
console.log("Ending worker for function", functionHash, "resource_id", resource_id); console.log("Ending worker for function", functionHash, "resource_id", resource_id);
process.exit(0) process.exit(0)
}) })
......
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