Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Changes to countly_multi_app_expire_script #5814

Merged
merged 7 commits into from
Nov 27, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 25 additions & 33 deletions bin/scripts/expire-data/countly_multi_app_expireData.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Setup TTL indexes to delete older data for one specific app. This script should be run periodically, to create TTL indexes on new collections too, like new events, etc for specific app
* Sets index on cd field if it does not exists and stores retention period in database. Nightly job will clear data based on set retention period.
* Server: countly
* Path: countly dir/bin/scripts/expire-data
* Command: node countly_multi_app_expireData.js
Expand Down Expand Up @@ -27,41 +27,19 @@ Promise.all([plugins.dbConnection("countly"), plugins.dbConnection("countly_dril
var dropIndex = false;
for (var i = 0; i < indexes.length; i++) {
if (indexes[i].name == INDEX_NAME) {
if (indexes[i].expireAfterSeconds == EXPIRE_AFTER) {
//print("skipping", c)
hasIndex = true;
}
//has index but incorrect expire time, need to be reindexed
else {
dropIndex = true;
}
hasIndex=true;
break;
}
}
if (dropIndex) {
console.log("modifying index", collection);
db_drill.command({
"collMod": collection,
"index": {
"keyPattern": {"cd": 1},
expireAfterSeconds: EXPIRE_AFTER
}
}, function(err) {
if (err) {
console.log(err);
}
done();
});

}
else if (!hasIndex) {
if (!hasIndex) {
console.log("creating index", collection);
db_drill.collection(collection).createIndex({"cd": 1}, {expireAfterSeconds: EXPIRE_AFTER, "background": true}, function() {
done();
db_drill.collection(collection).createIndex({"cd": 1}, function() {
done(true);
});
}
else {
done();
console.log("Appropriate index already set for", collection);
done(true);
}
}
else {
Expand All @@ -70,9 +48,23 @@ Promise.all([plugins.dbConnection("countly"), plugins.dbConnection("countly_dril
});


function done() {
db.close();
db_drill.close();
}
function done(index_set) {
if (index_set) {
db.collection("plugins").updateOne({"_id": "retention"}, {"$set": {"retention": EXPIRE_AFTER}}, {"upsert": true}, function(err) {
if (err) {
console.log("Error setting retention period", err);
}
else {
console.log("Retention period set: " + EXPIRE_AFTER);
}
db.close();
db_drill.close();

});
}
else {
db.close();
db_drill.close();
}
}
});
Loading