Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aws-cdk): continue after exceptions in stack monitor #791

Merged
merged 6 commits into from
Sep 28, 2018
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
4 changes: 2 additions & 2 deletions packages/aws-cdk/lib/api/deploy-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function deployStack(stack: cxapi.SynthesizedStack,
const monitor = quiet ? undefined : new StackActivityMonitor(cfn, deployName, stack.metadata, changeSetDescription.Changes.length).start();
debug('Execution of changeset %s on stack %s has started; waiting for the update to complete...', changeSetName, deployName);
await waitForStack(cfn, deployName);
if (monitor) { monitor.stop(); }
if (monitor) { await monitor.stop(); }
debug('Stack %s has completed updating', deployName);
return { noOp: false, outputs: await getStackOutputs(cfn, deployName), stackArn: changeSet.StackId! };
}
Expand Down Expand Up @@ -127,7 +127,7 @@ export async function destroyStack(stack: cxapi.StackInfo, sdk: SDK, deployName?
const monitor = quiet ? undefined : new StackActivityMonitor(cfn, deployName).start();
await cfn.deleteStack({ StackName: deployName }).promise().catch(e => { throw e; });
const destroyedStack = await waitForStack(cfn, deployName, false);
if (monitor) { monitor.stop(); }
if (monitor) { await monitor.stop(); }
if (destroyedStack && destroyedStack.StackStatus !== 'DELETE_COMPLETE') {
const status = StackStatus.fromStackDescription(destroyedStack);
throw new Error(`Failed to destroy ${deployName}: ${status}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import cxapi = require('@aws-cdk/cx-api');
import aws = require('aws-sdk');
import colors = require('colors/safe');
import util = require('util');
import { error } from '../../../logging';

interface StackActivity {
readonly event: aws.CloudFormation.StackEvent;
Expand Down Expand Up @@ -59,6 +60,11 @@ export class StackActivityMonitor {
*/
private lastPrintTime = Date.now();

/**
* Set to the activity of reading the current events
*/
private readPromise?: Promise<AWS.CloudFormation.StackEvent[]>;

constructor(private readonly cfn: aws.CloudFormation,
private readonly stackName: string,
private readonly metadata?: cxapi.StackMetadata,
Expand All @@ -80,11 +86,17 @@ export class StackActivityMonitor {
return this;
}

public stop() {
public async stop() {
this.active = false;
if (this.tickTimer) {
clearTimeout(this.tickTimer);
}

if (this.readPromise) {
// We're currently reading events, wait for it to finish and print them before continuing.
await this.readPromise;
this.flushEvents();
}
}

private scheduleNextTick() {
Expand All @@ -99,8 +111,18 @@ export class StackActivityMonitor {
return;
}

await this.readEvents();
this.flushEvents();
try {
this.readPromise = this.readEvents();
await this.readPromise;
this.readPromise = undefined;

// We might have been stop()ped while the network call was in progress.
if (!this.active) { return; }

this.flushEvents();
} catch (e) {
error("Error occurred while monitoring stack: %s", e);
}
this.scheduleNextTick();
}

Expand Down Expand Up @@ -220,7 +242,7 @@ export class StackActivityMonitor {
return colors.reset;
}

private async readEvents(nextToken?: string) {
private async readEvents(nextToken?: string): Promise<AWS.CloudFormation.StackEvent[]> {
const output = await this.cfn.describeStackEvents({ StackName: this.stackName, NextToken: nextToken }).promise()
.catch( e => {
if (e.code === 'ValidationError' && e.message === `Stack [${this.stackName}] does not exist`) {
Expand Down