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

init puppeteerCluster on app launch and cleanup html dir

Futa Arai 1 год назад
Родитель
Сommit
da13deaf6c

+ 8 - 0
apps/app/src/features/page-bulk-export/server/service/page-bulk-export-job-cron/index.ts

@@ -242,9 +242,17 @@ class PageBulkExportJobCronService extends CronService implements IPageBulkExpor
 
     const promises = [
       PageBulkExportPageSnapshot.deleteMany({ pageBulkExportJob }),
+      // delete /tmp/page-bulk-export/{jobId} dir
       fs.promises.rm(this.getTmpOutputDir(pageBulkExportJob), { recursive: true, force: true }),
     ];
 
+    if (pageBulkExportJob.format === PageBulkExportFormat.pdf) {
+      promises.push(
+        // delete /tmp/page-bulk-export/html/{jobId} dir
+        fs.promises.rm(this.getTmpOutputDir(pageBulkExportJob, true), { recursive: true, force: true }),
+      );
+    }
+
     const results = await Promise.allSettled(promises);
     results.forEach((result) => {
       if (result.status === 'rejected') logger.error(result.reason);

+ 1 - 1
apps/pdf-converter/package.json

@@ -11,7 +11,7 @@
     "start:prod:ci": "pnpm start:prod --ci",
     "start:prod": "node dist/index.js",
     "lint": "pnpm eslint **/*.{js,ts}",
-    "gen:client-code": "tsed run generate-swagger --output ./specs && orval",
+    "gen:client-code": "SWAGGER_GENERATION=true tsed run generate-swagger --output ./specs && orval",
     "build": "pnpm gen:client-code && tsc -p tsconfig.build.json"
   },
   "dependencies": {

+ 8 - 4
apps/pdf-converter/src/service/pdf-convert.ts

@@ -3,7 +3,7 @@ import path from 'path';
 import { Readable, Writable } from 'stream';
 import { pipeline as pipelinePromise } from 'stream/promises';
 
-import { Logger } from '@tsed/common';
+import { Logger, OnInit } from '@tsed/common';
 import { Inject, Service } from '@tsed/di';
 import { Cluster } from 'puppeteer-cluster';
 
@@ -33,7 +33,7 @@ interface JobInfo {
 }
 
 @Service()
-class PdfConvertService {
+class PdfConvertService implements OnInit {
 
   private puppeteerCluster: Cluster | undefined;
 
@@ -52,6 +52,12 @@ class PdfConvertService {
   @Inject()
     logger: Logger;
 
+  async $onInit(): Promise<void> {
+    if (process.env.SWAGGER_GENERATION === 'true') return;
+
+    await this.initPuppeteerCluster();
+  }
+
   /**
    * Register or update job inside jobList with given jobId, expirationDate, and status.
    * If job is new, start reading html files and convert them to pdf.
@@ -60,8 +66,6 @@ class PdfConvertService {
    * @param status status of job
    */
   async registerOrUpdateJob(jobId: string, expirationDate: Date, status: JobStatusSharedWithGrowi): Promise<void> {
-    if (this.puppeteerCluster == null) await this.initPuppeteerCluster();
-
     const isJobNew = !(jobId in this.jobList);
 
     if (isJobNew) {