-
Notifications
You must be signed in to change notification settings - Fork 974
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5708 from Countly/SER-1985-ss-logger-plugin-needs…
…-to-be-reworked [SER-1985] logger: replaced capped collection with periodic clean up job
- Loading branch information
Showing
3 changed files
with
67 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @typedef {import("mongodb").Db} Database | ||
* @typedef {import("mongodb").ObjectId} ObjectId | ||
*/ | ||
const JOB = require("../../../../api/parts/jobs/job.js"); | ||
const log = require("../../../../api/utils/log.js")("job:logger:clear"); | ||
|
||
const DEFAULT_MAX_ENTRIES = 1000; | ||
|
||
/** | ||
* clears logs | ||
*/ | ||
class ClearJob extends JOB.Job { | ||
/** | ||
* Cleans up the logs{APPID} collection | ||
* @param {Database} db mongodb database instance | ||
*/ | ||
async run(db) { | ||
let max = this.data.max; | ||
if (typeof max !== "number") { | ||
log.e( | ||
"Maximum number of log entries required. Falling back to default value:", | ||
DEFAULT_MAX_ENTRIES | ||
); | ||
max = DEFAULT_MAX_ENTRIES; | ||
} | ||
|
||
log.d("Started: cleaning logs before the last", max); | ||
const appIds = await db.collection("apps").find().project({ _id: 1 }).toArray(); | ||
const loggerCollections = appIds.map(({_id}) => "logs" + _id.toString()); | ||
|
||
for (const colName of loggerCollections) { | ||
const col = db.collection(colName); | ||
// find the first entry we want to keep | ||
const firstEntry = await col.find({}).project({ _id: 1 }).sort({ _id: -1 }) | ||
.skip(max - 1).limit(1).toArray(); | ||
if (!firstEntry[0]) { | ||
continue; | ||
} | ||
// delete all previous entries | ||
const result = await col.deleteMany({ _id: { $lt: firstEntry[0]._id } }); | ||
log.d("Cleaned up", result.deletedCount, "entries from", colName); | ||
} | ||
log.d("Finished cleaning logs"); | ||
} | ||
} | ||
|
||
module.exports = ClearJob; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +0,0 @@ | ||
var async = require('async'), | ||
pluginManager = require('../pluginManager.js'); | ||
|
||
console.log("Installing logger plugin"); | ||
pluginManager.dbConnection().then((countlyDb) => { | ||
countlyDb.collection('apps').find({}).toArray(function(err, apps) { | ||
|
||
if (!apps || err) { | ||
countlyDb.close(); | ||
return; | ||
} | ||
function upgrade(app, done) { | ||
console.log("Creating logs collection for " + app.name); | ||
function cb() { | ||
done(); | ||
} | ||
|
||
countlyDb.command({ "listCollections": 1, "filter": { "name": "logs" + app._id } }, function(err, res) { | ||
if (err) { | ||
console.log(err); | ||
cb(); | ||
} | ||
else { | ||
//check if collection capped | ||
if (res && res.cursor && res.cursor.firstBatch && res.cursor.firstBatch.length > 0) { | ||
//collection exists | ||
if (!res.cursor.firstBatch[0].options.capped) { | ||
console.log("converting to the capped"); | ||
countlyDb.command({ "convertToCapped": 'logs' + app._id, size: 10000000, max: 1000 }, | ||
function(err) { | ||
if (err) { | ||
console.log(err); | ||
} | ||
cb(); | ||
}); | ||
} | ||
else { | ||
cb(); | ||
} | ||
} | ||
else { //collection does not exist | ||
console.log("collection does not exist"); | ||
countlyDb.createCollection('logs' + app._id, { capped: true, size: 10000000, max: 1000 }, cb); | ||
} | ||
} | ||
}); | ||
} | ||
async.forEach(apps, upgrade, function() { | ||
console.log("Logger plugin installation finished"); | ||
countlyDb.close(); | ||
}); | ||
}); | ||
}); | ||