我在NodeJS上运行lambda函数时遇到了奇怪的问题。
根据CloudWatch日志,它看起来像跳过~10行代码而没有向日志显示~3个打印输出。
缺少的第一个日志是运行MySQL RDS的查询:
var query = con.query(myquery,[orders.SupplierID,orders.sub,orders.DeliveryDate, orders.CustomerComments,orders.TrackingNumber,orders.orderJson],function(err,rows){
console.log(query.sql);
if(err){
console.log("The Query = ",query.sql);
console.log('ERROR DB' , err);
throw err;
}
使用:https://www.npmjs.com/package/mysql
此外,我初始化的变量是获取此特定lambda函数的上一次运行的值。
var executionName = orderIdJson.OrderID.toString() + "_" + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '').replace(/-/g,'_').replace(' ','_').replace(/:/g,'_');
lambda函数的完整代码如下:
const AWS = require('aws-sdk');
var mysql = require('mysql');
var myrows;
function createConnection() {
var con = mysql.createConnection({
host: '****',
port: '3306',
user: '*****',
password: '88888*****',
database: 'db2b2b'
});
return con;
}
function startExecution(orderIdJsonStr) {
var stepfunctions = null;
stepfunctions = new AWS.StepFunctions();
var orderIdJson = JSON.parse(orderIdJsonStr);
console.log(orderIdJson);
var executionName = orderIdJson.OrderID.toString() + "_" + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '').replace(/-/g, '_').replace(' ', '_').replace(/:/g, '_');
console.log("Order created, starting workflow " + executionName);
var params = null;
params = {
stateMachineArn: 'arn:aws:states:us-east-1:453682599770:stateMachine:Post-Order-WF',
input: orderIdJsonStr,
name: executionName
};
console.log(params);
// start a state machine
stepfunctions.startExecution(params, (err, data) => {
if (err) {
// *** THIS IS WHERE THE LOG STARTS AGAIN ***
console.log(err);
console.log("could not create order", orderIdJson)
//callback(err, null);
return;
}
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'started state machine',
result: data
})
};
});
}
exports.handler = (event, context, callback) => {
console.log("EVENT", event);
var con = createConnection();
con.connect(function(err) {
if (err) {
console.log('Error connecting to Db');
console.log(err);
return;
}
});
console.log("con", con);
var orders = {
SupplierID: event['body-json'].SupplierID,
sub: event.context.sub,
DeliveryDate: event['body-json'].DeliveryDate,
CustomerComments: event['body-json'].CustomerComments,
TrackingNumber: event['body-json'].TrackingNumber,
orderJson: event['body-json'].orderJson
};
console.log("orders", orders);
var myquery = 'call usp_createOrder(?,?,?,?,?,?)';
console.log("myquery", myquery);
// *** myquery IS WHERE THE LOG STOPS ***
var query = con.query(myquery, [orders.SupplierID, orders.sub, orders.DeliveryDate, orders.CustomerComments, orders.TrackingNumber, orders.orderJson], function(err, rows) {
console.log(query.sql);
if (err) {
console.log("The Query = ", query.sql);
console.log('ERROR DB', err);
throw err;
}
myrows = rows;
console.log(myrows);
var orderIdJsonStr = JSON.stringify(myrows[0][0]);
console.log(orderIdJsonStr);
startExecution(orderIdJsonStr);
con.end(function(err) {
const res = myrows;
console.log(res[0]);
context.succeed(res[0]);
});
});
};
谢谢!