2016-08-24 3 views
0

Node.js 프로젝트에서 잘 실행되는 Sequelize 기반 코드가 있습니다. 그 코드를 AWS Lambda 핸들러로 옮기고 node-lambda 모듈로 테스트하고 있습니다. 이제 Sequelize 코드는 건너 뛴 것처럼 보입니다. 람다가 끝나기 전에 약속이 처리되지 않는지, 아니면 내가 다른 것을 놓치고 있는지 확실하지 않습니다. 다음 코드는 아래 출력에 표시된대로 "during"console.log를 건너 뜁니다.AWS 람다 내부에서 코드 실행 안 함

var models = require('./models'); 

exports.handler = function(event, context, callback) { 
    console.log("Before"); 

    var body = JSON.parse(event.body); 

    // Find the device ID from the devices table 
    models.Device.findOne({where: {device_uuid: body.device_uuid}, attributes: ['id']}).then(function(device) { 
     console.log("During"); 

     // Make sure that we got a device back 
     if (device === null) { 
      console.log("Device not found - could not insert data"); 
      return; 
     } 
     else { 
      console.log("Device found, inserting data - " + body.device_uuid); 

      //Insert the data for this device 
      models.Data.create({ 
       device_id: device.id,    
       data: body.data 
      }); 
     } 
    }); 

    console.log("After"); 

    callback(null, "{\"status\": \"success\"}"); 
} 

수익률 ... 내가 잘못거야 위치에

Before 
After 
Success: 
"{\"status\": \"success\"}" 

어떤 아이디어? Node v5.9.0을 사용하고 있습니다.

+0

콜백 참조를 제거하면 Sequelize 코드가 정상적으로 실행되는 것으로 나타났습니다. – user2174937

답변

3

나는 단지 apigateway/lambda로 재생을 시작하고 후편을 만들었지 만, 노드에 대해 알고 후유증을 남기려면 콜백은 "then"블록 안에 있어야합니다.

어제는 콜백 (null, successData)을 사용하는 경우 성능이 매우 좋지 않음 (Select top 1에서 11 초 초과)을 확인했습니다. context.callbackWaitsForEmptyEventLoop = false 플래그를 변경하면 api 호출에 24ms가 걸립니다.

//Important to improve performance! 
    context.callbackWaitsForEmptyEventLoop = false 

    // Find the device ID from the devices table 
    models.Device.findOne({where: {device_uuid: body.device_uuid}, attributes: ['id']}).then(function(device) { 
     console.log("During"); 

     // Make sure that we got a device back 
     if (device === null) { 
      callback(new Error("Device not found - could not insert data")) 
     } 
     else { 
      console.log("Device found, inserting data - " + body.device_uuid); 

      //Insert the data for this device 
      models.Data.create({ 
       device_id: device.id,    
       data: body.data 
      }) 
      .then(function(insertedData){ 
       callback(null, insertedData.toJSON())  
      }) 
      .catch(function(error){ 
       callback(new Error("Error creating") 
      }) 
     } 
    })  
    console.log("After") 
}