AWS Lambda和Redis客户端.为什么我不能叫回叫? [英] AWS Lambda and Redis client. Why can't I call callback?

查看:294
本文介绍了AWS Lambda和Redis客户端.为什么我不能叫回叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个使用Redis的AWS Lambda函数.当我运行以下代码时:

I'm trying to write an AWS Lambda function which uses Redis. When I run the code below:

'use strict'

function handler (data, context, callback) {
  const redis = require("redis")
  const _ = require("lodash")
  console.log('before client')
  const client = redis.createClient({
    url: 'redis://cache-url.euw1.cache.amazonaws.com:6379',
  })
  console.log('after client')
  callback(null, {status: 'result'})
  console.log('after callback')
}

exports.handler = handler

我有一个这样的答案:

{
  "errorMessage": "2016-09-20T15:22:27.301Z 07d24e0b-7f46-11e6-85e9-e5f48906c0da Task timed out after 3.00 seconds"
}

和日志如下:


17:22:24
START RequestId: 07d24e0b-7f46-11e6-85e9-e5f48906c0da Version: $LATEST

17:22:26
2016-09-20T15:22:26.014Z    07d24e0b-7f46-11e6-85e9-e5f48906c0da    before client

17:22:26
2016-09-20T15:22:26.134Z    07d24e0b-7f46-11e6-85e9-e5f48906c0da    after client

17:22:26
2016-09-20T15:22:26.135Z    07d24e0b-7f46-11e6-85e9-e5f48906c0da    after callback

17:22:27
END RequestId: 07d24e0b-7f46-11e6-85e9-e5f48906c0da

17:22:27
REPORT RequestId: 07d24e0b-7f46-11e6-85e9-e5f48906c0da  Duration: 3001.81 ms    Billed Duration: 3000 ms Memory Size: 128 MB    Max Memory Used: 24 MB

17:22:27
2016-09-20T15:22:27.301Z 07d24e0b-7f46-11e6-85e9-e5f48906c0da Task timed out after 3.00 seconds

,恕我直言,这意味着调用了回调,但是什么也没发生.

which, IMHO, means that callback was called but nothing happened.

删除客户端的初始化后,我会看到正确的响应.

When I remove client's initialization I see proper response.

有什么想法吗?

推荐答案

来自

调用回调时,Lambda函数仅在 Node.js事件循环为空.

When the callback is called, the Lambda function exits only after the Node.js event loop is empty.

由于您正在调用回调,但是您的Lambda函数调用尚未结束,因此看来事件循环上还有些东西.除了创建Redis连接之外,您的功能实际上没有做任何事情.我猜想您需要在完成Redis连接后关闭它,以便清除事件循环并允许Lambda调用完成.

Since you are calling the callback, but your Lambda function invocation is not ending, it appears you still have something on the event loop. Your function isn't really doing anything except creating a Redis connection. I'm guessing you need to close the Redis connection when you are done with it, in order to clear the event loop and allow the Lambda invocation to complete.

这篇关于AWS Lambda和Redis客户端.为什么我不能叫回叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆