Skip to content

Commit

Permalink
Add purge to compute clients. Closes #1724 (#1922)
Browse files Browse the repository at this point in the history
* Add purge to compute clients. Closes #1724

* Add purgeJob tests
  • Loading branch information
brollb committed Sep 28, 2020
1 parent c6553e5 commit fe9a6a9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/common/compute/backends/ComputeClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ define([], function() {
this._events = {};
}

async startJob (/*hash, metadata*/) {
unimplemented(this.logger, 'startJob');
}

async cancelJob (/*job*/) {
unimplemented(this.logger, 'cancelJob');
}

async startJob (/*hash, metadata*/) {
unimplemented(this.logger, 'startJob');
async purgeJob (/*jobInfo*/) {
}

async getStatus (/*jobInfo*/) {
Expand Down
13 changes: 13 additions & 0 deletions src/common/compute/backends/local/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ define([
return JSON.parse(resultsTxt);
}

async purge (job) {
const {hash} = job;
if (hash === this.currentJob) {
throw new Error('Cannot purge running job.');
} else if (this.jobQueue.includes(hash)) {
const index = this.jobQueue.indexOf(hash);
this.jobQueue.splice(index, 1);
} else {
const workingDir = this._getWorkingDir(hash);
await rm_rf(workingDir);
}
}

async _getJobFile (hash, name, notFoundMsg) {
const filename = path.join(this._getWorkingDir(hash), name);
try {
Expand Down
28 changes: 28 additions & 0 deletions test/unit/common/compute/backends/local/Client.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
describe('local compute', function() {
const fsp = require('fs').promises;
const assert = require('assert');
const testFixture = require('../../../../../globals');
const GeneratedFiles = testFixture.requirejs('deepforge/plugin/GeneratedFiles');
Expand Down Expand Up @@ -35,6 +36,33 @@ describe('local compute', function() {
});
});

describe('purgeJob', function() {
let jobInfo;
before(async () => {
const jobHash = await getJobHash('sleep', '0.1');
const deferred = utils.defer();
client.on('update', (hash, status) => {
if (hash === jobHash && status === client.RUNNING) {
deferred.resolve();
}
});

const computeJob = new ComputeJob(jobHash);
jobInfo = await client.startJob(computeJob);
await deferred.promise;
});

it('should throw an error when accessing status on purged job', async () => {
const {hash} = jobInfo;
await assert.rejects(() => client.getStatus(hash));
});

it('should remove tmp directory', async function() {
const dir = client._getWorkingDir();
await assert.rejects(() => fsp.lstat(dir));
});
});

async function getJobHash(cmd) {
const config = {
cmd, args: Array.prototype.slice.call(arguments, 1),
Expand Down

0 comments on commit fe9a6a9

Please sign in to comment.