lib/common/process/default/tmp-dir-cleaner.process.ts
Process class for cleaning the temporary directory.
Properties |
|
Methods |
constructor(logger: Logger, pmService: ProcessManagerService, cacheService: CacheService)
|
||||||||||||
Parameters :
|
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
|
Protected Async execute |
execute()
|
Inherited from
AbstractProcess
|
Defined in
AbstractProcess:42
|
Executes the process to clean the temporary directory.
Returns :
any
|
Private Async getStatus |
getStatus()
|
Inherited from
AbstractProcess
|
Defined in
AbstractProcess:165
|
Gets the current status of the process.
Returns :
unknown
The current process status. |
Protected Async onCrash | ||||||||
onCrash(error: Error)
|
||||||||
Inherited from
AbstractProcess
|
||||||||
Defined in
AbstractProcess:157
|
||||||||
Callback method that is called when the process crashes. Can be overridden by subclasses if needed.
Parameters :
Returns :
any
|
Protected Async onFinish |
onFinish()
|
Inherited from
AbstractProcess
|
Defined in
AbstractProcess:140
|
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
|
Defined in
AbstractProcess:148
|
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
|
||||||||
Defined in
AbstractProcess:173
|
||||||||
Sets the status of the process.
Parameters :
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 :
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 };
}
}