Skip to content

Commit

Permalink
Adding useRootDir option to CT for local use
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Apr 24, 2020
1 parent 04e8a48 commit f7bd04e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 30 deletions.
9 changes: 7 additions & 2 deletions js/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ module.exports = grunt => {

grunt.registerTask(
'continuous-server',
'Launches a continuous server',
'Launches a continuous server with the following options:\n' +
'--localCount=COUNT : [REQUIRED] specifies how many local build/grunt/etc. tasks should run in the background\n' +
'--port=PORT : specify a custom port for the server interface\n' +
'--useRootDir : when provided, files will not be copied (it will create one snapshot pointing to the root directly)\n' +
'--snapshot=false : when set to false, it will avoid creating any snapshots at all (loading previous state only)\n',
() => {
// We don't finish! Don't tell grunt this...
grunt.task.current.async();
Expand All @@ -31,6 +35,7 @@ module.exports = grunt => {
const port = grunt.option( 'port' ) ? Number.parseInt( grunt.option( 'port' ), 10 ) : 45366;
const localCount = Number.parseInt( grunt.option( 'localCount' ), 10 );
const snapshot = grunt.option( 'snapshot' ) !== false;
const useRootDir = !!grunt.option( 'useRootDir' );

const serverQueryParameter = encodeURIComponent( `http://localhost:${port}` );
const unbuiltReportURL = `${buildLocal.localTestingURL}aqua/html/continuous-unbuilt-report.html?server=${serverQueryParameter}`;
Expand All @@ -41,7 +46,7 @@ module.exports = grunt => {
console.log( builtReportURL );
console.log( loopURL );

const server = new ContinuousServer();
const server = new ContinuousServer( useRootDir );
server.startServer( port );
server.generateReportLoop();

Expand Down
52 changes: 35 additions & 17 deletions js/server/ContinuousServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ const winston = require( 'winston' );
// in days, any shapshots that are older will be removed from the continuous report
const NUMBER_OF_DAYS_TO_KEEP_SNAPSHOTS = 2;

const DEBUG_PRETEND_CLEAN = false;

// Headers that we'll include in all server replies
const jsonHeaders = {
'Content-Type': 'application/json',
Expand All @@ -48,7 +46,15 @@ const twoHours = 1000 * 60 * 60 * 2;
const twelveHours = 1000 * 60 * 60 * 12;

class ContinuousServer {
constructor() {
/**
* @param {boolean} useRootDir - If true, we won't create/copy, and we'll just use the files there instead
*/
constructor( useRootDir = false ) {

winston.info( `useRootDir: ${useRootDir}` );

// @public {boolean}
this.useRootDir = useRootDir;

// @public {string} - root of your GitHub working copy, relative to the name of the directory that the
// currently-executing script resides in
Expand Down Expand Up @@ -261,6 +267,11 @@ class ContinuousServer {
* @public
*/
saveToFile() {
// Don't save or load state if useRootDir is true
if ( this.useRootDir ) {
return;
}

fs.writeFileSync( this.saveFile, JSON.stringify( {
snapshots: this.snapshots.map( snapshot => snapshot.serialize() ),
pendingSnapshot: this.pendingSnapshot ? this.pendingSnapshot.serializeStub() : null,
Expand All @@ -273,6 +284,11 @@ class ContinuousServer {
* @public
*/
loadFromFile() {
// Don't save or load state if useRootDir is true
if ( this.useRootDir ) {
return;
}

if ( fs.existsSync( this.saveFile ) ) {
const serialization = JSON.parse( fs.readFileSync( this.saveFile, 'utf-8' ) );
this.snapshots = serialization.snapshots.map( Snapshot.deserialize );
Expand Down Expand Up @@ -436,9 +452,11 @@ class ContinuousServer {
}

// Kick off initial old snapshot removal
for ( const snapshot of this.trashSnapshots ) {
// NOTE: NO await here, we're going to do that asynchronously so we don't block
this.deleteTrashSnapshot( snapshot );
if ( !this.useRootDir ) {
for ( const snapshot of this.trashSnapshots ) {
// NOTE: NO await here, we're going to do that asynchronously so we don't block
this.deleteTrashSnapshot( snapshot );
}
}

// initial NPM checks, so that all repos will have node_modules that need them
Expand All @@ -449,20 +467,24 @@ class ContinuousServer {
}
}

while ( true ) { // eslint-disable-line
if ( this.useRootDir ) {
const snapshot = new Snapshot( this.rootDir, this.setStatus.bind( this ) );

// Create a snapshot without copying files
await snapshot.create( true );

this.snapshots.push( snapshot );
}

while ( !this.useRootDir ) {
try {
const staleMessage = wasStale ? 'Changes detected, waiting for stable SHAs' : 'No changes';

const reposToCheck = getRepoList( 'active-repos' ).filter( repo => repo !== 'aqua' );

const staleRepos = await asyncFilter( reposToCheck, async repo => {
this.setStatus( `${staleMessage}; checking ${repo}` );
if ( DEBUG_PRETEND_CLEAN ) {
return false;
}
else {
return await isStale( repo );
}
return await isStale( repo );
} );

if ( staleRepos.length ) {
Expand Down Expand Up @@ -519,10 +541,6 @@ class ContinuousServer {
this.saveToFile();
}
}

if ( DEBUG_PRETEND_CLEAN ) {
await sleep( 10000000 );
}
}
catch ( e ) {
this.setError( `snapshot error: ${e}` );
Expand Down
35 changes: 25 additions & 10 deletions js/server/Snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@ class Snapshot {
/**
* Creates this snapshot.
* @public
*
* @param {boolean} [useRootDir] - If true, we won't create/copy, and we'll just use the files there instead
*/
async create() {
async create( useRootDir = false ) {

const timestamp = Date.now();
const snapshotDir = `${this.rootDir}/ct-snapshots`;

this.setStatus( `Initializing new snapshot: ${timestamp}` );

// @public {boolean}
this.useRootDir = useRootDir;

// @public {number}
this.timestamp = timestamp;

Expand All @@ -57,12 +62,14 @@ class Snapshot {
this.exists = true;

// @public {string|null} - Set to null when it's deleted fully
this.directory = `${snapshotDir}/${timestamp}`;
this.directory = useRootDir ? this.rootDir : `${snapshotDir}/${timestamp}`;

if ( !fs.existsSync( snapshotDir ) ) {
await createDirectory( snapshotDir );
if ( !useRootDir ) {
if ( !fs.existsSync( snapshotDir ) ) {
await createDirectory( snapshotDir );
}
await createDirectory( this.directory );
}
await createDirectory( this.directory );

// @public {Array.<string>}
this.repos = getRepoList( 'active-repos' );
Expand All @@ -73,9 +80,11 @@ class Snapshot {
this.shas[ repo ] = await gitRevParse( repo, 'master' );
}

for ( const repo of this.repos ) {
this.setStatus( `Copying snapshot files: ${repo}` );
await copyDirectory( `${this.rootDir}/${repo}`, `${this.directory}/${repo}`, {} );
if ( !useRootDir ) {
for ( const repo of this.repos ) {
this.setStatus( `Copying snapshot files: ${repo}` );
await copyDirectory( `${this.rootDir}/${repo}`, `${this.directory}/${repo}`, {} );
}
}

this.setStatus( 'Scanning commit timestamps' );
Expand Down Expand Up @@ -126,7 +135,9 @@ class Snapshot {
async remove() {
this.exists = false;

await deleteDirectory( this.directory );
if ( !this.useRootDir ) {
await deleteDirectory( this.directory );
}

this.directory = null;
}
Expand Down Expand Up @@ -176,6 +187,7 @@ class Snapshot {
else {
return {
rootDir: this.rootDir,
useRootDir: this.useRootDir,
timestamp: this.timestamp,
constructed: this.constructed,
name: this.name,
Expand All @@ -198,7 +210,8 @@ class Snapshot {
return {
rootDir: this.rootDir,
constructed: this.constructed,
directory: this.directory
directory: this.directory,
useRootDir: this.useRootDir
};
}

Expand All @@ -211,6 +224,7 @@ class Snapshot {
static deserialize( serialization ) {
const snapshot = new Snapshot( serialization.rootDir, () => {} );

snapshot.useRootDir = serialization.useRootDir || false;
snapshot.constructed = serialization.constructed === undefined ? true : serialization.constructed;
snapshot.timestamp = serialization.timestamp;
snapshot.name = serialization.name;
Expand All @@ -234,6 +248,7 @@ class Snapshot {

snapshot.constructed = serialization.constructed === undefined ? true : serialization.constructed;
snapshot.directory = serialization.directory;
snapshot.useRootDir = serialization.useRootDir || false;

return snapshot;
}
Expand Down
2 changes: 1 addition & 1 deletion js/server/Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class Test {
getObjectForBrowser() {
assert( this.type === 'sim-test' || this.type === 'qunit-test' || this.type === 'pageload-test', 'Needs to be a browser test' );

const baseURL = `../../ct-snapshots/${this.snapshot.timestamp}`;
const baseURL = this.snapshot.useRootDir ? '../..' : `../../ct-snapshots/${this.snapshot.timestamp}`;
let url;

if ( this.type === 'sim-test' ) {
Expand Down

0 comments on commit f7bd04e

Please sign in to comment.