Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export class CloudWatchLogEventMonitor {
private readonly pollingInterval: number = 2_000;

public monitorId?: string;

private readonly ioHelper: IoHelper;
private currentTick?: NodeJS.Timeout;

constructor(props: CloudWatchLogEventMonitorProps) {
this.startTime = props.startTime?.getTime() ?? Date.now();
Expand All @@ -80,8 +82,8 @@ export class CloudWatchLogEventMonitor {
logGroupNames: this.logGroupNames(),
}));

// tick schedules the next tick as well
await this.tick();
this.scheduleNextTick();
}

/**
Expand All @@ -97,6 +99,7 @@ export class CloudWatchLogEventMonitor {
const oldMonitorId = this.monitorId!;
this.monitorId = undefined;
this.startTime = Date.now();
clearTimeout(this.currentTick);

await this.ioHelper.notify(IO.CDK_TOOLKIT_I5034.msg('Stopped monitoring log groups', {
monitor: oldMonitorId,
Expand Down Expand Up @@ -140,7 +143,7 @@ export class CloudWatchLogEventMonitor {
return;
}

setTimeout(() => void this.tick(), this.pollingInterval);
this.currentTick = setTimeout(() => void this.tick(), this.pollingInterval);
}

private async tick(): Promise<void> {
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,9 @@ export class Toolkit extends CloudAssemblySourceBuilder {

return {
async dispose() {
// stop the logs monitor, if it exists
await cloudWatchLogMonitor?.deactivate();
// close the watcher itself
await watcher.close();
// Prevents Node from staying alive. There is no 'end' event that the watcher emits
// that we can know it's definitely done, so best we can do is tell it to stop watching,
Expand Down
11 changes: 8 additions & 3 deletions packages/@aws-cdk/toolkit-lib/test/actions/watch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe('watch', () => {
// GIVEN
const cx = await builderFixture(toolkit, 'stack-with-role');
ioHost.level = 'debug';
await toolkit.watch(cx, {
const watcher = await toolkit.watch(cx, {
include: [],
traceLogs: true,
});
Expand All @@ -174,8 +174,13 @@ describe('watch', () => {
cloudWatchLogMonitor: expect.anything(), // Not undefined
}));

// Deactivate the cloudWatchLogMonitor that we created, otherwise the tests won't exit
(deploySpy.mock.calls[0]?.[2] as any).cloudWatchLogMonitor?.deactivate();
const logMonitorSpy = jest.spyOn((deploySpy.mock.calls[0]?.[2] as any).cloudWatchLogMonitor, 'deactivate');

// Deactivate the watcher and cloudWatchLogMonitor that we created, otherwise the tests won't exit
await watcher.dispose();

// ensure the log monitor has been closed
expect(logMonitorSpy).toHaveBeenCalled();
});

test('watch returns an object that can be used to stop the watch', async () => {
Expand Down
Loading