Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 2020-09-24 #222

Merged
merged 5 commits into from Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -19,7 +19,7 @@
"license": "CC0-1.0",
"dependencies": {
"@hapi/hapi": "^20.0.0",
"aws-sdk": "^2.751.0",
"aws-sdk": "^2.756.0",
"axios": "^0.20.0",
"cfenv": "^1.2.3",
"jsonwebtoken": "^8.5.1",
Expand Down
28 changes: 18 additions & 10 deletions src/cf-task-pool.js
Expand Up @@ -4,6 +4,7 @@ const CloudFoundryAPIClient = require('./cloud-foundry-api-client');
const logger = require('./logger');

class TaskStartError extends Error {}
class TaskStopError extends Error {}

class CFTaskPool {
constructor({
Expand Down Expand Up @@ -63,15 +64,18 @@ class CFTaskPool {
return true;
}

stopBuild(buildID) {
logger.info('Stopping build', buildID);
async stopBuild(buildID) {
const { taskGUID, timeout } = this._builds[buildID];
clearTimeout(timeout);
delete this._builds[buildID];
return this._apiClient.stopTask(taskGUID)
.catch(() => {
// This will fail if the task has already completed
});
try {
await this._apiClient.stopTask(taskGUID);
logger.info('Stopped build', buildID);
return;
} catch (_) {
// This will fail if the task has already completed
throw new TaskStopError('Build already completed');
}
}

_buildTask(build, command) {
Expand Down Expand Up @@ -100,10 +104,14 @@ class CFTaskPool {
return (allocMemory + requestedMemory) < this._maxTaskMemory;
}

_timeoutBuild(build) {
logger.warn('Build %s timed out', build.buildID);
this.stopBuild(build.buildID);
this._buildStatusReporter.reportBuildStatus(build, 'error');
async _timeoutBuild(build) {
try {
await this.stopBuild(build.buildID);
logger.warn('Build %s timed out', build.buildID);
this._buildStatusReporter.reportBuildStatus(build, 'error');
} catch (_) {
// The task already completed, do nothing
}
}

_requiresCustom(build) {
Expand Down
31 changes: 23 additions & 8 deletions test/cf-task-pool-test.js
Expand Up @@ -180,14 +180,15 @@ describe('CFTaskPool', () => {
sinon.assert.calledWith(builderPool._apiClient.stopTask, taskGUID);
});

it('ignores rejections from CF Api', async () => {
it('throws on rejections from CF Api', async () => {
sinon.stub(builderPool._apiClient, 'stopTask').rejects();

await builderPool.stopBuild(buildID);
const error = await builderPool.stopBuild(buildID).catch(e => e);

expect(builderPool._builds[buildID]).to.be.undefined;
sinon.assert.calledWith(sinon.clock.clearTimeout, timeout);
sinon.assert.calledWith(builderPool._apiClient.stopTask, taskGUID);
expect(error).to.be.an('error');
});
});

Expand Down Expand Up @@ -290,23 +291,37 @@ describe('CFTaskPool', () => {
const build = { buildID: 1 };

let builderPool;
let stopBuildStub;

beforeEach(() => {
builderPool = createPool();
sinon.stub(builderPool, 'stopBuild');
stopBuildStub = sinon.stub(builderPool, 'stopBuild');
sinon.stub(builderPool._buildStatusReporter, 'reportBuildStatus');
});

it('calls `this.stopBuild` with the buildID of the build', () => {
builderPool._timeoutBuild(build);
it('calls `this.stopBuild` with the buildID of the build', async () => {
stopBuildStub.resolves();
await builderPool._timeoutBuild(build);

sinon.assert.calledWith(builderPool.stopBuild, build.buildID);
});

it('reports the build timeout', () => {
builderPool._timeoutBuild(build);
context('when successful', () => {
it('reports the build timeout', async () => {
stopBuildStub.resolves();
await builderPool._timeoutBuild(build);

sinon.assert.calledWith(builderPool._buildStatusReporter.reportBuildStatus, build, 'error');
});
});

sinon.assert.calledWith(builderPool._buildStatusReporter.reportBuildStatus, build, 'error');
context('when failure', () => {
it('does not report the build timeout', async () => {
stopBuildStub.rejects();
await builderPool._timeoutBuild(build);

sinon.assert.notCalled(builderPool._buildStatusReporter.reportBuildStatus);
});
});
});

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -715,10 +715,10 @@ async@^3.1.0, async@^3.2.0:
resolved "https://registry.yarnpkg.com/async/-/async-3.2.0.tgz#b3a2685c5ebb641d3de02d161002c60fc9f85720"
integrity sha512-TR2mEZFVOj2pLStYxLht7TyfuRzaydfpxr3k9RpHIzMgw7A64dzsdqCxH1WJyQdoe8T10nDXd9wnEigmiuHIZw==

aws-sdk@^2.751.0:
version "2.751.0"
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.751.0.tgz#aeab21a5fc1b1f843180a988ff0fb650119dc6ff"
integrity sha512-0lo7YKTfjEwoP+2vK7F7WGNigvwFxqiM96PzBaseOpOelfhFHPKEJpk2Poa12JI89c8dGoc1PhTQ1TSTJK3ZqQ==
aws-sdk@^2.756.0:
version "2.756.0"
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.756.0.tgz#87ec4e2a03ed838de6d7b0ce6a62f276e318b4fc"
integrity sha512-Hk6DzcsXq1WRg+UVHDH56iQz31kDtg/NRqtJL1A0BrZ/PtNSLTHsQQllpcAi09UxLDMzBoDXymZ8kYg0Migq8w==
dependencies:
buffer "4.9.2"
events "1.1.1"
Expand Down