Guidelines for Function Compute Development - Troubleshoot Timeout Issues

本文涉及的产品
Serverless 应用引擎 SAE,800核*时 1600GiB*时
函数计算FC,每月免费额度15元,12个月
简介: Endless codes and endless bugsWhen you write code, you may inadvertently introduce some hidden bugs, even if you test a large proportion of the codes to the maximum extent possible.

Endless codes and endless bugs

When you write code, you may inadvertently introduce some hidden bugs, even if you test a large proportion of the codes to the maximum extent possible.It takes a sort of skill to write codes with bugs that are hidden or look like features, pass all processes (including testing and review), and are finally merged into the trunk.

This document describes how to fix code bugs when you have forgotten how they were written.

Code with hidden bugs

var request = require('request');

exports.handler = function(event, context, callback) {
    console.log("event: " + event);
    console.log('context: ', JSON.stringify(context));

    const options = {
        url: 'https://saweather.market.alicloudapi.com/spot-to-weather?area=%E6%B3%B0%E5%B1%B1&need3HourForcast=0&needAlarm=0&needHourData=0&needIndex=0&needMoreDay=0',

        headers: {
            Authorization: 'APPCODE 5d9129e294fc4f518793ae9f9a15dbff'
        }
    }

    request(options, function (error, response, body) {
        if (error || response.statusCode != 200) {
            console.log("error " + error);
            return 
        } 

        console.log(body.day_weether);
    });
};

The preceding is a sample code of a simple nodejs function. When you are getting to know Function Compute, you may write such a code to simply test the function use.However, after you publish the code, function timeout exception occurs.

Bold assumption and careful verification

When you have no idea about the cause of the bug, make the following assumptions boldly:

  1. The function code is wrong.
  2. The code logic is wrong.
  3. Function Compute has a problem.

To exclude the preceding three assumptions, use Fun Local to test the code locally:

fun local invoke nodejs_timeout

The following result is returned.

As you can see, the program gets stuck here. Assumption 3 is excluded.

Then, add certain logs or perform single-step debugging (here, single-step debugging is selected for simplicity) to further narrow the troubleshooting scope.

On VS Code, set a breakpoint on the sidebar:

Run the following command to run the function in debugging mode (for basic debugging methods, see(https://yq.aliyun.com/articles/672623)):

fun local invoke -d 3000 nodejs_timeout

Click the Start Debugging button on VS Code:

As you can see, the function is properly called and the code runs to the entry function.Assumption 1 is excluded.

Next, check whether assumption 2 is true.

Set a breakpoint at the point of request. The code continues running to this point, and you can see the variable values on Fun Local.

As expected, the statusCode returned in the response to the HTTP request is 200.The body data is also as expected.

Run the body.weakday command in Watch.

The result is not as expected.

Take a closer look at the body object. Its display format is wrong. Use the typeof function to print the body type:

The body type is string.

It is incorrect.

Change the body type to JSON.

After the body type is changed, it turns out to be even worse. day_weether is hidden deep in the content, instead of being directly under the body object.

Moreover, the word day_weether has a spelling mistake.The correct spelling is as follows:

JSON.parse(body).showapi_res_body.f1.day_weather

Check the correct expression in Watch. The correct value is returned:

Paste the correct value into the code.

Run the function again. The following data is returned:

However, the function gets stuck here again and the timeout issue persists.

New findings at the dead end

Think about the preceding process.

Based on the preceding debugging result, the function runs and returns a correct result, but it does not end until the timeout. Open the Function Compute Nodejs Documentation and read it through quickly. You now have the answer.

Then, continue to fix the bug.

callback(null, JSON.parse(body).showapi_res_body.f1.day_weather);

Then the result is correct:

There is one more thing you need to do for exception handling:

if (error || response.statusCode != 200) {
    console.log("error " + error);
    callback(error, null) ;
} 

Finally, you have the bug fixed.

Adequate preparations essential for a good job

The following three lessons can be learned from the preceding event:

  1. Be patient, careful, and diligent in the compilation of every code.Compiling the function code all at once may save time, but hidden bugs may exist and they might even fool you.
  2. You must read more documents.Read the relevant documents, for example, language documents for language use, library documents for the use of third-party libraries, and product documents for product use.If not, the code you write will likely be riddled with bugs. It is necessary to fully prepare for your work.
  3. Proficiency in Fun is of particular importance. Fun plays a great role in fixing bugs.However, compared to other debugging tools, it is easier to learn. Fun, especially its local debugging function. Considering Fun's great advantages, spend more time on Fun.

Note

This article was translated from 《开发函数计算的正确姿势 —— 排查超时问题》.

目录
相关文章
|
8月前
|
数据采集 Serverless API
在函数计算(Function Compute,FC)中部署Stable Diffusion(SD)
在函数计算(Function Compute,FC)中部署Stable Diffusion(SD)
312 2
|
21天前
|
JSON Java Serverless
函数计算操作报错合集之报错Function time out after该怎么办
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
|
21天前
|
存储 缓存 Serverless
函数计算操作报错合集之如何处理运行时报错:“Function time out after 600 seconds”
在使用函数计算服务(如阿里云函数计算)时,用户可能会遇到多种错误场景。以下是一些常见的操作报错及其可能的原因和解决方法,包括但不限于:1. 函数部署失败、2. 函数执行超时、3. 资源不足错误、4. 权限与访问错误、5. 依赖问题、6. 网络配置错误、7. 触发器配置错误、8. 日志与监控问题。
|
3月前
|
运维 监控 JavaScript
【阿里云云原生专栏】Serverless架构下的应用部署与运维:阿里云Function Compute深度探索
【5月更文挑战第21天】阿里云Function Compute是事件驱动的无服务器计算服务,让用户无需关注基础设施,专注业务逻辑。本文详述了在FC上部署应用的步骤,包括创建函数、编写代码和部署,并介绍了运维功能:监控告警、日志管理、版本管理和授权管理,提供高效低成本的计算服务。
267 6
|
3月前
|
Serverless 应用服务中间件 数据安全/隐私保护
Serverless 应用引擎操作报错合集之在阿里函数计算中,函数执行超时,报错Function time out after如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
|
3月前
|
运维 监控 Dubbo
SAE(Serverless App Engine)和FC(Function Compute)
【1月更文挑战第18天】【1月更文挑战第89篇】SAE(Serverless App Engine)和FC(Function Compute)
134 1
|
3月前
|
存储 Serverless
在阿里云函数计算(Function Compute)中,上传模型的步骤如下
在阿里云函数计算(Function Compute)中,上传模型的步骤如下
287 2
|
9月前
|
监控 前端开发 Serverless
阿里云函数计算(Function Compute,FC)是一种事件驱动的计算服务
阿里云函数计算(Function Compute,FC)是一种事件驱动的计算服务
377 1
|
运维 JavaScript Serverless
Function Compute
函数计算(Function Compute)是云计算领域的一种服务模型,由云服务提供商(例如阿里云、AWS、Google Cloud 等)提供。它是一种无服务器计算服务,允许开发者编写和部署函数,以响应事件触发,而无需管理底层的服务器和基础设施。函数计算提供了弹性的计算资源分配、按需计费、自动扩缩容等特性,使开发者能够聚焦于编写函数逻辑而不必担心底层的运维工作。
259 2
|
Serverless
函数计算(Function Compute)部署失败可能有多种原因
函数计算(Function Compute)部署失败可能有多种原因
139 2

热门文章

最新文章