Skip to content

Commit 34b0b1f

Browse files
committed
refactor(project): Improve abort signal handling
1 parent 2f89bae commit 34b0b1f

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

packages/project/lib/build/BuildServer.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import WatchHandler from "./helpers/WatchHandler.js";
55
import {getLogger} from "@ui5/logger";
66
const log = getLogger("build:BuildServer");
77

8+
class AbortBuildError extends Error {
9+
constructor(message) {
10+
super(message);
11+
this.name = "AbortBuildError";
12+
}
13+
};
14+
815
/**
916
* Development server that provides access to built project resources with automatic rebuilding
1017
*
@@ -255,7 +262,7 @@ class BuildServer extends EventEmitter {
255262
#projectResourceChangedLive(project, fileAddedOrRemoved) {
256263
for (const {project: affectedProject} of this.#graph.traverseDependents(project.getName(), true)) {
257264
const projectBuildStatus = this.#projectBuildStatus.get(affectedProject.getName());
258-
projectBuildStatus.abortBuild("Source files changed");
265+
projectBuildStatus.abortBuild(new AbortBuildError(`Source change in project '${project.getName()}'`));
259266
if (fileAddedOrRemoved) {
260267
// Reset any cached readers in case files were added or removed
261268
projectBuildStatus.resetReaderCache();
@@ -353,13 +360,9 @@ class BuildServer extends EventEmitter {
353360
// Project has been built and result can be used
354361
const projectBuildStatus = this.#projectBuildStatus.get(projectName);
355362
projectBuildStatus.setReader(project.getReader({style: "runtime"}));
356-
});
357-
358-
try {
359-
const builtProjects = await buildPromise;
360-
this.emit("buildFinished", builtProjects);
361-
} catch (err) {
362-
if (err.name === "AbortError") {
363+
}).catch((err) => {
364+
if (err instanceof AbortBuildError) {
365+
log.info("Build aborted");
363366
// Build was aborted - do not log as error
364367
// Re-queue any outstanding projects
365368
for (const projectName of projectsToBuild) {
@@ -378,10 +381,12 @@ class BuildServer extends EventEmitter {
378381
// Re-throw to be handled by caller
379382
throw err;
380383
}
381-
} finally {
382-
// Clear active build
383-
this.#activeBuild = null;
384-
}
384+
});
385+
386+
const builtProjects = await buildPromise;
387+
this.emit("buildFinished", builtProjects);
388+
// Clear active build
389+
this.#activeBuild = null;
385390
if (signal.aborted) {
386391
log.verbose(`Build aborted for projects: ${projectsToBuild.join(", ")}`);
387392
return;
@@ -405,7 +410,7 @@ class ProjectBuildStatus {
405410
invalidate() {
406411
this.#state = PROJECT_STATES.INVALIDATED;
407412
// Ensure any running build is aborted. Then reset the abort controller
408-
this.#abortController.abort();
413+
this.#abortController.abort(new AbortBuildError("Project invalidated"));
409414
this.#abortController = new AbortController();
410415
}
411416

packages/project/lib/build/ProjectBuilder.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,10 @@ class ProjectBuilder {
304304
}
305305

306306
const cleanupSigHooks = this._registerCleanupSigHooks();
307+
const pCacheWrites = [];
307308
try {
308309
const startTime = process.hrtime();
309-
const pCacheWrites = [];
310310
while (queue.length) {
311-
signal?.throwIfAborted();
312311
const projectBuildContext = queue.shift();
313312
const project = projectBuildContext.getProject();
314313
const projectName = project.getName();
@@ -327,6 +326,7 @@ class ProjectBuilder {
327326
await this._buildProject(projectBuildContext);
328327
}
329328
}
329+
signal?.throwIfAborted();
330330

331331
if (projectBuiltCallback && requestedProjects.includes(projectName)) {
332332
projectBuiltCallback(projectName, project, projectBuildContext);
@@ -337,16 +337,12 @@ class ProjectBuilder {
337337
pCacheWrites.push(projectBuildContext.writeBuildCache());
338338
}
339339
}
340-
await Promise.all(pCacheWrites);
341340
this.#log.info(`Build succeeded in ${this._getElapsedTime(startTime)}`);
342341
} catch (err) {
343-
if (err.name === "AbortError") {
344-
this.#log.info(`Build aborted. Reason: ${err.message}`);
345-
} else {
346-
this.#log.error(`Build failed`);
347-
}
342+
this.#log.error(`Build failed`);
348343
throw err;
349344
} finally {
345+
await Promise.all(pCacheWrites);
350346
this._deregisterCleanupSigHooks(cleanupSigHooks);
351347
await this._executeCleanupTasks();
352348
this.#buildIsRunning = false;

0 commit comments

Comments
 (0)