Skip to content
Snippets Groups Projects
Commit d6080b2e authored by Tuan Hoang Dinh Anh's avatar Tuan Hoang Dinh Anh
Browse files

feat(change-logs): add max size cap for change logs collection

parent c53c7daa
No related branches found
No related tags found
1 merge request!17keep track with main
......@@ -59,6 +59,8 @@ const envVarsSchema = Joi.object()
// Admin emails
ADMIN_EMAILS: Joi.string().description('Admin emails'),
ADMIN_PASSWORD: Joi.string().description('Admin password'),
// Change Logs max size
LOGS_MAX_SIZE: Joi.number().default(100).description('Max size of change logs in megabytes'),
})
.unknown();
......@@ -178,6 +180,7 @@ const config = {
},
adminEmails: envVars.ADMIN_EMAILS?.split(',') || [],
adminPassword: envVars.ADMIN_PASSWORD,
logsMaxSize: envVars.LOGS_MAX_SIZE,
};
if (config.env === 'development') {
......
......@@ -4,12 +4,13 @@ const config = require('./config/config');
const logger = require('./config/logger');
const initializeRoles = require('./utils/initializeRoles');
const { init } = require('./config/socket');
const setupScheduledCheck = require('./scripts/checkVSSUpdate');
const assignAdmins = require('./scripts/assignAdmins');
const { setupScheduledCheck, assignAdmins, convertLogsCap } = require('./scripts');
let server;
mongoose.connect(config.mongoose.url, config.mongoose.options).then(() => {
logger.info('Connected to MongoDB ');
convertLogsCap();
initializeRoles().then(() => assignAdmins());
server = app.listen(config.port, () => {
logger.info(`Listening to port ${config.port}`);
......
......@@ -46,6 +46,7 @@ const changeLogSchema = new mongoose.Schema(
changeLogSchema.plugin(toJSON);
changeLogSchema.plugin(paginate);
changeLogSchema.index({ ref: 1 });
const ChangeLog = mongoose.model('ChangeLog', changeLogSchema);
......
const mongoose = require('mongoose');
const logger = require('../config/logger');
const config = require('../config/config');
const ChangeLog = require('../models/changeLog.model');
async function setLogsCap() {
const db = mongoose.connection.db;
const name = 'changelogs';
const max = config.logsMaxSize * 1024 * 1024; // 100MB capped
try {
const collInfo = await db.listCollections({ name }).next();
if (!collInfo) {
await db.createCollection(name, { capped: true, size: max });
return logger.info(`Created capped collection ${name} with ${max} bytes`);
}
const stats = await db.collection(name).stats();
if (stats.capped && stats.maxSize === max) {
return logger.info(`Collection ${name} is already capped to ${max} bytes. Skipping...`);
}
await db.command({
convertToCapped: name,
size: max,
});
logger.info(`Capped collection ${name} to ${max} bytes`);
} catch (error) {
logger.error(`Error capping collection ${name}: %o`, error?.message || error);
}
}
async function convertLogsCap() {
try {
await setLogsCap();
await ChangeLog.createIndexes();
} catch (error) {
logger.error('Error converting logs cap: %o', error.message || error);
}
}
module.exports = convertLogsCap;
module.exports.assignAdmins = require('./assignAdmins');
module.exports.setupScheduledCheck = require('./setupScheduledCheck');
module.exports.convertLogsCap = require('./convertLogsCap');
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment