-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
taskScheduler.ts
168 lines (153 loc) · 4.98 KB
/
taskScheduler.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
import {Config} from './config';
import {ConfigParser} from './configParser';
export interface Task {
capabilities: any;
specs: Array<string>;
taskId: string;
done: any;
}
/**
* The taskScheduler keeps track of the spec files that needs to run next
* and which task is running what.
*/
export class TaskQueue {
numRunningInstances: number = 0;
maxInstance: number;
specsIndex: number = 0;
// A queue of specs for a particular capacity
constructor(public capabilities: any, public specLists: any) {
this.maxInstance = capabilities.maxInstances || 1;
}
}
export class TaskScheduler {
taskQueues: Array<TaskQueue>;
rotationIndex: number;
/**
* A scheduler to keep track of specs that need running and their associated
* capabilities. It will suggest a task (combination of capabilities and spec)
* to run while observing the following config rules:
* multiCapabilities, shardTestFiles, and maxInstance.
* Precondition: multiCapabilities is a non-empty array
* (capabilities and getCapabilities will both be ignored)
*
* @constructor
* @param {Object} config parsed from the config file
*/
constructor(private config: Config) {
let excludes = ConfigParser.resolveFilePatterns(
config.exclude, true, config.configDir);
let allSpecs =
ConfigParser
.resolveFilePatterns(
ConfigParser.getSpecs(config), false, config.configDir)
.filter((path: string) => {
return excludes.indexOf(path) < 0;
});
let taskQueues: Array<TaskQueue> = [];
config.multiCapabilities.forEach((capabilities) => {
let capabilitiesSpecs = allSpecs;
if (capabilities.specs) {
let capabilitiesSpecificSpecs = ConfigParser.resolveFilePatterns(
capabilities.specs, false, config.configDir);
capabilitiesSpecs = capabilitiesSpecs.concat(capabilitiesSpecificSpecs);
}
if (capabilities.exclude) {
let capabilitiesSpecExcludes = ConfigParser.resolveFilePatterns(
capabilities.exclude, true, config.configDir);
capabilitiesSpecs = capabilitiesSpecs.filter((path) => {
return capabilitiesSpecExcludes.indexOf(path) < 0;
});
}
let specLists: Array<Array<string>> = [];
// If we shard, we return an array of one element arrays, each containing
// the spec file. If we don't shard, we return an one element array
// containing an array of all the spec files
if (capabilities.shardTestFiles) {
capabilitiesSpecs.forEach((spec) => {
specLists.push([spec]);
});
} else {
specLists.push(capabilitiesSpecs);
}
capabilities.count = capabilities.count || 1;
for (var i = 0; i < capabilities.count; ++i) {
taskQueues.push(new TaskQueue(capabilities, specLists));
}
});
this.taskQueues = taskQueues;
this.rotationIndex = 0; // Helps suggestions to rotate amongst capabilities
}
/**
* Get the next task that is allowed to run without going over maxInstance.
*
* @return {{capabilities: Object, specs: Array.<string>, taskId: string,
* done: function()}}
*/
public nextTask(): Task {
for (var i = 0; i < this.taskQueues.length; ++i) {
var rotatedIndex = ((i + this.rotationIndex) % this.taskQueues.length);
var queue = this.taskQueues[rotatedIndex];
if (queue.numRunningInstances < queue.maxInstance &&
queue.specsIndex < queue.specLists.length) {
this.rotationIndex = rotatedIndex + 1;
++queue.numRunningInstances;
let taskId = '' + rotatedIndex + 1;
if (queue.specLists.length > 1) {
taskId += '-' + queue.specsIndex;
}
let specs = queue.specLists[queue.specsIndex];
++queue.specsIndex;
return {
capabilities: queue.capabilities,
specs: specs,
taskId: taskId,
done: function() {
--queue.numRunningInstances;
}
};
}
}
return null;
}
/**
* Get the number of tasks left to run or are currently running.
*
* @return {number}
*/
public numTasksOutstanding(): number {
var count = 0;
this.taskQueues.forEach((queue) => {
count += queue.numRunningInstances +
(queue.specLists.length - queue.specsIndex);
});
return count;
}
/**
* Get maximum number of concurrent tasks required/permitted.
*
* @return {number}
*/
public maxConcurrentTasks(): number {
if (this.config.maxSessions && this.config.maxSessions > 0) {
return this.config.maxSessions;
} else {
var count = 0;
this.taskQueues.forEach((queue) => {
count += Math.min(queue.maxInstance, queue.specLists.length);
});
return count;
}
}
/**
* Returns number of tasks currently running.
*
* @return {number}
*/
public countActiveTasks() {
var count = 0;
this.taskQueues.forEach((queue) => {
count += queue.numRunningInstances;
});
return count;
}
}