-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathjob-queue.ts
225 lines (197 loc) · 7.87 KB
/
job-queue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { ArnFormat, IResource, Lazy, Resource, Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { CfnJobQueue } from 'aws-cdk-lib/aws-batch';
import { IComputeEnvironment } from './compute-environment-base';
import { ISchedulingPolicy } from './scheduling-policy';
/**
* Represents a JobQueue
*/
export interface IJobQueue extends IResource {
/**
* The name of the job queue. It can be up to 128 letters long.
* It can contain uppercase and lowercase letters, numbers, hyphens (-), and underscores (_)
*
* @attribute
*/
readonly jobQueueName: string
/**
* The ARN of this job queue
*
* @attribute
*/
readonly jobQueueArn: string;
/**
* The set of compute environments mapped to a job queue and their order relative to each other.
* The job scheduler uses this parameter to determine which compute environment runs a specific job.
* Compute environments must be in the VALID state before you can associate them with a job queue.
* You can associate up to three compute environments with a job queue.
* All of the compute environments must be either EC2 (EC2 or SPOT) or Fargate (FARGATE or FARGATE_SPOT);
* EC2 and Fargate compute environments can't be mixed.
*
* *Note*: All compute environments that are associated with a job queue must share the same architecture.
* AWS Batch doesn't support mixing compute environment architecture types in a single job queue.
*/
readonly computeEnvironments: OrderedComputeEnvironment[]
/**
* The priority of the job queue.
* Job queues with a higher priority are evaluated first when associated with the same compute environment.
* Priority is determined in descending order.
* For example, a job queue with a priority value of 10 is given scheduling preference over a job queue with a priority value of 1.
*/
readonly priority: number
/**
* If the job queue is enabled, it is able to accept jobs.
* Otherwise, new jobs can't be added to the queue, but jobs already in the queue can finish.
*
* @default true
*/
readonly enabled?: boolean
/**
* The SchedulingPolicy for this JobQueue. Instructs the Scheduler how to schedule different jobs.
*
* @default - no scheduling policy
*/
readonly schedulingPolicy?: ISchedulingPolicy
/**
* Add a `ComputeEnvironment` to this Queue.
* The Queue will prefer lower-order `ComputeEnvironment`s.
*/
addComputeEnvironment(computeEnvironment: IComputeEnvironment, order: number): void;
}
/**
* Props to configure a JobQueue
*/
export interface JobQueueProps {
/**
* The set of compute environments mapped to a job queue and their order relative to each other.
* The job scheduler uses this parameter to determine which compute environment runs a specific job.
* Compute environments must be in the VALID state before you can associate them with a job queue.
* You can associate up to three compute environments with a job queue.
* All of the compute environments must be either EC2 (EC2 or SPOT) or Fargate (FARGATE or FARGATE_SPOT);
* EC2 and Fargate compute environments can't be mixed.
*
* *Note*: All compute environments that are associated with a job queue must share the same architecture.
* AWS Batch doesn't support mixing compute environment architecture types in a single job queue.
*
* @default none
*/
readonly computeEnvironments?: OrderedComputeEnvironment[]
/**
* The priority of the job queue.
* Job queues with a higher priority are evaluated first when associated with the same compute environment.
* Priority is determined in descending order.
* For example, a job queue with a priority of 10 is given scheduling preference over a job queue with a priority of 1.
*
* @default 1
*/
readonly priority?: number
/**
* The name of the job queue. It can be up to 128 letters long.
* It can contain uppercase and lowercase letters, numbers, hyphens (-), and underscores (_)
*
* @default - no name
*/
readonly jobQueueName?: string
/**
* If the job queue is enabled, it is able to accept jobs.
* Otherwise, new jobs can't be added to the queue, but jobs already in the queue can finish.
*
* @default true
*/
readonly enabled?: boolean
/**
* The SchedulingPolicy for this JobQueue. Instructs the Scheduler how to schedule different jobs.
*
* @default - no scheduling policy
*/
readonly schedulingPolicy?: ISchedulingPolicy
}
/**
* Assigns an order to a ComputeEnvironment.
* The JobQueue will prioritize the lowest-order ComputeEnvironment.
*/
export interface OrderedComputeEnvironment {
/**
* The ComputeEnvironment to link to this JobQueue
*/
readonly computeEnvironment: IComputeEnvironment;
/**
* The order associated with `computeEnvironment`
*/
readonly order: number;
}
/**
* JobQueues can receive Jobs, which are removed from the queue when
* sent to the linked ComputeEnvironment(s) to be executed.
* Jobs exit the queue in FIFO order unless a `SchedulingPolicy` is linked.
*/
export class JobQueue extends Resource implements IJobQueue {
/**
* refer to an existing JobQueue by its arn
*/
public static fromJobQueueArn(scope: Construct, id: string, jobQueueArn: string): IJobQueue {
const stack = Stack.of(scope);
class Import extends Resource implements IJobQueue {
public readonly computeEnvironments = [];
public readonly priority = 1;
public readonly jobQueueArn = jobQueueArn;
public readonly jobQueueName = stack.splitArn(jobQueueArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName!;
public addComputeEnvironment(_computeEnvironment: IComputeEnvironment, _order: number): void {
throw new Error(`cannot add ComputeEnvironments to imported JobQueue '${id}'`);
}
}
return new Import(scope, id);
}
public readonly computeEnvironments: OrderedComputeEnvironment[]
public readonly priority: number
public readonly enabled?: boolean
public readonly schedulingPolicy?: ISchedulingPolicy
public readonly jobQueueArn: string;
public readonly jobQueueName: string;
constructor(scope: Construct, id: string, props?: JobQueueProps) {
super(scope, id, {
physicalName: props?.jobQueueName,
});
this.computeEnvironments = props?.computeEnvironments ?? [];
this.priority = props?.priority ?? 1;
this.enabled = props?.enabled;
this.schedulingPolicy = props?.schedulingPolicy;
const resource = new CfnJobQueue(this, id, {
computeEnvironmentOrder: Lazy.any({
produce: () => this.computeEnvironments.map((ce) => {
return {
computeEnvironment: ce.computeEnvironment.computeEnvironmentArn,
order: ce.order,
};
}),
}),
priority: this.priority,
jobQueueName: props?.jobQueueName,
state: (this.enabled ?? true) ? 'ENABLED' : 'DISABLED',
schedulingPolicyArn: this.schedulingPolicy?.schedulingPolicyArn,
});
this.jobQueueArn = this.getResourceArnAttribute(resource.attrJobQueueArn, {
service: 'batch',
resource: 'job-queue',
resourceName: this.physicalName,
});
this.jobQueueName = Stack.of(this).splitArn(this.jobQueueArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName!;
this.node.addValidation({ validate: () => validateOrderedComputeEnvironments(this.computeEnvironments) });
}
addComputeEnvironment(computeEnvironment: IComputeEnvironment, order: number): void {
this.computeEnvironments.push({
computeEnvironment,
order,
});
}
}
function validateOrderedComputeEnvironments(computeEnvironments: OrderedComputeEnvironment[]): string[] {
const seenOrders: number[] = [];
for (const ce of computeEnvironments) {
if (seenOrders.includes(ce.order)) {
return ['assigns the same order to different ComputeEnvironments'];
}
seenOrders.push(ce.order);
}
return seenOrders.length === 0 ? ['This JobQueue does not link any ComputeEnvironments'] : [];
}