Netlify lambda 函数在 10 秒后超时

kha*_*han 5 netlify

我在生产环境中从 Netlify 无服务器函数返回数据时遇到问题。目前,它会在 10 秒后超时,并返回状态代码 502 和以下错误消息:

{
  "errorMessage": "2019-... 5f... Task timed out after 10.01 seconds"
}
Run Code Online (Sandbox Code Playgroud)

但是,在我的开发环境中,Netlify 无服务器功能可以完美运行,并响应我数据库中的数据。

我已确认 Netlify 站点上的环境变量和我的.env文件中的环境变量是相同的。我将如何调试这个问题并解决问题?


Netlify无服务器功能

const { connectDatabase, ProjectSchema } = require('../database')

const createHeaders = origin => {
    // This is a comma-separated list of allowed origins, including my local 
    // environment and production website
    const allowedOrigins = process.env.ALLOWED_ORIGINS
        ? process.env.ALLOWED_ORIGINS.split(',')
        : []

    return {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Headers':
            'Origin, X-Requested-With, Content-Type, Accept',
        'Access-Control-Allow-Origin': allowedOrigins.includes(origin)
            ? origin
            : ''
    }
}

/**
 * This function queries for all `Project` instances in the database and
 * returns it in the response body.
 */
const getProjects = async origin => {
    try {
        const projects = await ProjectSchema.find()

        return {
            headers: createHeaders(origin),
            statusCode: 200,
            body: JSON.stringify({ projects })
        }
    } catch (err) {
        return {
            headers: createHeaders(origin),
            statusCode: 400,
            body: JSON.stringify({ error: err })
        }
    }
}

/**
 * This function is the serverless lambda for `/.netlify/functions/get-projects`.
 */
exports.handler = async event => {
    try {
        await connectDatabase()
        const response = await getProjects(event.headers.origin)
        return response
    } catch (err) {
        return err
    }
}

Run Code Online (Sandbox Code Playgroud)

数据库功能

require('dotenv').config()
const mongoose = require('mongoose')
const { Schema } = mongoose

/**
 * This function establishes a connection to the MongoDB Atlas database.
 */
exports.connectDatabase = async () => {
    await mongoose.connect(process.env.DATABASE_URL, {
        useNewUrlParser: true,
        useUnifiedTopology: true
    })
}

exports.ProjectSchema = mongoose.model(
    'project',
    new Schema({
        title: {
            type: String,
            required: [true, 'Title field is required']
        },
        description: {
            type: String,
            required: [true, 'Description field is required']
        }
    })
)
Run Code Online (Sandbox Code Playgroud)

从客户端获取请求

const App = () => {
    const [projects, setProjects] = useState([])

    useEffect(() => {
        fetch(API.GET_PROJECTS, {
            headers: { 'Content-Type': 'application/json' }
        })
            .then(res => res.json())
            .then(({ projects }) => setProjects(projects))
    }, [])

    return (...)
}
Run Code Online (Sandbox Code Playgroud)

netlify.toml

[build]
  functions = "functions"
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "scripts": {
    "build": "npm run build:client & npm run build:server",
    "build:client": "webpack --config webpack.client.js",
    "build:server": "netlify-lambda build src/server/functions -c webpack.server.js",
    "start": "netlify-lambda serve src/server/functions",
    "start:dev": "npm run build:client -- --watch"
  }
}
Run Code Online (Sandbox Code Playgroud)

Gee*_*Gee 5

这可能有点晚了,但是 netlify 函数有 10 秒的执行限制:https://docs.netlify.com/functions/overview/#default-deployment-options

他们指定,如果您需要更长的执行时间,您可以与他们的销售团队讨论您的用例,他们也许可以调整它。

当您在开发环境中运行时,执行时间是多少?