File

lib/common/process/default/tmp-dir-cleaner.process.ts

Description

Process class for cleaning the temporary directory.

Extends

AbstractProcess

Index

Properties
Methods

Constructor

constructor(logger: Logger, pmService: ProcessManagerService, cacheService: CacheService)
Parameters :
Name Type Optional
logger Logger No
pmService ProcessManagerService No
cacheService CacheService No

Properties

Protected Abstract Readonly logger
Type : Logger
Inherited from AbstractProcess
Defined in AbstractProcess:31
Private logInstance
Type : ProcessLogEntity
Inherited from AbstractProcess
Defined in AbstractProcess:33
Protected Abstract Readonly pmService
Type : ProcessManagerService
Inherited from AbstractProcess
Defined in AbstractProcess:32

Methods

Protected Async execute
execute()
Inherited from AbstractProcess
Defined in AbstractProcess:42

Executes the process to clean the temporary directory.

Returns : any
Private getDeleteStats
getDeleteStats(dirStruct: literal type)

Gets statistics on the number of files and folders to be deleted.

Parameters :
Name Type Optional Description
dirStruct literal type No
  • The structure of the directory.
Returns : { filesCount: number; foldersCount: number; }

An object containing the file and folder counts.

Private Async getStatus
getStatus()
Inherited from AbstractProcess

Gets the current status of the process.

Returns : unknown

The current process status.

Protected Async onCrash
onCrash(error: Error)
Inherited from AbstractProcess

Callback method that is called when the process crashes. Can be overridden by subclasses if needed.

Parameters :
Name Type Optional Description
error Error No
  • The error that caused the crash.
Returns : any
Protected Async onFinish
onFinish()
Inherited from AbstractProcess

Callback method that is called when the process finishes. Can be overridden by subclasses if needed.

Returns : any
Protected Async onStop
onStop()
Inherited from AbstractProcess

Callback method that is called when the process stops. Can be overridden by subclasses if needed.

Returns : any
Private Async setStatus
setStatus(status: Status)
Inherited from AbstractProcess

Sets the status of the process.

Parameters :
Name Type Optional Description
status Status No
  • The new status to set.
Returns : any
Async start
start()
Inherited from AbstractProcess
Defined in AbstractProcess:44

Starts the process and handles its execution.

Returns : any
Async stop
stop()
Inherited from AbstractProcess
Defined in AbstractProcess:80

Stops the process and updates its status.

Returns : any
Protected Async writeLog
writeLog(message: string, data?, level)
Inherited from AbstractProcess
Defined in AbstractProcess:94

Writes a log entry with the specified message, data, and log level.

Parameters :
Name Type Optional Default value Description
message string No
  • The log message.
data Yes
  • Optional additional data.
level No LogLevel.Log
  • The log level (default: LogLevel.Log).
Returns : any
import { AbstractProcess } from "../abstract-process";
import { Inject, Logger } from "@nestjs/common";
import { ProcessManagerService } from "../process-manager.service";
import * as fs from "fs";
import { KpConfig } from "../../../../gen-src/kp.config";
import { FilesUtils } from "../../../shared/utils/files.utils";
import { LOGGER } from "../../../shared/modules/log/log.constants";
import { CacheService } from "../../../shared/modules/cache/cache.types";
import readDirectoryRecursively = FilesUtils.readDirectoryRecursively;

/**
 * Process class for cleaning the temporary directory.
 */
export class TmpDirCleanerProcess extends AbstractProcess {
  constructor(
    @Inject(LOGGER) protected readonly logger: Logger,
    protected readonly pmService: ProcessManagerService,
    private readonly cacheService: CacheService,
  ) {
    super();
  }

  /**
   * Executes the process to clean the temporary directory.
   */
  protected async execute() {
    const tmpDir =
      process.cwd() + (await this.cacheService.get(KpConfig.TMP_DIR));
    if (!fs.existsSync(tmpDir)) {
      await this.writeLog(`Nothing to delete`);
      return;
    }
    const dirStruct = await readDirectoryRecursively(tmpDir);
    const stats = this.getDeleteStats(dirStruct as { [k: string]: string[] });
    await this.writeLog(
      `Try to delete ${stats.filesCount} files and ${stats.foldersCount} folders...`,
    );
    await fs.promises.rm(tmpDir, { recursive: true, force: true });
    await this.writeLog(`Tmp dir was cleaned`);
  }

  /**
   * Gets statistics on the number of files and folders to be deleted.
   * @param dirStruct - The structure of the directory.
   * @returns An object containing the file and folder counts.
   */
  private getDeleteStats(dirStruct: { [k: string]: string[] }) {
    let filesCount = 0;
    let foldersCount = 0;
    for (const key in dirStruct) {
      filesCount += dirStruct[key].length;
      if (!key.length) {
        continue;
      }
      foldersCount++;
    }
    return { filesCount, foldersCount };
  }
}

results matching ""

    No results matching ""