All of my functions are timing out after 10 seconds despite their tasks being completed in less than 10 seconds. Most of these functions connect to the database to do CRUD operations, below I’ve posted a snippet of my login.js function.
const middy = require('middy')
const onlyPost = require('./_middleware/onlyPost')
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const { connectToDb } = require('./_utils/db')
const { BAD_REQUEST } = require('./_utils/errorMessages')
const login = async (event, context) => {
// Destructure 'username' and 'password' out of the body
const { username, password } = JSON.parse(event.body)
// Check if 'username' and 'password' exist and are set
if (!username || !password) return BAD_REQUEST
// Connect to database
const { User } = connectToDb()
// Check if the user exists in the database
const user = await User.findOne({
where: { username }
})
if (!user)
return {
statusCode: 400,
body: JSON.stringify({
err: true,
msg: 'User not found.'
})
}
// Compare passwords
const comparePasswords = await bcrypt.compare(password, user.hashedPassword)
if (!comparePasswords)
return {
statusCode: 403,
body: JSON.stringify({
err: true,
msg: 'Password invalid.'
})
}
// Generate a JWT
const token = jwt.sign(
{
id: user.id,
isAdmin: user.isAdmin
},
process.env.AUTH_SECRET
)
// Console log
console.log(user)
// Return the JWT
return {
statusCode: 200,
body: JSON.stringify({
err: false,
token
})
}
module.exports.handler = middy(login).use(onlyPost())
This is the output from Netlify for login.js
1:09:20 PM: 2020-02-15T19:09:20.448Z d5e2e2d4-071b-4c86-995d-2aafab1456f1 INFO Executing (default): SELECT `id`, `firstname`, `username`, `hashedPassword`, `email`, `discord`, `count`, `isAdmin`, `createdAt`, `updatedAt` FROM `users` AS `user` WHERE `user`.`username` = 'Athys' LIMIT 1;
1:09:20 PM: 2020-02-15T19:09:20.667Z d5e2e2d4-071b-4c86-995d-2aafab1456f1 INFO user {...}
1:09:29 PM: Duration: 10010.15 ms Memory Usage: 112 MB Init Duration: 519.65 ms
1:09:29 PM: 2020-02-15T19:09:29.992Z d5e2e2d4-071b-4c86-995d-2aafab1456f1 Task timed out after 10.01 second
As you can see, the user is grabbed within 1 second of invoking the function, however, I’m still receiving this after 10 seconds:
Any ideas as to what is going on?