Skip to content

Commit a0a21b7

Browse files
d3xter666RandomByteKlattG
authored
[FEATURE] TaskUtil: Add 'force' flag to cleanup task callback (#677)
JIRA: CPOUI5FOUNDATION-751 \@ui5/builder PR that depends on this change: SAP/ui5-builder#953 When cleaning up after tasks run, it can happen in couple cases: - When the project is build - When there's a SIGTERM signal - (Maybe) on some error We need to be able to distinguish between those cases as in some places we rely on graceful termination and cleanup. Especially, after the project has been build successfully. On the other hand when the user presses `Ctrl + C`, for example, we need to enforce the termination. This cahnge provides this context. --------- Co-authored-by: Merlin Beutlberger <[email protected]> Co-authored-by: Günter Klatt <[email protected]>
1 parent 7824bef commit a0a21b7

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

lib/build/ProjectBuilder.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,17 +388,18 @@ class ProjectBuilder {
388388
}));
389389
}
390390

391-
async _executeCleanupTasks() {
391+
async _executeCleanupTasks(force) {
392392
this.#log.info("Executing cleanup tasks...");
393-
await this._buildContext.executeCleanupTasks();
393+
394+
await this._buildContext.executeCleanupTasks(force);
394395
}
395396

396397
_registerCleanupSigHooks() {
397398
const that = this;
398399
function createListener(exitCode) {
399400
return function() {
400401
// Asynchronously cleanup resources, then exit
401-
that._executeCleanupTasks().then(() => {
402+
that._executeCleanupTasks(true).then(() => {
402403
process.exit(exitCode);
403404
});
404405
};

lib/build/helpers/BuildContext.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ class BuildContext {
8080
return projectBuildContext;
8181
}
8282

83-
async executeCleanupTasks() {
83+
async executeCleanupTasks(force = false) {
8484
await Promise.all(this._projectBuildContexts.map((ctx) => {
85-
return ctx.executeCleanupTasks();
85+
return ctx.executeCleanupTasks(force);
8686
}));
8787
}
8888
}

lib/build/helpers/ProjectBuildContext.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class ProjectBuildContext {
4747
this._queues.cleanup.push(callback);
4848
}
4949

50-
async executeCleanupTasks() {
50+
async executeCleanupTasks(force) {
5151
await Promise.all(this._queues.cleanup.map((callback) => {
52-
return callback();
52+
return callback(force);
5353
}));
5454
}
5555

lib/build/helpers/TaskUtil.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ class TaskUtil {
163163
return this._projectBuildContext.getOption(key);
164164
}
165165

166+
/**
167+
* Callback that is executed once the build has finished
168+
*
169+
* @public
170+
* @callback @ui5/project/build/helpers/TaskUtil~cleanupTaskCallback
171+
* @param {boolean} force Whether the cleanup callback should
172+
* gracefully wait for certain jobs to be completed (<code>false</code>)
173+
* or enforce immediate termination (<code>true</code>)
174+
*/
175+
166176
/**
167177
* Register a function that must be executed once the build is finished. This can be used to, for example,
168178
* clean up files temporarily created on the file system. If the callback returns a Promise, it will be waited for.
@@ -172,7 +182,8 @@ class TaskUtil {
172182
* This method is only available to custom task extensions defining
173183
* <b>Specification Version 2.2 and above</b>.
174184
*
175-
* @param {Function} callback Callback to register. If it returns a Promise, it will be waited for
185+
* @param {@ui5/project/build/helpers/TaskUtil~cleanupTaskCallback} callback Callback to
186+
* register; it will be waited for if it returns a Promise
176187
* @public
177188
*/
178189
registerCleanupTask(callback) {

test/lib/build/ProjectBuilder.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,16 @@ test("_executeCleanupTasks", async (t) => {
727727
const executeCleanupTasksStub = sinon.stub(builder._buildContext, "executeCleanupTasks");
728728
await builder._executeCleanupTasks();
729729
t.is(executeCleanupTasksStub.callCount, 1, "BuildContext#executeCleanupTasks got called once");
730-
t.deepEqual(executeCleanupTasksStub.getCall(0).args, [],
731-
"BuildContext#executeCleanupTasks got called with no arguments");
730+
t.deepEqual(executeCleanupTasksStub.getCall(0).args, [undefined],
731+
"BuildContext#executeCleanupTasks got called with correct arguments");
732+
733+
// reset stub
734+
executeCleanupTasksStub.reset();
735+
// Call with enforcement flag
736+
await builder._executeCleanupTasks(true);
737+
t.is(executeCleanupTasksStub.callCount, 1, "BuildContext#executeCleanupTasks got called once");
738+
t.deepEqual(executeCleanupTasksStub.getCall(0).args, [true],
739+
"BuildContext#executeCleanupTasks got called with correct arguments");
732740
});
733741

734742
test("instantiate new logger for every ProjectBuilder", async (t) => {

test/lib/build/helpers/BuildContext.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,15 @@ test("executeCleanupTasks", async (t) => {
203203

204204
t.is(executeCleanupTasks.callCount, 2,
205205
"Project context executeCleanupTasks got called twice");
206+
t.is(executeCleanupTasks.getCall(0).firstArg, false,
207+
"Project context executeCleanupTasks got called with expected arguments");
208+
209+
210+
executeCleanupTasks.reset();
211+
await buildContext.executeCleanupTasks(true);
212+
213+
t.is(executeCleanupTasks.callCount, 2,
214+
"Project context executeCleanupTasks got called twice");
215+
t.is(executeCleanupTasks.getCall(0).firstArg, true,
216+
"Project context executeCleanupTasks got called with expected arguments");
206217
});

0 commit comments

Comments
 (0)