'use strict';
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const morgan = require('morgan')
app.use(morgan('combined'))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
let port = 5000, lastRequest = Date.now()
app.post('/serverless/function/execute', (req, res) => {
    let payload = req.body
    lastRequest = Date.now()
    executor(payload).then((result) => {
        res.json(result)
    })
})

function executor(payload) {
    return new Promise((resolve, reject) => {let a = payload.message

resolve(a)
// while (i < 10000) {
//     let x = 10
//     x = x ** 3
//     i++
// }

    })
}

app.listen(port, () => console.log(`Server listening on port ${port}!`))

function shouldDie() {
    console.log(Date.now() - lastRequest);
    
    if (Date.now() - lastRequest > 5000) {
        console.log("Idle for too long. Existing");
        
        process.exit(0)
    }
}

setInterval(shouldDie, 1000);