I have a schedule option in my API what I did with Expressjs and NodeJS. Our backend calls this API with two parameters (userID & timestamp). The goal is to delete user informations (folders, documents, etc) at the specified time (..or few minutes later). The timestamp can be 5 minutes to 2 days from now.
When it happened the job is done and the API is waiting for an other call. The API can receive 1000 calls per day, so the performance is very important. What's the best way to run the function in the future?
async function scheduledAction(userId, timestamp) {
var actionInFuture = await checkDate(timestamp);
if(actionInFuture) {
console.log('Action triggered');
}
}
async function checkDate(timestamp) {
await new Promise(resolve => {
setTimeout(resolve, 1000)
});
// timestamp = 1627311533000
if(Date.now() >= timestamp) {
return true;
} else {
return checkDate(timestamp);
}
}
app.get('/action', (req, res) => {
scheduledAction(req.query.userId, req.query.timestamp);
}
scheduledAction(req.query.userId, req.query.timestamp);