Skip to content

Commit 7429fd5

Browse files
fix: ensure all temp files are cleaned
Currently CLI relies on temp module to create temporary files, which should be deleted once CLI process has no work left. However, in some cases, when Node.js crashes (or the process is killed with a signal) the temp module is unable to delete the files. In order to fix this, introduce a tempService wrapper for temp module. For each path created by temp module, add a new cleanup action in the CLI's cleanup service, which will take care to delete the files even when the main CLI process dies with unusual error.
1 parent 755752a commit 7429fd5

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

lib/bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,4 @@ $injector.require("ipService", "./services/ip-service");
236236
$injector.require("jsonFileSettingsService", "./common/services/json-file-settings-service");
237237
$injector.require("markingModeService", "./services/marking-mode-service");
238238
$injector.require("metadataFilteringService", "./services/metadata-filtering-service");
239+
$injector.require("tempService", "./services/temp-service");

lib/definitions/temp-service.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Declares wrapped functions of temp module
3+
*/
4+
interface ITempService {
5+
mkdirSync(affixes: string): Promise<string>;
6+
path(options: ITempPathOptions): Promise<string>;
7+
}

lib/services/temp-service.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as temp from "temp";
2+
3+
export class TempService implements ITempService {
4+
constructor(private $cleanupService: ICleanupService) {
5+
temp.track();
6+
}
7+
8+
public async mkdirSync(affixes: string): Promise<string> {
9+
const pathToDir = temp.mkdirSync(affixes);
10+
await this.$cleanupService.addCleanupDeleteAction(pathToDir);
11+
return pathToDir;
12+
}
13+
14+
public async path(options: ITempPathOptions): Promise<string> {
15+
const pathToFile = temp.path(options);
16+
await this.$cleanupService.addCleanupDeleteAction(pathToFile);
17+
return pathToFile;
18+
}
19+
}
20+
21+
$injector.register("tempService", TempService);

0 commit comments

Comments
 (0)