yohei0125 3 лет назад
Родитель
Сommit
5c20528697

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

@@ -11,6 +11,8 @@ import {
 import loggerFactory from '../../utils/logger';
 import loggerFactory from '../../utils/logger';
 import { ObjectIdLike } from '../interfaces/mongoose-utils';
 import { ObjectIdLike } from '../interfaces/mongoose-utils';
 
 
+const TIME_TO_ADD_SEC = 10;
+
 const logger = loggerFactory('growi:models:page-operation');
 const logger = loggerFactory('growi:models:page-operation');
 
 
 type IObjectId = mongoose.Types.ObjectId;
 type IObjectId = mongoose.Types.ObjectId;
@@ -55,6 +57,8 @@ export interface PageOperationModel extends Model<PageOperationDocument> {
   findByIdAndUpdatePageActionStage(pageOpId: ObjectIdLike, stage: PageActionStage): Promise<PageOperationDocumentHasId | null>
   findByIdAndUpdatePageActionStage(pageOpId: ObjectIdLike, stage: PageActionStage): Promise<PageOperationDocumentHasId | null>
   findMainOps(filter?: FilterQuery<PageOperationDocument>, projection?: any, options?: QueryOptions): Promise<PageOperationDocumentHasId[]>
   findMainOps(filter?: FilterQuery<PageOperationDocument>, projection?: any, options?: QueryOptions): Promise<PageOperationDocumentHasId[]>
   deleteByActionTypes(deleteTypeList: PageActionType[]): Promise<void>
   deleteByActionTypes(deleteTypeList: PageActionType[]): Promise<void>
+  isProcessable(pageOp: PageOperationDocument): boolean
+  extendExpiryDate(operationId: ObjectIdLike): Promise<void>
 }
 }
 
 
 const pageSchemaForResuming = new Schema<IPageForResuming>({
 const pageSchemaForResuming = new Schema<IPageForResuming>({
@@ -132,4 +136,17 @@ schema.statics.deleteByActionTypes = async function(
   logger.info(`Deleted all PageOperation documents with actionType: [${actionTypes}]`);
   logger.info(`Deleted all PageOperation documents with actionType: [${actionTypes}]`);
 };
 };
 
 
+schema.statics.isProcessable = function(pageOp: PageOperationDocument): boolean {
+  const { unprocessableExpiryDate } = pageOp;
+  return unprocessableExpiryDate == null || (unprocessableExpiryDate != null && new Date() > unprocessableExpiryDate);
+};
+
+/**
+ * add TIME_TO_ADD_SEC to current time and update unprocessableExpiryDate with it
+ */
+schema.statics.extendExpiryDate = async function(operationId: ObjectIdLike): Promise<void> {
+  const date = addSeconds(new Date(), TIME_TO_ADD_SEC);
+  await this.findByIdAndUpdate(operationId, { unprocessableExpiryDate: date });
+};
+
 export default getOrCreateModel<PageOperationDocument, PageOperationModel>('PageOperation', schema);
 export default getOrCreateModel<PageOperationDocument, PageOperationModel>('PageOperation', schema);

+ 2 - 17
packages/app/src/server/service/page-operation.ts

@@ -1,12 +1,10 @@
 import { pagePathUtils } from '@growi/core';
 import { pagePathUtils } from '@growi/core';
-import { addSeconds } from 'date-fns';
 
 
-import PageOperation, { PageActionType, PageOperationDocument } from '~/server/models/page-operation';
+import PageOperation, { PageActionType } from '~/server/models/page-operation';
 
 
 import { ObjectIdLike } from '../interfaces/mongoose-utils';
 import { ObjectIdLike } from '../interfaces/mongoose-utils';
 
 
 const { isEitherOfPathAreaOverlap, isPathAreaOverlap, isTrashPage } = pagePathUtils;
 const { isEitherOfPathAreaOverlap, isPathAreaOverlap, isTrashPage } = pagePathUtils;
-const TIME_TO_ADD_SEC = 10;
 const AUTO_UPDATE_INTERVAL_SEC = 5;
 const AUTO_UPDATE_INTERVAL_SEC = 5;
 
 
 class PageOperationService {
 class PageOperationService {
@@ -82,19 +80,6 @@ class PageOperationService {
     return true;
     return true;
   }
   }
 
 
-  isProcessable(pageOp: PageOperationDocument): boolean {
-    const { unprocessableExpiryDate } = pageOp;
-    return unprocessableExpiryDate == null || (unprocessableExpiryDate != null && new Date() > unprocessableExpiryDate);
-  }
-
-  /**
-   * add TIME_TO_ADD_SEC to current time and update unprocessableExpiryDate with it
-   */
-  async extendExpiryDate(operationId: ObjectIdLike): Promise<void> {
-    const date = addSeconds(new Date(), TIME_TO_ADD_SEC);
-    await PageOperation.findByIdAndUpdate(operationId, { unprocessableExpiryDate: date });
-  }
-
   /**
   /**
    * Set interval to update unprocessableExpiryDate every AUTO_UPDATE_INTERVAL_SEC seconds.
    * Set interval to update unprocessableExpiryDate every AUTO_UPDATE_INTERVAL_SEC seconds.
    * This is used to prevent the same page operation from being processed multiple times at once
    * This is used to prevent the same page operation from being processed multiple times at once
@@ -102,7 +87,7 @@ class PageOperationService {
   autoUpdateExpiryDate(operationId: ObjectIdLike): NodeJS.Timeout {
   autoUpdateExpiryDate(operationId: ObjectIdLike): NodeJS.Timeout {
     // https://github.com/Microsoft/TypeScript/issues/30128#issuecomment-651877225
     // https://github.com/Microsoft/TypeScript/issues/30128#issuecomment-651877225
     const timerObj = global.setInterval(async() => {
     const timerObj = global.setInterval(async() => {
-      await this.extendExpiryDate(operationId);
+      await PageOperation.extendExpiryDate(operationId);
     }, AUTO_UPDATE_INTERVAL_SEC * 1000);
     }, AUTO_UPDATE_INTERVAL_SEC * 1000);
     return timerObj;
     return timerObj;
   }
   }