Handler Function
Handler function is a function that will be invoked by AWS Lambda when the trigger is activated.
/**
* The main function
* @param {object} event Event from system
* @returns
*/
export const handler = async (event) => {
// Return the output
return { statusCode: 200, body: 'Success' };
};
Loading Global Variables
// Load environment variables
try {
dotenv.config({ path: '/var/task/.env' });
} catch (err) {
console.error('Error loading .env file:', err);
return { statusCode: 500 };
}
Creating the CSV File Storage
// Setup logging before executing the rest of the code
if (!fs.existsSync(process.env.S3_CSV_FOLDER)) {
fs.mkdirSync(process.env.S3_CSV_FOLDER, { recursive: true });
}
Checking Execution Lock
// Check execution
const executingQueryChk = `SELECT is_executing, started_at, finished_at
FROM indices.s3_execution
WHERE is_executing = true
LIMIT 1`;
const resultEx = await execQuery(executingQueryChk);
if (resultEx.rows.length > 0) {
const startedAt = convertDateStringToDate(resultEx.rows[0]['started_at']);
const finishedAt = convertDateStringToDate(resultEx.rows[0]['finished_at'])
const currentTime = new Date();
// Convert ms to minutes
const timeDifferenceInMinutes = Math.floor((currentTime - startedAt) / (1000 * 60));
if (startedAt > finishedAt && Math.abs(timeDifferenceInMinutes) < process.env.AFTER_FAILED_IGNORING_MINUTES) {
return processEnding(false);
}
}
Finding Import Date
// Find importing date
const findDateQuery = `SELECT date_hns
FROM indices.s3_date_hns
WHERE is_imported = false
ORDER BY date_hns ASC
LIMIT 1`;
const result = await execQuery(findDateQuery);
const now = result.rows[0]['date_hns'];
Updating Execution Info
// Update execution info
const updateStartQuery = `UPDATE indices.s3_execution
SET is_executing = true, started_at = $1, last_executed_date_hns = $2, last_error = null`;
await execQuery(updateStartQuery, [
getTimestamp(),
formatDateForPostgres(now),
]);
Updating Import Date Info
// Update importing date
const updateStartedAtQuery = `UPDATE indices.s3_date_hns
SET is_imported = false, started_at = $1
WHERE date_hns = $2`;
await execQuery(updateStartedAtQuery, [
getTimestamp(),
formatDateForPostgres(now),
]);
No Comments