|
1 | 1 | import { DescribeTypeOutput } from '@aws-sdk/client-cloudformation'; |
2 | | -import { SettingsConfigurable, ISettingsSubscriber, SettingsSubscription } from '../settings/ISettingsSubscriber'; |
3 | | -import { DefaultSettings } from '../settings/Settings'; |
4 | 2 | import { LoggerFactory } from '../telemetry/LoggerFactory'; |
5 | | -import { Closeable } from '../utils/Closeable'; |
6 | | -import { AwsRegion } from '../utils/Region'; |
| 3 | +import { AwsRegion, getRegion } from '../utils/Region'; |
7 | 4 | import { GetSamSchemaTask } from './GetSamSchemaTask'; |
8 | 5 | import { GetPrivateSchemasTask, GetPublicSchemaTask } from './GetSchemaTask'; |
9 | 6 | import { SchemaFileType } from './RegionalSchemas'; |
10 | 7 | import { CloudFormationResourceSchema } from './SamSchemaTransformer'; |
11 | 8 | import { SchemaStore } from './SchemaStore'; |
12 | 9 |
|
13 | | -const TenSeconds = 10 * 1000; |
14 | | -const OneHour = 60 * 60 * 1000; |
15 | | - |
16 | | -export class GetSchemaTaskManager implements SettingsConfigurable, Closeable { |
| 10 | +export class GetSchemaTaskManager { |
| 11 | + private readonly processedRegions = new Set<AwsRegion>(); |
17 | 12 | private readonly tasks: GetPublicSchemaTask[] = []; |
18 | 13 | private readonly privateTask: GetPrivateSchemasTask; |
19 | 14 | private readonly samTask: GetSamSchemaTask; |
20 | | - private settingsSubscription?: SettingsSubscription; |
21 | 15 | private readonly log = LoggerFactory.getLogger(GetSchemaTaskManager); |
22 | | - |
23 | | - private isRunning: boolean = false; |
24 | | - |
25 | | - private readonly timeout: NodeJS.Timeout; |
26 | | - private readonly interval: NodeJS.Timeout; |
| 16 | + private isRunning = false; |
27 | 17 |
|
28 | 18 | constructor( |
29 | 19 | private readonly schemas: SchemaStore, |
30 | 20 | private readonly getPublicSchemas: (region: AwsRegion) => Promise<SchemaFileType[]>, |
31 | 21 | getPrivateResources: () => Promise<DescribeTypeOutput[]>, |
32 | 22 | getSamSchemas: () => Promise<Map<string, CloudFormationResourceSchema>>, |
33 | | - private profile: string = DefaultSettings.profile.profile, |
34 | | - private readonly onSchemaUpdate: (region?: string, profile?: string) => void, |
35 | 23 | ) { |
36 | | - this.privateTask = new GetPrivateSchemasTask(getPrivateResources, () => this.profile); |
| 24 | + this.privateTask = new GetPrivateSchemasTask(getPrivateResources); |
37 | 25 | this.samTask = new GetSamSchemaTask(getSamSchemas); |
38 | | - |
39 | | - this.timeout = setTimeout(() => { |
40 | | - // Wait before trying to call CFN APIs so that credentials have time to update |
41 | | - this.runPrivateTask(); |
42 | | - }, TenSeconds); |
43 | | - |
44 | | - this.interval = setInterval(() => { |
45 | | - // Keep private schemas up to date with credential changes if profile has not already ben loaded |
46 | | - this.runPrivateTask(); |
47 | | - }, OneHour); |
48 | 26 | } |
49 | 27 |
|
50 | | - configure(settingsManager: ISettingsSubscriber): void { |
51 | | - // Clean up existing subscription if present |
52 | | - if (this.settingsSubscription) { |
53 | | - this.settingsSubscription.unsubscribe(); |
54 | | - } |
55 | | - |
56 | | - // Set initial settings |
57 | | - this.profile = settingsManager.getCurrentSettings().profile.profile; |
| 28 | + addTask(reg: string, regionFirstCreatedMs?: number) { |
| 29 | + const region = getRegion(reg); |
58 | 30 |
|
59 | | - // Subscribe to profile settings changes |
60 | | - this.settingsSubscription = settingsManager.subscribe('profile', (newProfileSettings) => { |
61 | | - this.onSettingsChanged(newProfileSettings.profile); |
62 | | - }); |
63 | | - } |
| 31 | + if (!this.processedRegions.has(region)) { |
| 32 | + this.tasks.push(new GetPublicSchemaTask(region, this.getPublicSchemas, regionFirstCreatedMs)); |
| 33 | + this.processedRegions.add(region); |
| 34 | + } |
64 | 35 |
|
65 | | - private onSettingsChanged(newProfile: string): void { |
66 | | - this.profile = newProfile; |
| 36 | + if (!this.isRunning) { |
| 37 | + this.runNextTask(); |
| 38 | + } |
67 | 39 | } |
68 | 40 |
|
69 | | - addTask(region: AwsRegion, regionFirstCreatedMs?: number) { |
70 | | - if (!this.currentRegionalTasks().has(region)) { |
71 | | - this.tasks.push(new GetPublicSchemaTask(region, this.getPublicSchemas, regionFirstCreatedMs)); |
| 41 | + private runNextTask() { |
| 42 | + const task = this.tasks.shift(); |
| 43 | + if (!task) { |
| 44 | + this.isRunning = false; |
| 45 | + return; |
72 | 46 | } |
73 | | - this.startProcessing(); |
| 47 | + |
| 48 | + this.isRunning = true; |
| 49 | + task.run(this.schemas.publicSchemas) |
| 50 | + .catch((err) => { |
| 51 | + this.log.error(err); |
| 52 | + this.tasks.push(task); |
| 53 | + }) |
| 54 | + .finally(() => { |
| 55 | + this.isRunning = false; |
| 56 | + this.runNextTask(); |
| 57 | + }); |
74 | 58 | } |
75 | 59 |
|
76 | 60 | runPrivateTask() { |
77 | 61 | this.privateTask |
78 | | - .run(this.schemas.privateSchemas, this.log) |
79 | | - .then(() => { |
80 | | - this.onSchemaUpdate(undefined, this.profile); |
81 | | - }) |
82 | | - .catch(() => {}); |
| 62 | + .run(this.schemas.privateSchemas) |
| 63 | + .then(() => this.schemas.invalidate()) |
| 64 | + .catch(this.log.error); |
83 | 65 | } |
84 | 66 |
|
85 | 67 | runSamTask() { |
86 | 68 | this.samTask |
87 | | - .run(this.schemas.samSchemas, this.log) |
88 | | - .then(() => { |
89 | | - this.onSchemaUpdate(); // No params = SAM update |
90 | | - }) |
91 | | - .catch(() => {}); |
92 | | - } |
93 | | - |
94 | | - public currentRegionalTasks() { |
95 | | - return new Set(this.tasks.map((task) => task.region)); |
96 | | - } |
97 | | - |
98 | | - private startProcessing() { |
99 | | - if (!this.isRunning && this.tasks.length > 0) { |
100 | | - this.isRunning = true; |
101 | | - this.run(); |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - private run() { |
106 | | - const task = this.tasks.shift(); |
107 | | - if (task) { |
108 | | - task.run(this.schemas.publicSchemas, this.log) |
109 | | - .then(() => { |
110 | | - this.onSchemaUpdate(task.region); |
111 | | - }) |
112 | | - .catch(() => { |
113 | | - this.tasks.push(task); |
114 | | - }) |
115 | | - .finally(() => { |
116 | | - this.isRunning = false; |
117 | | - this.startProcessing(); |
118 | | - }); |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - public close() { |
123 | | - // Unsubscribe from settings changes |
124 | | - if (this.settingsSubscription) { |
125 | | - this.settingsSubscription.unsubscribe(); |
126 | | - this.settingsSubscription = undefined; |
127 | | - } |
128 | | - |
129 | | - clearTimeout(this.timeout); |
130 | | - clearInterval(this.interval); |
| 69 | + .run(this.schemas.samSchemas) |
| 70 | + .then(() => this.schemas.invalidate()) |
| 71 | + .catch(this.log.error); |
131 | 72 | } |
132 | 73 | } |
0 commit comments