Skip to content

Commit

Permalink
[heft] Handle errors when IPC messaging host (#4832)
Browse files Browse the repository at this point in the history
Co-authored-by: David Michon <[email protected]>
  • Loading branch information
dmichon-msft and dmichon-msft authored Jul 16, 2024
1 parent 835ca5b commit 4d62faa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/operation-graph",
"comment": "Handle errors when sending IPC messages to host.",
"type": "patch"
}
],
"packageName": "@rushstack/operation-graph"
}
20 changes: 17 additions & 3 deletions libraries/operation-graph/src/WatchLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ export class WatchLoop implements IWatchLoopState {
let runRequestedFromHost: boolean = true;
let status: OperationStatus = OperationStatus.Ready;

function tryMessageHost(
message: ISyncEventMessage | IRequestRunEventMessage | IAfterExecuteEventMessage
): void {
if (!host.send) {
return reject(new Error('Host does not support IPC'));
}

try {
host.send(message);
} catch (err) {
reject(new Error(`Unable to communicate with host: ${err}`));
}
}

function requestRunFromHost(requestor?: string): void {
if (runRequestedFromHost) {
return;
Expand All @@ -145,15 +159,15 @@ export class WatchLoop implements IWatchLoopState {
requestor
};

host.send!(requestRunMessage);
tryMessageHost(requestRunMessage);
}

function sendSync(): void {
const syncMessage: ISyncEventMessage = {
event: 'sync',
status
};
host.send!(syncMessage);
tryMessageHost(syncMessage);
}

host.on('message', async (message: CommandMessageFromHost) => {
Expand Down Expand Up @@ -189,7 +203,7 @@ export class WatchLoop implements IWatchLoopState {
event: 'after-execute',
status
};
host.send!(afterExecuteMessage);
tryMessageHost(afterExecuteMessage);
}
return;
}
Expand Down

0 comments on commit 4d62faa

Please sign in to comment.