Guidelines for Function Compute Development - Troubleshoot Timeout Issues

本文涉及的产品
简介: 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 《开发函数计算的正确姿势 —— 排查超时问题》.

目录
相关文章
|
4月前
|
数据采集 Serverless API
在函数计算(Function Compute,FC)中部署Stable Diffusion(SD)
在函数计算(Function Compute,FC)中部署Stable Diffusion(SD)
284 2
|
3月前
|
运维 监控 Dubbo
SAE(Serverless App Engine)和FC(Function Compute)
【1月更文挑战第18天】【1月更文挑战第89篇】SAE(Serverless App Engine)和FC(Function Compute)
87 1
|
4月前
|
存储 Serverless
在阿里云函数计算(Function Compute)中,上传模型的步骤如下
在阿里云函数计算(Function Compute)中,上传模型的步骤如下
238 2
|
5月前
|
监控 前端开发 Serverless
阿里云函数计算(Function Compute,FC)是一种事件驱动的计算服务
阿里云函数计算(Function Compute,FC)是一种事件驱动的计算服务
323 1
|
8月前
|
运维 JavaScript Serverless
Function Compute
函数计算(Function Compute)是云计算领域的一种服务模型,由云服务提供商(例如阿里云、AWS、Google Cloud 等)提供。它是一种无服务器计算服务,允许开发者编写和部署函数,以响应事件触发,而无需管理底层的服务器和基础设施。函数计算提供了弹性的计算资源分配、按需计费、自动扩缩容等特性,使开发者能够聚焦于编写函数逻辑而不必担心底层的运维工作。
195 2
|
9月前
|
Serverless
函数计算(Function Compute)部署失败可能有多种原因
函数计算(Function Compute)部署失败可能有多种原因
114 2
|
10月前
|
弹性计算 监控 负载均衡
阿里云函数计算(Function Compute):快速高效的事件驱动计算
阿里云函数计算(Function Compute)是一种事件驱动计算服务,能够在阿里云上运行代码,且只按照实际使用时间付费。它无需管理服务器和基础架构,并可以与其他阿里云产品以及第三方服务集成,为用户提供了快速、高效、低成本、弹性的云计算能力。
|
数据采集 消息中间件 监控
Function Compute构建高弹性大数据采集系统
解决问题: 1.利用服务器自建数据采集系统成本高,弹性不足。 2.利用服务器自建数据采集系统运维复杂,成本高。
Function Compute构建高弹性大数据采集系统
|
对象存储
Guidelines for Function Compute Development -- OSS Trigger
Preface Terms and descriptions: Function Compute: Alibaba Cloud Function Compute is an event-driven computing service that dynamically allocates resources after you write and upload code.
1096 0
|
SQL 数据库 索引
Using Python to Connect Function Compute to SQL Server
Normally, a third-party module is required for establishing Python-based database connections. To connect to Microsoft SQL Server, pymssql is required.
12596 0