Skip to content

Commit

Permalink
Block until job canceled (local compute). Fixes #1743
Browse files Browse the repository at this point in the history
  • Loading branch information
brollb committed Jul 2, 2020
1 parent b203e79 commit a67a794
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/common/compute/backends/local/Client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*globals define*/
// TODO: Show an error if not running on the server...
define([
'../../../utils',
'common/util/assert',
'../ComputeClient',
'../JobResults',
Expand All @@ -12,6 +13,7 @@ define([
'os',
'path',
], function(
utils,
assert,
ComputeClient,
JobResults,
Expand Down Expand Up @@ -56,12 +58,15 @@ define([
this.canceled = false;
}

cancelJob (jobInfo) {
async cancelJob (jobInfo) {
const {hash} = jobInfo;

if (this.currentJob === hash) {
this.canceled = true;
this.subprocess.kill();
await utils.waitUntil(
async () => this.CANCELED === await this.getStatus(jobInfo)
);
} else if (this.jobQueue.includes(hash)) {
const i = this.jobQueue.indexOf(hash);
this.jobQueue.splice(i, 1);
Expand Down
14 changes: 14 additions & 0 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,26 @@
return obj1;
};

async function sleep(duration) {
const deferred = defer();
setTimeout(deferred.resolve, duration);
return deferred.promise;
}

async function waitUntil(fn, interval=50) {
while (!await fn()) {
await sleep(interval);
}
}

return {
deepExtend,
splitObj,
resolveCarriageReturns,
abbr,
withTimeout,
defer,
sleep,
waitUntil,
};
}));
45 changes: 45 additions & 0 deletions test/unit/common/compute/backends/local/Client.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
describe('local compute', function() {
const assert = require('assert');
const testFixture = require('../../../../../globals');
const GeneratedFiles = testFixture.requirejs('deepforge/plugin/GeneratedFiles');
const Compute = testFixture.requirejs('deepforge/compute/index');
const BlobClient = require('webgme-engine/src/server/middleware/blob/BlobClientWithFSBackend');
const gmeConfig = testFixture.getGmeConfig();
const blobClient = new BlobClient(gmeConfig, testFixture.logger);
const backend = Compute.getBackend('local');
const client = backend.getClient(testFixture.logger, blobClient);
const utils = testFixture.requirejs('deepforge/utils');

describe('cancelJob', function() {
let jobInfo;

beforeEach(async () => {
const jobHash = await getJobHash('sleep', '10');
const deferred = utils.defer();
client.on('update', (hash, status) => {
if (hash === jobHash && status === client.RUNNING) {
deferred.resolve();
}
});

jobInfo = await client.createJob(jobHash);
await deferred.promise;
});

it('should block until canceled', async function() {
await client.cancelJob(jobInfo);
const status = await client.getStatus(jobInfo);
assert.equal(status, client.CANCELED);
});
});

async function getJobHash(cmd) {
const config = {
cmd, args: Array.prototype.slice.call(arguments, 1),
};
const files = new GeneratedFiles(blobClient);
files.addFile('executor_config.json', JSON.stringify(config));
return await files.save();
}
});

0 comments on commit a67a794

Please sign in to comment.