Skip to content

Commit 249e9b8

Browse files
Merge pull request #5721 from Countly/SER-1987-ss-online-users-no-ttl
[SER-1987] ttl collection clean up job
2 parents db6c3c5 + eb684f8 commit 249e9b8

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

api/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ plugins.connectToAllDatabases().then(function() {
313313
jobs.job('api:clearAutoTasks').replace().schedule('every 1 day');
314314
jobs.job('api:task').replace().schedule('every 5 minutes');
315315
jobs.job('api:userMerge').replace().schedule('every 10 minutes');
316+
jobs.job("api:ttlCleanup").replace().schedule("every 1 minute");
316317
//jobs.job('api:appExpire').replace().schedule('every 1 day');
317318
}, 10000);
318319

api/jobs/ttlCleanup.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const plugins = require("../../plugins/pluginManager.js");
2+
const common = require('../utils/common');
3+
const job = require("../parts/jobs/job.js");
4+
const log = require("../utils/log.js")("job:ttlCleanup");
5+
6+
/**
7+
* Class for job of cleaning expired records inside ttl collections
8+
*/
9+
class TTLCleanup extends job.Job {
10+
/**
11+
* Run the job
12+
*/
13+
async run() {
14+
log.d("Started running TTL clean up job");
15+
for (let i = 0; i < plugins.ttlCollections.length; i++) {
16+
const {
17+
db = "countly",
18+
collection,
19+
property,
20+
expireAfterSeconds = 0
21+
} = plugins.ttlCollections[i];
22+
let dbInstance;
23+
switch (db) {
24+
case "countly": dbInstance = common.db; break;
25+
case "countly_drill": dbInstance = common.drillDb; break;
26+
case "countly_out": dbInstance = common.outDb; break;
27+
}
28+
if (!dbInstance) {
29+
log.e("Invalid db selection:", db);
30+
continue;
31+
}
32+
33+
log.d("Started cleaning up", collection);
34+
const result = await dbInstance.collection(collection).deleteMany({
35+
[property]: {
36+
$lte: new Date(Date.now() - expireAfterSeconds * 1000)
37+
}
38+
});
39+
log.d("Finished cleaning up", result.deletedCount, "records from", collection);
40+
41+
// Sleep 1 second to prevent sending too many deleteMany queries
42+
await new Promise(res => setTimeout(res, 1000));
43+
}
44+
log.d("Finished running TTL clean up job");
45+
}
46+
}
47+
48+
module.exports = TTLCleanup;

plugins/pluginManager.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ var pluginManager = function pluginManager() {
7474
countly_out: "../api/configs/config.db_out.js",
7575
countly_fs: "../api/configs/config.db_fs.js"
7676
};
77-
77+
/**
78+
* TTL collections to clean up periodically
79+
* @type {{collection: string, db: mongodb.Db, property: string, expireAfterSeconds: number}[]}
80+
*/
81+
this.ttlCollections = [];
7882
/**
7983
* Custom configuration files for different databases for docker env
8084
*/

0 commit comments

Comments
 (0)