节点获取超时问题/快速超时/ lambda超时还是其他?

时间:2019-05-27 20:13:08

标签: node.js express aws-lambda node-fetch

下面的代码在本地运行时可以正常工作,但是在对/ webhook端点执行“ POST”时在aws lambda函数内部运行时,“ getAccessToken()”不能按预期工作。我正在使用节点8.10,“ aws-serverless-express”:“ ^ 3.3.6”,“ express”:“ ^ 4.16.4”和“ node-fetch”:“ ^ 2.5.0”,基本上它打印正确jwt令牌,但node-fetch不返回任何内容,示例日志。

START RequestId:c8efba59-1869-4eaa-b9d8-aa15a7507d52版本:$ LATEST 2019-05-27T19:55:32.328Z c8efba59-1869-4eaa-b9d8-aa15a7507d52开始getExecution 2019-05-27T19:55:32.328Z c8efba59-1869-4eaa-b9d8-aa15a7507d52 exectution_url: 2019-05-27T19:55:32.328Z c8efba59-1869-4eaa-b9d8-aa15a7507d52 https://cloudmanager.adobe.io/somevalidurl 2019-05-27T19:55:32.328Z c8efba59-1869-4eaa-b9d8-aa15a7507d52 start getAccessToken END RequestId:c8efba59-1869-4eaa-b9d8-aa15a7507d52 REPORT RequestId:c8efba59-1869-4eaa-b9d8-aa15a7507d52持续时间:6657.00 ms计费持续时间:6700 ms内存大小:128 MB使用的最大内存:37 MB

我确保lambda超时为30秒,尝试通过将其设置为0来禁用“ node-fetch”超时,并且还对所有路由“ app.use(timeout(“ 30000”))”使用了中间件该特定的Webhook请求超时。 (我立即收到200 pong响应,但getexectuion异步功能无法正常工作)

const express = require('express')
const bodyParser = require('body-parser')
const crypto = require('crypto')
const jsrsasign = require('jsrsasign')
const fetch = require('node-fetch')
const timeout = require('connect-timeout')

const URL  = require('url').URL
const URLSearchParams = require('url').URLSearchParams



//require('dotenv').config()
const app = express()



async function getAccessToken () {
  console.log("start getAccessToken")
  const EXPIRATION = 60 * 60 // 1 hour

  const header = {
    'alg': 'RS256',
    'typ': 'JWT'
  }



  const payload = {
    'exp': Math.round(new Date().getTime() / 1000) + EXPIRATION,
    'iss': process.env.ORGANIZATION_ID,
    'sub': process.env.TECHNICAL_ACCOUNT_ID,
    'aud': `https://ims-na1.adobelogin.com/c/${process.env.API_KEY}`,
    'https://ims-na1.adobelogin.com/s/ent_cloudmgr_sdk': true
  }

  const jwtToken = jsrsasign.jws.JWS.sign('RS256', JSON.stringify(header), JSON.stringify(payload), process.env.PRIVATE_KEY)
  //console.log("jwt token:")
  //console.log(jwtToken)

  const body = new URLSearchParams({
      client_id: process.env.API_KEY,
      client_secret: process.env.CLIENT_SECRET,
      jwt_token: jwtToken
    })


   const response = await fetch('https://ims-na1.adobelogin.com/ims/exchange/jwt', {
    method: 'POST',
    options: { timeout: 0},
    timeout: 0,
    size: 0,
    body: body
  })//.catch(error => {
    //        console.log("an error happend in fetchg")
    //        console.log(error)
    //})


  const json = await response.json()

  if ((response.status !== 200) && (response.status !== 201)) {
      console.error(`Invalid response status ${ response.status }.`);
      throw json;
  }

  console.log("access_token:")
  console.log(json['access_token'])
  return json['access_token']
}

async function makeApiCall (accessToken, url, method) {
  console.log("start make api call")
  const response = await fetch(url, {
    'method': method,
    'headers': {
      'x-gw-ims-org-id': process.env.ORGANIZATION_ID,
      'x-api-key': process.env.API_KEY,
      'Authorization': `Bearer ${accessToken}`
    }
  })

  console.log("finish make api call")
  const json = await response.json()
  return json
}

function getLink (obj, linkType) {
  return obj['_links'][linkType].href
}

async function getExecution (executionUrl) {
  console.log("start getExecution")
  console.log("exectution_url:")
  console.log(executionUrl)
  const accessToken = await getAccessToken()
  console.log("access-token:")
  console.log(accessToken)
  const execution = await makeApiCall(accessToken, executionUrl, 'GET')
  console.log(execution)
  console.log("aaaa")
  const program = await makeApiCall(accessToken, new URL(getLink(execution, 'http://ns.adobe.com/adobecloud/rel/program'), executionUrl))
  console.log(execution)
  console.log("here")
  execution.program = program

  return execution
}


//app.use(bodyParser.json())

app.use(bodyParser.json({
  verify: (req, res, buf, encoding) => {
    const signature = req.header('x-adobe-signature')
    if (signature) {
      const hmac = crypto.createHmac('sha256', process.env.CLIENT_SECRET)
      hmac.update(buf)
      const digest = hmac.digest('base64')

      if (signature !== digest) {
        throw new Error('x-adobe-signature HMAC check failed')
      }
    } else if (!process.env.DEBUG && req.method === 'POST') {
      throw new Error('x-adobe-signature required')
    }
  }
}))

app.use(timeout("30000"))



app.post('/webhook', (req, res) => {

  req.setTimeout(120000, function(){
      console.log('Request has timed out.');
         res.send(408);
  });
  res.writeHead(200, { 'Content-Type': 'application/text' })
  res.end('pong')


    getExecution("https://cloudmanager.adobe.io/<somevalidurl>").then(execution => {
    console.log(`Execution for ${execution.program.name} started`)
    })




})



module.exports = app;

//const port = process.env.PORT || 3000

//app.listen(port, () =>
//  console.log(`App is listening on port ${port}.`)
//)

0 个答案:

没有答案