generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor schema workflows to avoid extra operations and duplicate sch… #321
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,132 +1,73 @@ | ||
| import { DescribeTypeOutput } from '@aws-sdk/client-cloudformation'; | ||
| import { SettingsConfigurable, ISettingsSubscriber, SettingsSubscription } from '../settings/ISettingsSubscriber'; | ||
| import { DefaultSettings } from '../settings/Settings'; | ||
| import { LoggerFactory } from '../telemetry/LoggerFactory'; | ||
| import { Closeable } from '../utils/Closeable'; | ||
| import { AwsRegion } from '../utils/Region'; | ||
| import { AwsRegion, getRegion } from '../utils/Region'; | ||
| import { GetSamSchemaTask } from './GetSamSchemaTask'; | ||
| import { GetPrivateSchemasTask, GetPublicSchemaTask } from './GetSchemaTask'; | ||
| import { SchemaFileType } from './RegionalSchemas'; | ||
| import { CloudFormationResourceSchema } from './SamSchemaTransformer'; | ||
| import { SchemaStore } from './SchemaStore'; | ||
|
|
||
| const TenSeconds = 10 * 1000; | ||
| const OneHour = 60 * 60 * 1000; | ||
|
|
||
| export class GetSchemaTaskManager implements SettingsConfigurable, Closeable { | ||
| export class GetSchemaTaskManager { | ||
| private readonly processedRegions = new Set<AwsRegion>(); | ||
| private readonly tasks: GetPublicSchemaTask[] = []; | ||
| private readonly privateTask: GetPrivateSchemasTask; | ||
| private readonly samTask: GetSamSchemaTask; | ||
| private settingsSubscription?: SettingsSubscription; | ||
| private readonly log = LoggerFactory.getLogger(GetSchemaTaskManager); | ||
|
|
||
| private isRunning: boolean = false; | ||
|
|
||
| private readonly timeout: NodeJS.Timeout; | ||
| private readonly interval: NodeJS.Timeout; | ||
| private isRunning = false; | ||
|
|
||
| constructor( | ||
| private readonly schemas: SchemaStore, | ||
| private readonly getPublicSchemas: (region: AwsRegion) => Promise<SchemaFileType[]>, | ||
| getPrivateResources: () => Promise<DescribeTypeOutput[]>, | ||
| getSamSchemas: () => Promise<Map<string, CloudFormationResourceSchema>>, | ||
| private profile: string = DefaultSettings.profile.profile, | ||
| private readonly onSchemaUpdate: (region?: string, profile?: string) => void, | ||
| ) { | ||
| this.privateTask = new GetPrivateSchemasTask(getPrivateResources, () => this.profile); | ||
| this.privateTask = new GetPrivateSchemasTask(getPrivateResources); | ||
| this.samTask = new GetSamSchemaTask(getSamSchemas); | ||
|
|
||
| this.timeout = setTimeout(() => { | ||
| // Wait before trying to call CFN APIs so that credentials have time to update | ||
| this.runPrivateTask(); | ||
| }, TenSeconds); | ||
|
|
||
| this.interval = setInterval(() => { | ||
| // Keep private schemas up to date with credential changes if profile has not already ben loaded | ||
| this.runPrivateTask(); | ||
| }, OneHour); | ||
| } | ||
|
|
||
| configure(settingsManager: ISettingsSubscriber): void { | ||
| // Clean up existing subscription if present | ||
| if (this.settingsSubscription) { | ||
| this.settingsSubscription.unsubscribe(); | ||
| } | ||
|
|
||
| // Set initial settings | ||
| this.profile = settingsManager.getCurrentSettings().profile.profile; | ||
| addTask(reg: string, regionFirstCreatedMs?: number) { | ||
satyakigh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const region = getRegion(reg); | ||
|
|
||
| // Subscribe to profile settings changes | ||
| this.settingsSubscription = settingsManager.subscribe('profile', (newProfileSettings) => { | ||
| this.onSettingsChanged(newProfileSettings.profile); | ||
| }); | ||
| } | ||
| if (!this.processedRegions.has(region)) { | ||
| this.tasks.push(new GetPublicSchemaTask(region, this.getPublicSchemas, regionFirstCreatedMs)); | ||
| this.processedRegions.add(region); | ||
| } | ||
|
|
||
| private onSettingsChanged(newProfile: string): void { | ||
| this.profile = newProfile; | ||
| if (!this.isRunning) { | ||
| this.runNextTask(); | ||
| } | ||
| } | ||
|
|
||
| addTask(region: AwsRegion, regionFirstCreatedMs?: number) { | ||
| if (!this.currentRegionalTasks().has(region)) { | ||
| this.tasks.push(new GetPublicSchemaTask(region, this.getPublicSchemas, regionFirstCreatedMs)); | ||
| private runNextTask() { | ||
| const task = this.tasks.shift(); | ||
| if (!task) { | ||
| this.isRunning = false; | ||
| return; | ||
| } | ||
| this.startProcessing(); | ||
|
|
||
| this.isRunning = true; | ||
| task.run(this.schemas.publicSchemas) | ||
| .catch((err) => { | ||
| this.log.error(err); | ||
| this.tasks.push(task); | ||
| }) | ||
| .finally(() => { | ||
| this.isRunning = false; | ||
| this.runNextTask(); | ||
| }); | ||
| } | ||
|
|
||
| runPrivateTask() { | ||
| this.privateTask | ||
| .run(this.schemas.privateSchemas, this.log) | ||
| .then(() => { | ||
| this.onSchemaUpdate(undefined, this.profile); | ||
| }) | ||
| .catch(() => {}); | ||
| .run(this.schemas.privateSchemas) | ||
| .then(() => this.schemas.invalidate()) | ||
| .catch(this.log.error); | ||
| } | ||
|
|
||
| runSamTask() { | ||
| this.samTask | ||
| .run(this.schemas.samSchemas, this.log) | ||
| .then(() => { | ||
| this.onSchemaUpdate(); // No params = SAM update | ||
| }) | ||
| .catch(() => {}); | ||
| } | ||
|
|
||
| public currentRegionalTasks() { | ||
| return new Set(this.tasks.map((task) => task.region)); | ||
| } | ||
|
|
||
| private startProcessing() { | ||
| if (!this.isRunning && this.tasks.length > 0) { | ||
| this.isRunning = true; | ||
| this.run(); | ||
| } | ||
| } | ||
|
|
||
| private run() { | ||
| const task = this.tasks.shift(); | ||
| if (task) { | ||
| task.run(this.schemas.publicSchemas, this.log) | ||
| .then(() => { | ||
| this.onSchemaUpdate(task.region); | ||
| }) | ||
| .catch(() => { | ||
| this.tasks.push(task); | ||
| }) | ||
| .finally(() => { | ||
| this.isRunning = false; | ||
| this.startProcessing(); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| public close() { | ||
| // Unsubscribe from settings changes | ||
| if (this.settingsSubscription) { | ||
| this.settingsSubscription.unsubscribe(); | ||
| this.settingsSubscription = undefined; | ||
| } | ||
|
|
||
| clearTimeout(this.timeout); | ||
| clearInterval(this.interval); | ||
| .run(this.schemas.samSchemas) | ||
| .then(() => this.schemas.invalidate()) | ||
| .catch(this.log.error); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.