|
|
@@ -1,4 +1,5 @@
|
|
|
import { getModelSafely } from '@growi/core';
|
|
|
+import mongoose from 'mongoose';
|
|
|
|
|
|
import { IActivity } from '~/interfaces/activity';
|
|
|
import { IPage } from '~/interfaces/page';
|
|
|
@@ -44,6 +45,35 @@ class ActivityService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ createTtlIndex = async function() {
|
|
|
+ const configManager = this.crowi.configManager;
|
|
|
+ const activityExpirationSeconds = configManager != null ? configManager.getConfig('crowi', 'app:activityExpirationSeconds') : 2592000;
|
|
|
+ const collection = mongoose.connection.collection('activities');
|
|
|
+
|
|
|
+ try {
|
|
|
+ const targetField = 'createdAt_1';
|
|
|
+
|
|
|
+ const indexes = await collection.indexes();
|
|
|
+ const foundCreatedAt = indexes.find(i => i.name === targetField);
|
|
|
+
|
|
|
+ const isNotSpec = foundCreatedAt?.expireAfterSeconds == null || foundCreatedAt?.expireAfterSeconds !== activityExpirationSeconds;
|
|
|
+ const shoudDropIndex = foundCreatedAt != null && isNotSpec;
|
|
|
+ const shoudCreateIndex = foundCreatedAt == null || shoudDropIndex;
|
|
|
+
|
|
|
+ if (shoudDropIndex) {
|
|
|
+ await collection.dropIndex(targetField);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (shoudCreateIndex) {
|
|
|
+ await collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: activityExpirationSeconds });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error('Failed to create TTL Index', err);
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* @param {object} parameters
|