【问题标题】:function is timing out when http-triggeredhttp触发时函数超时
【发布时间】:2020-04-08 02:50:48
【问题描述】:

我做错了什么?为什么函数会超时?

我有一个完全可以正常工作的 blob 触发函数:

const Jimp = require("jimp");

module.exports = function(context, myBlob) {
  const correlation = context.bindings.inputBlob.correlation;
  const inputImage = context.bindings.inputBlob.image;
  const imageName = context.bindings.inputBlob.imageName;

  context.log(
    correlation + "Attempting to convert this image to a tiff: " + imageName
  ); 
  Jimp.read(Buffer.from(inputImage, "base64"), function(err, image) {
    image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
      const response = {
        myimage: tiff.toString("base64"),
        correlation: correlation
      };
      context.bindings.outputBlob = response
      context.log(
        correlation + "Successfully converted " + imageName + " to tiff."
      );
      context.done();
    });
  });
};

这是 function.json 文件:

{
  "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "images-in/{destination}/{name}",
      "connection": "AZURE_STORAGE_CONNECTION_STRING"
    },
    {
      "name": "inputBlob",
      "type": "blob",
      "direction": "in",
      "path": "images-in/{destination}/{name}",
      "connection": "AZURE_STORAGE_CONNECTION_STRING"
    },
    {
      "type": "blob",
      "name": "outputBlob",
      "path": "{destination}/{name}.tiff",
      "connection": "AZURE_STORAGE_CONNECTION_STRING",
      "direction": "out"
    }
  ],
  "disabled": false
}

工作方式:

  1. 将 blob 拖放到特定位置
  2. 将生成输出 blob 通过另一个位置的功能(整个过程不超过大约 5 秒)。

我决定我需要该函数是 http 触发的,所以我做了以下更改:

const Jimp = require("jimp");

module.exports = function(context, req) {

  Jimp.read(Buffer.from(req.body, "base64"), function(err, image) {
    image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
      const response = {
        myimage: tiff.toString("base64"),
        correlation: "yeeeeees"
      };


    });
      context.res = {
          body: response
      };
      context.done();
  });


};

function.json

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

但是,我得到了 500

遥测显示如下:

我做错了什么?为什么函数会超时?

我对该函数进行了以下更新:

const Jimp = require("jimp");

module.exports = function (context, req) {
    const text = Buffer.from(req.body, "base64").toString("utf-8");

    Jimp.read(text, function(err, image) {
    if (err) {
      context.res = {
        body: err
      };
      context.done();
    }

    image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
      if (error) {
        context.res = {
          body: error
        };
        context.done();
      }
      const response = {
        myimage: tiff.toString("base64"),
        correlation: "yeeeeees"
      };
      context.res = {
        body: response
      };
      context.done();
    });
  });
};

这产生了以下荒谬的回应:

{
  "errno": -4058,
  "code": "ENOENT",
  "syscall": "open",
  "path": "D:\\home\\site\\wwwroot\\$R\u0005������{\u001av��r��Ū�O�$z�ނ)",
  "methodName": "constructor"
}

【问题讨论】:

  • 您的第二个代码对response 的引用无效(超出范围)并且您调用done 太快了。应该放在内部回调中(稍后执行)。可能这不是您的代码的正确表示?
  • 谢谢!我尝试使用下面答案中的代码,但是,我仍然遇到超时

标签: javascript node.js azure-functions jimp


【解决方案1】:

如果您在 Azure 中检查 node.js 日志,您会看到类似 response is not defined 的错误,因为您定义响应的范围与您使用的范围不同。

所以基本上你不能调用 context.done();函数,这就是您的请求引发超时异常的原因。

使用 async/await 将帮助您避免此类问题。请检查此代码示例以了解可能出现的问题。

const Jimp = require("jimp");

module.exports = function(context, req) {

  Jimp.read(Buffer.from(req.body, "base64"), function(err, image) {

    if(err){
      context.res = {
          body: err
      };
      context.done();
    }

    image.getBuffer(Jimp.MIME_TIFF, function(error, tiff) {
      if(error){
        context.res = {
          body: error
        };
        context.done();
      }
      const response = {
        myimage: tiff.toString("base64"),
        correlation: "yeeeeees"
      };
      context.res = {
        body: response 
      };
      context.done();
    });
  });
};

【讨论】:

  • 使用您的代码后,我收到以下回复{ "methodName": "constructor" } 非常奇怪!
  • 您是否尝试在本地使用相同的代码和相同的图像样本?也许您可以在本地调试它的问题。
猜你喜欢
  • 2020-07-03
  • 1970-01-01
  • 2021-03-26
  • 2019-06-07
  • 1970-01-01
  • 2015-09-25
  • 2020-09-08
  • 2018-08-29
  • 2013-02-26
相关资源
最近更新 更多