William Hatch
02/03/2022, 7:03 PMGabriel Araújo
02/03/2022, 8:36 PMAshishkumar Pandey
02/03/2022, 9:48 PMAshishkumar Pandey
02/03/2022, 9:49 PMWilliam Hatch
02/03/2022, 10:04 PM2022-02-03T21:50:20.763Z 8c855639-1beb-473a-870e-bbb2819c9fb5 ERROR Unhandled Promise Rejection {
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "MongoServerSelectionError: connection timed out",
"reason": {
"errorType": "MongoServerSelectionError",
"errorMessage": "connection timed out",
"name": "MongoServerSelectionError",
"reason": {
"type": "ReplicaSetNoPrimary",
"setName": "atlas-ug2v9v-shard-0",
"maxSetVersion": 1,
"maxElectionId": "7fffffff0000000000000012",
"servers": {},
"stale": false,
"compatible": true,
"compatibilityError": null,
"logicalSessionTimeoutMinutes": null,
"heartbeatFrequencyMS": 10000,
"localThresholdMS": 15,
"commonWireVersion": 9
},
"stack": [
"MongoServerSelectionError: connection timed out",
" at Timeout._onTimeout (/var/task/src/kinesis.js:18862:34)",
" at listOnTimeout (internal/timers.js:557:17)",
" at processTimers (internal/timers.js:500:7)"
]
},
"promise": {},
"stack": [
"Runtime.UnhandledPromiseRejection: MongoServerSelectionError: connection timed out",
" at process.<anonymous> (/var/runtime/index.js:35:15)",
" at process.emit (events.js:400:28)",
" at processPromiseRejections (internal/process/promises.js:245:33)",
" at processTicksAndRejections (internal/process/task_queues.js:96:32)"
]
}
The function connection setup:
import * as mongodb from 'mongodb';
const MongoClient = mongodb.MongoClient;
// Once we connect to the database once, we'll store that connection
// and reuse it so that we don't have to connect to the database on every request.
let cachedDb = null;
async function connectToDatabase() {
console.log('connectToDatabase');
if (cachedDb) {
return cachedDb;
}
const connectionURL = process.env.MONGO_URL;
// console.log(
// `the env connection url: ${process.env.MONGO_URL} and the variable from same: ${connectionURL}`
// );
const client = await MongoClient.connect(connectionURL, {
useUnifiedTopology: true,
});
cachedDb = await client.db(process.env.MONGO_DB);
console.log(`returning cachedDb: ${cachedDb}`);
return cachedDb;
}
export async function main(event, context) {
// console.log(`the kinesis event: ${JSON.stringify(event)}`);
// per atlas docs, this isn't appropriate as we're in async without a callback arg
// tried this both ways, no difference in error frequency
// context.callbackWaitsForEmptyEventLoop = false;
const db = await connectToDatabase();
console.log(`The db: ${db}`);
event.Records.forEach((item) => {
let payload = Buffer.from(item.kinesis.data, 'base64').toString('utf-8');
const record = JSON.parse(payload);
// console.log(
// `the record decoded, to be inserted: ${JSON.stringify(record)}`
// );
const insertRecord = {
customer_id: record.customer_id,
device_id: record.item_id,
sensorid: record.sensorid,
state: record.state,
voltage: record.voltage,
type: record.type,
signal: record.signal,
data: record.deviceData,
customer_code: record.customer_code,
sensortimestamp: new Date(record.sensortimestamp),
};
// console.log(`inserting ${JSON.stringify(insertRecord)}`);
db.collection(process.env.MONGO_COLLECTION).insertOne(insertRecord);
});
return {
statusCode: 200,
body: 'ok',
};
}
And, the connection url is modified to include &authMechanism=SCRAM-SHA-1, per Atlas recommendation, as that avoids the second attempt after initially trying using ...SHA-256, because as of now, they only support SHA-1
Thanks!William Hatch
02/03/2022, 10:08 PMAshishkumar Pandey
02/04/2022, 12:51 AMWilliam Hatch
02/04/2022, 11:48 AMWilliam Hatch
02/04/2022, 4:37 PMGabriel Araújo
02/04/2022, 6:27 PMWilliam Hatch
02/04/2022, 10:07 PMGabriel Araújo
02/04/2022, 10:45 PMbike-bill
02/07/2022, 1:03 PMAshishkumar Pandey
02/07/2022, 1:39 PM