Permalink
Browse files

feat(): stop old, deprecated builds

  • Loading branch information...
Izak88 committed Sep 5, 2017
1 parent 6b244c5 commit 284abcd6e1a1ece6a21b24745acb09ded34b4f55
Showing with 123 additions and 39 deletions.
  1. +33 −0 e2e/050_builds.e2e.ts
  2. +1 −1 e2e/060_build.e2e.ts
  3. +39 −0 src/api/db/build.ts
  4. +50 −38 src/api/process-manager.ts
@@ -118,6 +118,39 @@ describe('Builds', () => {
}));
});

it('should restart last build and send same push event, the old build should stop', () => {
return Promise.resolve()
.then((): any => browser.wait(() => {
return element.all(by.css('.is-running')).count().then(count => count === 0);
}))
.then((): any => browser.wait(() => {
return element.all(by.css('.restart-build')).first().isPresent();
}))
.then(() => delay(2000))
.then((): any => element.all(by.css('.restart-build')).first().click())
.then((): any => browser.wait(() => {
return element.all(by.css('.is-running')).count().then(count => count === 1);
}))
.then(() => sendGitHubRequest(pushEventRequest, pushEventHeader))
.then((): any => browser.wait(() => {
return element.all(by.css('.is-running')).count().then(count => count === 1);
}))
.then((): any => browser.wait(() => {
return element.all(by.css('.stop-build')).first().isPresent();
}))
.then((): any => {
return browser.wait(() => {
const el = element(by.css('.stop-build'));
return ExpectedConditions.elementToBeClickable(el);
});
})
.then(() => delay(2000))
.then((): any => element.all(by.css('.stop-build')).first().click())
.then((): any => browser.wait(() => {
return element.all(by.css('.is-running')).count().then(count => count === 0);
}));
});

it('should restart last build', () => {
return Promise.resolve()
.then((): any => browser.wait(() => {
@@ -162,7 +162,7 @@ describe('Build Details', () => {
.then(() => browser.get('/'))
.then(() => sendGitHubRequest(requestD3, header))
.then((): any => browser.wait(() => element.all(by.css('.list-item')).count().then(cnt => {
return cnt === 4;
return cnt === 5;
})))
.then((): any => browser.wait(() => {
return element.all(by.css('.is-running')).count().then(count => count === 1);
@@ -282,3 +282,42 @@ export function getBuildStatus(buildId: number): Promise<any> {
});
});
}

export function getDepracatedBuilds(build: any): Promise<any> {
return new Promise((resolve, reject) => {
new Build({ repositories_id: build.repositories_id })
.query(q => {
q.whereNull('end_time')
.andWhereNot('id', build.id);

if (build.pr) {
q.where('pr', build.pr);
}
})
.fetchAll()
.then(builds => {
if (!builds) {
reject();
}

builds = builds.toJSON();
if (!build.pr) {
builds = builds.filter(b => {
if (build.data.before) {
return build.data.before === b.data.before;
} else if (build.data.before_sha) {
return build.data.before_sha === b.data.before_sha;
} else if (build.data.object_attributes && build.data.object_attirbutes.before_sha) {
return build.data.object_attirbutes.before_sha
=== b.data.object_attirbutes.before_sha;
}

return false;
});
}
builds = builds.map(b => b.id);

resolve(builds);
});
});
}
@@ -1,6 +1,13 @@
import { startDockerImageSetupJob, startBuildProcess, stopContainer } from './process';
import { Observable, Subject, BehaviorSubject, Subscription } from 'rxjs';
import { insertBuild, updateBuild, getBuild, getBuildStatus, getLastRunId } from './db/build';
import {
insertBuild,
updateBuild,
getBuild,
getBuildStatus,
getLastRunId,
getDepracatedBuilds
} from './db/build';
import { insertBuildRun, updateBuildRun } from './db/build-run';
import * as dbJob from './db/job';
import * as dbJobRuns from './db/job-run';
@@ -126,46 +133,51 @@ export function startBuild(data: any): Promise<any> {
return insertBuildRun(data);
})
.then(() => getBuild(data.build_id))
.then(buildData => sendPendingStatus(buildData, buildData.id))
.then(() => {
return config.reduce((prev, cfg, i) => {
return prev.then(() => {
let dataJob = null;

return dbJob.insertJob({ data: JSON.stringify(cfg), builds_id: data.build_id })
.then(job => dataJob = job)
.then(() => getLastRunId(data.build_id))
.then(lastRunId => {
const jobRun = {
start_time: new Date,
status: 'queued',
build_run_id: lastRunId,
job_id: dataJob.id
};

return dbJobRuns.insertJobRun(jobRun);
})
.then(() => {
jobEvents.next({
type: 'process',
build_id: data.build_id,
job_id: dataJob.id,
data: 'jobAdded'
.then(buildData => {
sendPendingStatus(buildData, buildData.id)
.then(() => {
return config.reduce((prev, cfg, i) => {
return prev.then(() => {
let dataJob = null;

return dbJob.insertJob({ data: JSON.stringify(cfg), builds_id: data.build_id })
.then(job => dataJob = job)
.then(() => getLastRunId(data.build_id))
.then(lastRunId => {
const jobRun = {
start_time: new Date,
status: 'queued',
build_run_id: lastRunId,
job_id: dataJob.id
};

return dbJobRuns.insertJobRun(jobRun);
})
.then(() => {
jobEvents.next({
type: 'process',
build_id: data.build_id,
job_id: dataJob.id,
data: 'jobAdded'
});

return queueJob(data.build_id, dataJob.id);
});

return queueJob(data.build_id, dataJob.id);
});
});
}, Promise.resolve());
})
.then(() => {
jobEvents.next({
type: 'process',
build_id: data.build_id,
repository_id: data.repositories_id,
data: 'buildAdded'
});
}, Promise.resolve());
})
.then(() => getDepracatedBuilds(buildData))
.then(builds => Promise.all(builds.map(build => stopBuild(build))));
})
.then(() => {
jobEvents.next({
type: 'process',
build_id: data.build_id,
repository_id: data.repositories_id,
data: 'buildAdded'
});
}).catch(err => logger.error(err));
.catch(err => logger.error(err));
}

export function startJob(proc: JobProcess): Promise<void> {

0 comments on commit 284abcd

Please sign in to comment.