Shun Miyazawa 3 лет назад
Родитель
Сommit
87f5201728
1 измененных файлов с 12 добавлено и 13 удалено
  1. 12 13
      packages/app/src/server/service/activity.ts

+ 12 - 13
packages/app/src/server/service/activity.ts

@@ -52,23 +52,22 @@ class ActivityService {
     const collection = mongoose.connection.collection('activities');
 
     try {
-      const targetField = 'createdAt_1';
-      const indexes = await collection.getIndexes();
-      const isExistTargetField = Object.keys(indexes).includes(targetField);
-      if (isExistTargetField) {
-        await collection.dropIndex(targetField);
+      const indexes = await collection.indexes();
+      const ttlIndex = indexes.find(i => i.name === 'createdAt_1');
+      const shoudCreateIndex = ttlIndex == null;
+      const shoudDropAndCreateIndex = ttlIndex != null && ttlIndex.expireAfterSeconds !== activityExpirationSeconds;
+
+      if (shoudDropAndCreateIndex) {
+        await collection.dropIndex('createdAt_1');
       }
-    }
-    catch (err) {
-      logger.error('Failed to drop target index', err);
-      return;
-    }
 
-    try {
-      await collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: activityExpirationSeconds });
+      if (shoudDropAndCreateIndex || shoudCreateIndex) {
+        await collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: activityExpirationSeconds });
+      }
     }
     catch (err) {
-      logger.error('Failed to create TTL indexes', err);
+      logger.error('Failed to create TTL Index', err);
+      throw err;
     }
   };