forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): re-introduce
--concurrency
option (aws#21681)
Re-rolls aws#20345, after it had to be reverted in aws#21664. Includes the fix necessary to make sure that `--exclusively` works (aws#21663), and includes `cdk watch --concurrency` as well (aws#21598). Closes aws#21663.
- Loading branch information
1 parent
71678e4
commit b1ea299
Showing
11 changed files
with
494 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as cxapi from '@aws-cdk/cx-api'; | ||
import PQueue from 'p-queue'; | ||
|
||
type Options = { | ||
concurrency: number; | ||
deployStack: (stack: cxapi.CloudFormationStackArtifact) => Promise<void>; | ||
}; | ||
|
||
type DeploymentState = 'pending' | 'queued' | 'deploying' | 'completed' | 'failed' | 'skipped'; | ||
|
||
export const deployStacks = async (stacks: cxapi.CloudFormationStackArtifact[], { concurrency, deployStack }: Options): Promise<void> => { | ||
const queue = new PQueue({ concurrency }); | ||
const deploymentStates = stacks.reduce((acc, stack) => ({ ...acc, [stack.id]: 'pending' as const }), {} as Record<string, DeploymentState>); | ||
|
||
const isStackUnblocked = (stack: cxapi.CloudFormationStackArtifact) => | ||
stack.dependencies | ||
.map(({ id }) => id) | ||
.filter((id) => !id.endsWith('.assets')) | ||
.every((id) => !deploymentStates[id] || deploymentStates[id] === 'completed'); // Dependency not selected or already finished | ||
|
||
const hasAnyStackFailed = (states: Record<string, DeploymentState>) => Object.values(states).includes('failed'); | ||
|
||
const deploymentErrors: Error[] = []; | ||
|
||
const enqueueStackDeploys = () => { | ||
stacks.forEach(async (stack) => { | ||
if (deploymentStates[stack.id] === 'pending' && isStackUnblocked(stack)) { | ||
deploymentStates[stack.id] = 'queued'; | ||
|
||
await queue.add(async () => { | ||
// Do not start new deployments if any has already failed | ||
if (hasAnyStackFailed(deploymentStates)) { | ||
deploymentStates[stack.id] = 'skipped'; | ||
return; | ||
} | ||
|
||
deploymentStates[stack.id] = 'deploying'; | ||
|
||
await deployStack(stack).catch((err) => { | ||
// By recording the failure immediately as the queued task exits, we prevent the next | ||
// queued task from starting (its 'hasAnyStackFailed' will return 'true'). | ||
deploymentStates[stack.id] = 'failed'; | ||
throw err; | ||
}); | ||
|
||
deploymentStates[stack.id] = 'completed'; | ||
enqueueStackDeploys(); | ||
}).catch((err) => { | ||
deploymentStates[stack.id] = 'failed'; | ||
deploymentErrors.push(err); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
enqueueStackDeploys(); | ||
|
||
await queue.onIdle(); | ||
|
||
if (deploymentErrors.length) { | ||
throw Error(`Stack Deployments Failed: ${deploymentErrors}`); | ||
} | ||
|
||
// We shouldn't be able to get here, but check it anyway | ||
const neverUnblocked = Object.entries(deploymentStates).filter(([_, s]) => s === 'pending').map(([n, _]) => n); | ||
if (neverUnblocked.length > 0) { | ||
throw new Error(`The following stacks never became unblocked: ${neverUnblocked.join(', ')}. Please report this at https://github.com/aws/aws-cdk/issues`); | ||
} | ||
}; |
Oops, something went wrong.