-
Notifications
You must be signed in to change notification settings - Fork 4
/
QuickServer.js
424 lines (357 loc) · 14.6 KB
/
QuickServer.js
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright 2022, University of Colorado Boulder
/**
* Coordinates continuous testing, and provides HTTP APIs for reports or clients that run browser tests.
*
* @author Jonathan Olson <[email protected]>
*/
const cloneMissingRepos = require( '../../../perennial/js/common/cloneMissingRepos' );
const deleteDirectory = require( '../../../perennial/js/common/deleteDirectory' );
const execute = require( '../../../perennial/js/common/execute' );
const getRepoList = require( '../../../perennial/js/common/getRepoList' );
const gitPull = require( '../../../perennial/js/common/gitPull' );
const gitRevParse = require( '../../../perennial/js/common/gitRevParse' );
const gruntCommand = require( '../../../perennial/js/common/gruntCommand' );
const isStale = require( '../../../perennial/js/common/isStale' );
const npmUpdate = require( '../../../perennial/js/common/npmUpdate' );
const puppeteerLoad = require( '../../../perennial/js/common/puppeteerLoad' );
const withServer = require( '../../../perennial/js/common/withServer' );
const assert = require( 'assert' );
const http = require( 'http' );
const _ = require( 'lodash' ); // eslint-disable-line require-statement-match
const path = require( 'path' );
const url = require( 'url' );
const winston = require( 'winston' );
const puppeteer = require( 'puppeteer' );
const sendSlackMessage = require( './sendSlackMessage' );
const ctqType = {
LINT: 'lint',
TSC: 'tsc',
TRANSPILE: 'transpile',
SIM_FUZZ: 'simFuzz',
STUDIO_FUZZ: 'studioFuzz'
};
// Headers that we'll include in all server replies
const jsonHeaders = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
};
// For now, provide an initial message every time, so treat it as broken when it starts
let lastBroken = false;
class QuickServer {
constructor( options ) {
options = {
rootDir: path.normalize( `${__dirname}/../../../` ),
isTestMode: false,
...options
};
// @public {*}
this.testingState = {};
// @public {string} - root of your GitHub working copy, relative to the name of the directory that the
// currently-executing script resides in
this.rootDir = options.rootDir;
// @public {boolean} - whether we are in testing mode. if true, tests are continuously forced to run
this.isTestMode = options.isTestMode;
// @private {string[]} - errors found in any given loop
this.errorMessages = [];
}
/**
* @public
*/
async startMainLoop() {
// Let it execute tests on startup once
let forceTests = true;
let count = 0;
// Launch the browser once and reuse it to generate new pages in puppeteerLoad
const browser = await puppeteer.launch( {
// With this flag, temp files are written to /tmp/ on bayes, which caused https://github.com/phetsims/aqua/issues/145
// /dev/shm/ is much bigger
ignoreDefaultArgs: [ '--disable-dev-shm-usage' ],
// Command line arguments passed to the chrome instance,
args: [
'--enable-precise-memory-info',
// To prevent filling up `/tmp`, see https://github.com/phetsims/aqua/issues/145
`--user-data-dir=${process.cwd()}/../tmp/puppeteerUserData/`
]
} );
const puppeteerOptions = {
waitAfterLoad: 10000,
allowedTimeToLoad: 120000,
puppeteerTimeout: 120000,
browser: browser
};
while ( true ) { // eslint-disable-line no-constant-condition
try {
winston.info( 'QuickServer: cycle start' );
const reposToCheck = getRepoList( 'active-repos' ).filter( repo => repo !== 'aqua' );
const timestamp = Date.now();
// TODO: don't be synchronous here
const staleRepos = [];
winston.info( 'QuickServer: checking stale' );
await Promise.all( reposToCheck.map( async repo => {
if ( await isStale( repo ) ) {
staleRepos.push( repo );
winston.info( `QuickServer: ${repo} stale` );
}
} ) );
if ( staleRepos.length || forceTests ) {
forceTests = this.isTestMode;
winston.info( `QuickServer: stale repos: ${staleRepos}` );
for ( const repo of staleRepos ) {
winston.info( `QuickServer: pulling ${repo}` );
await gitPull( repo );
}
winston.info( 'QuickServer: cloning missing repos' );
const clonedRepos = await cloneMissingRepos();
for ( const repo of [ ...staleRepos, ...clonedRepos ] ) {
if ( [ 'chipper', 'perennial', 'perennial-alias' ].includes( repo ) ) {
winston.info( `QuickServer: npm update ${repo}` );
await npmUpdate( repo );
}
}
winston.info( 'QuickServer: checking SHAs' );
const shas = {};
for ( const repo of reposToCheck ) {
shas[ repo ] = await gitRevParse( repo, 'master' );
}
winston.info( 'QuickServer: linting' );
const lintResult = await execute( gruntCommand, [ 'lint-everything', '--hide-progress-bar' ], `${this.rootDir}/perennial`, { errors: 'resolve' } );
// Periodically clean chipper/dist, but not on the first time for easier local testing
if ( count++ % 10 === 2 ) {
await deleteDirectory( `${this.rootDir}/chipper/dist` );
}
winston.info( 'QuickServer: tsc' );
const tscResult = await execute( '../../node_modules/typescript/bin/tsc', [], `${this.rootDir}/chipper/tsconfig/all`, { errors: 'resolve' } );
winston.info( 'QuickServer: transpiling' );
const transpileResult = await execute( 'node', [ 'js/scripts/transpile.js' ], `${this.rootDir}/chipper`, { errors: 'resolve' } );
winston.info( 'QuickServer: sim fuzz' );
let simFuzz = null;
try {
await withServer( async port => {
const url = `http://localhost:${port}/natural-selection/natural-selection_en.html?brand=phet&ea&debugger&fuzz`;
const error = await puppeteerLoad( url, puppeteerOptions );
if ( error ) {
simFuzz = error;
}
} );
}
catch( e ) {
simFuzz = e;
}
winston.info( 'QuickServer: studio fuzz' );
let studioFuzz = null;
try {
await withServer( async port => {
const url = `http://localhost:${port}/studio/index.html?sim=states-of-matter&phetioElementsDisplay=all&fuzz`;
const error = await puppeteerLoad( url, puppeteerOptions );
if ( error ) {
studioFuzz = error;
}
} );
}
catch( e ) {
studioFuzz = e;
}
const executeResultToOutput = ( result, name ) => {
return {
passed: result.code === 0,
// full length message, used when someone clicks on a quickNode in CT for error details
message: `code: ${result.code}\nstdout:\n${result.stdout}\nstderr:\n${result.stderr}`,
// trimmed down and separated error messages, used to track the state of individual errors and show
// abbreviated errors for the Slack CT Notifier
errorMessages: result.code === 0 ? [] : this.parseErrors( result.stdout, name )
};
};
const fuzzResultToOutput = ( result, name ) => {
if ( result === null ) {
return { passed: true, message: '', errorMessages: [] };
}
else {
return { passed: false, message: '' + result, errorMessages: this.parseErrors( result, name ) };
}
};
// This would take up too much space
transpileResult.stdout = '';
this.testingState = {
lint: executeResultToOutput( lintResult, ctqType.LINT ),
tsc: executeResultToOutput( tscResult, ctqType.TSC ),
transpile: executeResultToOutput( transpileResult, ctqType.TRANSPILE ),
simFuzz: fuzzResultToOutput( simFuzz, ctqType.SIM_FUZZ ),
studioFuzz: fuzzResultToOutput( studioFuzz, ctqType.STUDIO_FUZZ ),
shas: shas,
timestamp: timestamp
};
try {
const broken = !this.testingState.lint.passed ||
!this.testingState.tsc.passed ||
!this.testingState.transpile.passed ||
!this.testingState.simFuzz.passed ||
!this.testingState.studioFuzz.passed;
await this.reportErrorStatus( broken, lastBroken );
lastBroken = broken;
}
catch( e ) {
winston.info( `Slack error: ${e}` );
}
}
}
catch( e ) {
winston.info( `QuickServer error: ${e}` );
forceTests = true;
}
}
}
/**
* Starts the HTTP server part (that will connect with any reporting features).
* @public
*
* @param {number} port
*/
startServer( port ) {
assert( typeof port === 'number', 'port should be a number' );
// Main server creation
http.createServer( ( req, res ) => {
try {
const requestInfo = url.parse( req.url, true );
if ( requestInfo.pathname === '/quickserver/status' ) {
res.writeHead( 200, jsonHeaders );
res.end( JSON.stringify( this.testingState, null, 2 ) );
}
}
catch( e ) {
this.setError( `server error: ${e}` );
}
} ).listen( port );
winston.info( `QuickServer: running on port ${port}` );
}
/**
* Checks the error messages and reports the current status to the logs and Slack.
*
* @param {boolean} broken
* @param {boolean} lastBroken
* @private
* TODO for @chrisklus: add comments to this function
*/
async reportErrorStatus( broken, lastBroken ) {
if ( lastBroken === true && !broken ) {
this.errorMessages.length = 0;
winston.info( 'broken -> passing, sending CTQ passing message to Slack' );
await sendSlackMessage( 'CTQ passing', this.isTestMode );
}
else if ( broken ) {
let message = '';
let newErrorCount = 0;
const previousErrorsFound = [];
const check = result => {
if ( !result.passed ) {
result.errorMessages.forEach( errorMessage => {
if ( _.every( this.errorMessages, preExistingErrorMessage => {
const preExistingErrorMessageWithNoSpaces = preExistingErrorMessage.replace( /\s/g, '' );
const newErrorMessageWithNoSpaces = errorMessage.replace( /\s/g, '' );
return preExistingErrorMessageWithNoSpaces !== newErrorMessageWithNoSpaces;
} ) ) {
this.errorMessages.push( errorMessage );
message += `\n${errorMessage}`;
newErrorCount++;
}
else {
previousErrorsFound.push( errorMessage );
}
} );
}
};
check( this.testingState.lint );
check( this.testingState.tsc );
check( this.testingState.transpile );
check( this.testingState.simFuzz );
check( this.testingState.studioFuzz );
if ( message.length > 0 ) {
if ( previousErrorsFound.length || lastBroken ) {
winston.info( 'broken -> more broken, sending additional CTQ failure message to Slack' );
const sForFailure = newErrorCount > 1 ? 's' : '';
message = `CTQ additional failure${sForFailure}:\n\`\`\`${message}\`\`\``;
if ( previousErrorsFound.length ) {
assert && assert( lastBroken, 'Last cycle must be broken if pre-existing errors were found' );
const sForError = previousErrorsFound.length > 1 ? 's' : '';
const sForRemain = previousErrorsFound.length === 1 ? 's' : '';
message += `\n${previousErrorsFound.length} other pre-existing error${sForError} remain${sForRemain}.`;
}
else {
assert && assert( lastBroken, 'Last cycle must be broken if no pre-existing errors were found and you made it here' );
message += '\nAll other pre-existing errors fixed.';
}
}
else {
winston.info( 'passing -> broken, sending CTQ failure message to Slack' );
message = 'CTQ failing:\n```' + message + '```';
}
winston.info( message );
await sendSlackMessage( message, this.isTestMode );
}
else {
winston.info( 'broken -> broken, no new failures to report to Slack' );
assert && assert( previousErrorsFound.length, 'Previous errors must exist if no new errors are found and CTQ is still broken' );
}
}
else {
winston.info( 'passing -> passing' );
}
}
/**
* Parses individual errors out of a collection of the same type of error, e.g. lint
*
* @param {string} message
* @param {string} name
* @returns {string[]}
* @private
*/
parseErrors( message, name ) {
const errorMessages = [];
// most lint and tsc errors have a file associated with them. look for them in a line via slashes
const fileNameRegex = /.*\/.+\/.+\/.+\/.+/;
// split up the error message by line for parsing
const messageLines = message.split( /\r?\n/ );
if ( name === ctqType.LINT ) {
let currentFilename = null;
// look for a filename. once found, all subsequent lines are an individual errors to add until a blank line is reached
messageLines.forEach( line => {
if ( currentFilename ) {
if ( line.length > 0 ) {
errorMessages.push( `lint: ${currentFilename}${line}` );
}
else {
currentFilename = null;
}
}
else if ( fileNameRegex.test( line ) ) {
currentFilename = line.match( fileNameRegex )[ 0 ];
}
} );
}
else if ( name === ctqType.TSC ) {
let currentError = '';
const addCurrentError = () => {
if ( currentError.length ) {
errorMessages.push( currentError );
}
};
// look for a filename. if found, all subsequent lines that don't contain filenames are part of the same error to
// add until a new filename line is found
messageLines.forEach( line => {
if ( fileNameRegex.test( line ) ) {
addCurrentError();
currentError = `tsc: ${line}`;
}
else if ( currentError.length && line.length ) {
currentError += `\n${line}`;
}
} );
addCurrentError();
}
// if we are not a lint or tsc error, or if those errors were not able to be parsed above, send the whole message
if ( !errorMessages.length ) {
errorMessages.push( `${name}: ${message}` );
}
return errorMessages;
}
}
module.exports = QuickServer;