Skip to content

Commit 6028eae

Browse files
feat: improve cleanup service
Currently it is possible just to add new actions to be executed in the cleanup. However, in some cases, we want to add action and remove it after that. Also, we need to delete some lock files when CLI is not using them anymore. So, introduce new API in the cleanup service that allow all of these actions.
1 parent 4d848ab commit 6028eae

File tree

5 files changed

+117
-4
lines changed

5 files changed

+117
-4
lines changed

lib/definitions/cleanup-service.d.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,34 @@ interface ICleanupService extends IShouldDispose, IDisposable {
1010
*/
1111
addCleanupAction(action: ICleanupAction): Promise<void>;
1212

13+
/**
14+
* Remove action to be executed when CLI process exits.
15+
* NOTE: The action should be added in the action list by calling `addCleanupAction` first.
16+
* @param {ICleanupAction} action The action that should be removed from cleanup execution, including command and args.
17+
* @returns {Promise<void>}
18+
*/
19+
removeCleanupAction(action: ICleanupAction): Promise<void>
20+
1321
/**
1422
* Sets the file in which the cleanup process will write its logs.
1523
* This method must be called before starting the cleanup process, i.e. when CLI is initialized.
1624
* @param {string} filePath Path to file where the logs will be written. The logs are appended to the passed file.
1725
* @returns {void}
1826
*/
19-
setCleanupLogFile(filePath: string): void
27+
setCleanupLogFile(filePath: string): void;
28+
29+
/**
30+
* Adds file/dir to be deleted once CLI process exits.
31+
* @param {string} filePath Path to file/directory to be deleted.
32+
* @returns {Promise<void>}
33+
*/
34+
addCleanupDeleteAction(filePath: string): Promise<void>;
35+
36+
/**
37+
* Removes file/dir from the list of files to be deleted once CLI process exits.
38+
* NOTE: The file should be first added with `addCleanupDeleteAction`
39+
* @param {string} filePath Path to file/directory to be removed from the list of files to be deleted.
40+
* @returns {Promise<void>}
41+
*/
42+
removeCleanupDeleteAction(filePath: string): Promise<void>;
2043
}

lib/detached-processes/cleanup-process-definitions.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ interface ICleanupProcessMessage {
1919
* Type of the action
2020
*/
2121
actionType: CleanupProcessMessageType;
22+
}
2223

24+
interface ICleanupActionMessage extends ICleanupProcessMessage {
2325
/**
2426
* Describes the action that must be executed
2527
*/
2628
action: ICleanupAction;
2729
}
2830

31+
interface ICleanupDeleteActionMessage extends ICleanupProcessMessage {
32+
/**
33+
* Path to file/directory to be deleted.
34+
*/
35+
filePath: string;
36+
}

lib/detached-processes/cleanup-process.ts

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// NOTE: This file is used to clean up resources used by CLI, when the CLI is killed.
22
// The instances here are not shared with the ones in main CLI process.
33
import * as fs from "fs";
4+
import * as path from "path";
5+
import * as shelljs from "shelljs";
46
import { FileLogService } from "./file-log-service";
57

68
const pathToBootstrap = process.argv[2];
@@ -16,6 +18,7 @@ const fileLogService = $injector.resolve<IFileLogService>(FileLogService, { logF
1618
fileLogService.logData({ message: "Initializing Cleanup process." });
1719

1820
const actionsToExecute: ICleanupAction[] = [];
21+
const filesToDelete: string[] = [];
1922

2023
const executeCleanup = async () => {
2124
const $childProcess = $injector.resolve<IChildProcess>("childProcess");
@@ -31,16 +34,52 @@ const executeCleanup = async () => {
3134
}
3235
}
3336

37+
if (filesToDelete.length) {
38+
fileLogService.logData({ message: `Deleting files ${filesToDelete.join(" ")}` });
39+
shelljs.rm("-Rf", filesToDelete);
40+
}
41+
3442
fileLogService.logData({ message: `cleanup-process finished` });
3543
process.exit();
3644
};
3745

3846
const addCleanupAction = (newAction: ICleanupAction): void => {
39-
if (!_.some(actionsToExecute, currentAction => _.isEqual(currentAction, newAction))) {
47+
if (_.some(actionsToExecute, currentAction => _.isEqual(currentAction, newAction))) {
48+
fileLogService.logData({ message: `cleanup-process will not add action for execution as it has been added already: ${JSON.stringify(newAction)}` });
49+
} else {
4050
fileLogService.logData({ message: `cleanup-process added action for execution: ${JSON.stringify(newAction)}` });
4151
actionsToExecute.push(newAction);
52+
}
53+
};
54+
55+
const removeCleanupAction = (actionToRemove: ICleanupAction): void => {
56+
if (_.some(actionsToExecute, currentAction => _.isEqual(currentAction, actionToRemove))) {
57+
_.remove(actionsToExecute, currentAction => _.isEqual(currentAction, actionToRemove))
58+
fileLogService.logData({ message: `cleanup-process removed action for execution: ${JSON.stringify(actionToRemove)}` });
4259
} else {
43-
fileLogService.logData({ message: `cleanup-process will not add action for execution as it has been added already: ${JSON.stringify(newAction)}` });
60+
fileLogService.logData({ message: `cleanup-process cannot remove action for execution as it has note been added before: ${JSON.stringify(actionToRemove)}` });
61+
}
62+
};
63+
64+
const addDeleteAction = (filePath: string): void => {
65+
const fullPath = path.resolve(filePath);
66+
67+
if (_.some(filesToDelete, f => f === fullPath)) {
68+
fileLogService.logData({ message: `cleanup-process will not add ${fullPath} for deletion as it has been added already` });
69+
} else {
70+
filesToDelete.push(fullPath);
71+
fileLogService.logData({ message: `cleanup-process added ${fullPath} for deletion` });
72+
}
73+
};
74+
75+
const removeDeleteAction = (filePath: string): void => {
76+
const fullPath = path.resolve(filePath);
77+
78+
if (_.some(filesToDelete, f => f === fullPath)) {
79+
_.remove(filesToDelete, f => f === fullPath);
80+
fileLogService.logData({ message: `cleanup-process removed ${fullPath} from the list of files for deletion.` });
81+
} else {
82+
fileLogService.logData({ message: `cleanup-process cannot remove ${fullPath} for deletion as no such entry is found in the files marked for deletion` });
4483
}
4584
};
4685

@@ -49,7 +88,16 @@ process.on("message", async (cleanupProcessMessage: ICleanupProcessMessage) => {
4988

5089
switch (cleanupProcessMessage.actionType) {
5190
case CleanupProcessMessageType.AddCleanAction:
52-
addCleanupAction(cleanupProcessMessage.action);
91+
addCleanupAction((<ICleanupActionMessage>cleanupProcessMessage).action);
92+
break;
93+
case CleanupProcessMessageType.RemoveCleanAction:
94+
removeCleanupAction((<ICleanupActionMessage>cleanupProcessMessage).action);
95+
break;
96+
case CleanupProcessMessageType.AddDeleteAction:
97+
addDeleteAction((<ICleanupDeleteActionMessage>cleanupProcessMessage).filePath);
98+
break;
99+
case CleanupProcessMessageType.RemoveDeleteAction:
100+
removeDeleteAction((<ICleanupDeleteActionMessage>cleanupProcessMessage).filePath);
53101
break;
54102
default:
55103
fileLogService.logData({ message: `Unable to handle message of type ${cleanupProcessMessage.actionType}. Full message is ${JSON.stringify(cleanupProcessMessage)}`, type: FileLogMessageType.Error });

lib/detached-processes/detached-process-enums.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,24 @@ declare const enum FileLogMessageType {
2424
}
2525

2626
declare const enum CleanupProcessMessageType {
27+
/**
28+
* This type of action defines that cleanup procedure should execute specific action.
29+
*/
2730
AddCleanAction = "AddCleanAction",
31+
32+
/**
33+
* This type of action defines that cleanup procedure should not execute previously defined cleanup action.
34+
*/
35+
RemoveCleanAction = "RemoveCleanAction",
36+
37+
/**
38+
* This type of action defines that cleanup procedure should delete specified files.
39+
*/
40+
AddDeleteAction = "AddDeleteAction",
41+
42+
/**
43+
* This type of action defines the cleanup procedure should not delete previously specified file.
44+
*/
45+
RemoveDeleteAction = "RemoveDeleteAction",
46+
2847
}

lib/services/cleanup-service.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ export class CleanupService implements ICleanupService {
2020
cleanupProcess.send(<ICleanupProcessMessage>{ actionType: CleanupProcessMessageType.AddCleanAction, action });
2121
}
2222

23+
public async removeCleanupAction(action: ICleanupAction): Promise<void> {
24+
const cleanupProcess = await this.getCleanupProcess();
25+
cleanupProcess.send(<ICleanupProcessMessage>{ actionType: CleanupProcessMessageType.RemoveCleanAction, action });
26+
}
27+
28+
public async addCleanupDeleteAction(filePath: string): Promise<void> {
29+
const cleanupProcess = await this.getCleanupProcess();
30+
cleanupProcess.send(<ICleanupDeleteActionMessage>{ actionType: CleanupProcessMessageType.AddDeleteAction, filePath });
31+
}
32+
33+
public async removeCleanupDeleteAction(filePath: string): Promise<void> {
34+
const cleanupProcess = await this.getCleanupProcess();
35+
cleanupProcess.send(<ICleanupDeleteActionMessage>{ actionType: CleanupProcessMessageType.RemoveDeleteAction, filePath });
36+
}
37+
2338
@exported("cleanupService")
2439
public setCleanupLogFile(filePath: string): void {
2540
this.pathToCleanupLogFile = filePath;

0 commit comments

Comments
 (0)