Просмотр исходного кода

remove unnecessary values and fix test

yohei0125 4 лет назад
Родитель
Сommit
a46237a190

+ 0 - 1
packages/app/src/server/models/page-operation.ts

@@ -31,7 +31,6 @@ export type PageActionStage = typeof PageActionStage[keyof typeof PageActionStag
 export const PageOperationAutoUpdateTimerType = {
   ExtendSec: 5, // add this second(s) to current time
   IntervalSec: 5, // every this second(s)
-  SelfStopSec: 20, // execute self-stop after this second(s)
 } as const;
 
 /*

+ 18 - 18
packages/app/src/server/service/page.ts

@@ -1,5 +1,6 @@
 import pathlib from 'path';
 import { Readable, Writable } from 'stream';
+import { clearInterval } from 'timers';
 
 import { pagePathUtils, pathUtils } from '@growi/core';
 import { addSeconds } from 'date-fns';
@@ -31,6 +32,7 @@ import { PageRedirectModel } from '../models/page-redirect';
 import { serializePageSecurely } from '../models/serializers/page-serializer';
 import Subscription from '../models/subscription';
 import ActivityDefine from '../util/activityDefine';
+import { lookup } from '../util/i18nUserSettingDetector';
 
 const debug = require('debug')('growi:services:page');
 
@@ -403,11 +405,10 @@ class PageService {
    * @param pageOperationId
    * @param extendTimeSec time to add on current time every intervalSec
    * @param intervalSec interval time in sec
-   * @param selfStopSec time to self-stop in case setInterval kept alive as a zombie process
    * @returns tiemr id
    */
   async setIntervalUpdatePageOperationExpiryDate(
-      pageOperationId: ObjectIdLike, extendTimeSec: number, intervalSec: number, selfStopSec: number,
+      pageOperationId: ObjectIdLike, extendTimeSec: number, intervalSec: number,
   ): Promise<TSetInterval> {
     const pageOp = await PageOperation.findByIdAndUpdate(pageOperationId, { unprocessableExpiryDate: addSeconds(new Date(), extendTimeSec) }, { new: true });
     if (pageOp == null) throw Error('setinterval cannot be set as page operation is not found');
@@ -418,20 +419,11 @@ class PageService {
       logger.info(`property unprocessableExpiryDate of page operation(${pageOperationId}) is updated`);
     }, intervalSec * 1000);
 
-    this.setTimeoutToStopSetInterval(timerId, selfStopSec);
-
     logger.info(`autoUpdateInterval(${timerId}) for page operation process is set.`);
 
     return timerId;
   }
 
-  setTimeoutToStopSetInterval(timerId:TSetInterval, selfStopSec:number): TSetTimeout {
-    return setTimeout(() => {
-      clearInterval(timerId);
-      logger.warn('executed self-stop setInterval in case it keeps alive as a zombie process');
-    }, selfStopSec * 1000);
-  }
-
   async renamePage(page, newPagePath, user, options) {
     /*
      * Common Operation
@@ -489,11 +481,19 @@ class PageService {
     }
     const extendTimeSec = PageOperationAutoUpdateTimerType.ExtendSec;
     const intervalSec = PageOperationAutoUpdateTimerType.IntervalSec;
-    const selfStopSec = PageOperationAutoUpdateTimerType.SelfStopSec;
-    // Keep updating the property unprocessableExpiryDate until the renaming process is complete
-    // to avoid PageOperation being processed by multiple processes.
-    const autoUpdateIntervalTimerId = await this.setIntervalUpdatePageOperationExpiryDate(pageOp._id, extendTimeSec, intervalSec, selfStopSec);
-    const renamedPage = await this.renameMainOperation(page, newPagePath, user, options, pageOp._id, autoUpdateIntervalTimerId);
+
+    const autoUpdateIntervalTimerId = await this.setIntervalUpdatePageOperationExpiryDate(pageOp._id, extendTimeSec, intervalSec);
+
+    let renamedPage;
+    try {
+      renamedPage = await this.renameMainOperation(page, newPagePath, user, options, pageOp._id, autoUpdateIntervalTimerId);
+    }
+    catch (err) {
+      clearInterval(autoUpdateIntervalTimerId);
+      logger.info(`autoUpdateInterval(${autoUpdateIntervalTimerId}) is now cleared.`);
+      logger.warn(`renameMainOperation Failed: ${err}`);
+      throw err;
+    }
 
     return renamedPage;
   }
@@ -601,6 +601,7 @@ class PageService {
     catch (err) {
       // clear interval if failed
       clearInterval(autoUpdateIntervalTimerId);
+      logger.info(`autoUpdateInterval(${autoUpdateIntervalTimerId}) is now cleared.`);
       logger.error('renameDescendantsWithStream Failed:', err);
       throw Error(err);
     }
@@ -647,8 +648,7 @@ class PageService {
 
     const extendTimeSec = PageOperationAutoUpdateTimerType.ExtendSec;
     const intervalSec = PageOperationAutoUpdateTimerType.IntervalSec;
-    const selfStopSec = PageOperationAutoUpdateTimerType.SelfStopSec;
-    const autoUpdateIntervalTimerId = await this.setIntervalUpdatePageOperationExpiryDate(pageOperation._id, extendTimeSec, intervalSec, selfStopSec);
+    const autoUpdateIntervalTimerId = await this.setIntervalUpdatePageOperationExpiryDate(pageOperation._id, extendTimeSec, intervalSec);
     await this.renameSubOperation(page, toPath, user, options, renamedPage, pageOperation._id, autoUpdateIntervalTimerId);
   }
 

+ 0 - 2
packages/app/test/integration/service/v5.page.test.ts

@@ -223,14 +223,12 @@ describe('Test page service methods', () => {
   describe('restart renameOperation', () => {
     const resumePageRenameOperation = async(pageOperationId) => {
       const mockedRenameSubOperation = jest.spyOn(crowi.pageService, 'renameSubOperation').mockReturnValue(null);
-      const mockedSetTimeoutToStopSetInterval = jest.spyOn(crowi.pageService, 'setTimeoutToStopSetInterval').mockReturnValue(null);
 
       await crowi.pageService.resumePageRenameOperation(pageOperationId);
 
       const argsForRenameSubOperation = mockedRenameSubOperation.mock.calls[0];
 
       mockedRenameSubOperation.mockRestore();
-      mockedSetTimeoutToStopSetInterval.mockRestore();
       await crowi.pageService.renameSubOperation(...argsForRenameSubOperation);
     };
     test('it should successfully restart rename operation', async() => {