Browse Source

change function names and add detailed comments

yohei0125 3 years ago
parent
commit
2f6906a13b

+ 8 - 4
packages/app/src/server/crowi/index.js

@@ -150,9 +150,13 @@ Crowi.prototype.init = async function() {
   await this.autoInstall();
 };
 
-Crowi.prototype.afterInit = async function() {
+/**
+ * Execute functions that should be run after the express server is ready.
+ * Functions here does not block the
+ */
+Crowi.prototype.asyncAfterExpressServerReady = async function() {
   if (this.pageOperationService != null) {
-    this.pageOperationService.onAfterInit();
+    await this.pageOperationService.afterServiceReady();
   }
 };
 
@@ -477,7 +481,8 @@ Crowi.prototype.start = async function() {
   // setup Global Error Handlers
   this.setupGlobalErrorHandlers();
 
-  this.afterInit();
+  // Execute this asynchronously after the express server is ready so it does not block the ongoing process
+  this.asyncAfterExpressServerReady();
 
   return serverListening;
 };
@@ -697,7 +702,6 @@ Crowi.prototype.setupPageService = async function() {
   }
   if (this.pageOperationService == null) {
     this.pageOperationService = new PageOperationService(this);
-    // TODO: Remove this code when resuming feature is implemented
     await this.pageOperationService.init();
   }
 };

+ 15 - 5
packages/app/src/server/service/page-operation.ts

@@ -24,17 +24,27 @@ class PageOperationService {
   }
 
   async init(): Promise<void> {
-    // cleanup PageOperation documents
+    // cleanup PageOperation documents except ones with actionType: Rename
     const types = [Duplicate, Delete, DeleteCompletely, Revert, NormalizeParent];
     await PageOperation.deleteByActionTypes(types);
   }
 
-  async onAfterInit(): Promise<void> {
-    // execute rename operation
-    this.executeAllRenameOperationBySystem();
+  /**
+   * run programs that should be executed only after the service is ready
+   */
+  async afterServiceReady(): Promise<void> {
+    try {
+      // execute rename operation
+      await this.executeAllRenameOperationBySystem();
+    }
+    catch (err) {
+      logger.error(err);
+    }
   }
 
-  // execute renameSubOperation on every page operation order by ASC
+  /**
+   * execute renameSubOperation on every page operation for rename ordered by ASC
+   */
   private async executeAllRenameOperationBySystem(): Promise<void> {
     const Page = this.crowi.model('Page');