Skip to content

Commit

Permalink
Refactor compute adapters to use ES6 classes. Closes #1698 (#1699)
Browse files Browse the repository at this point in the history
* WIP refactor compute backend, client, local compute

* Update GME, SciServer Compute clients to ES6 classes. Closes #1698
  • Loading branch information
brollb authored May 14, 2020
1 parent b06dfc8 commit 15c16a4
Show file tree
Hide file tree
Showing 5 changed files with 579 additions and 576 deletions.
58 changes: 28 additions & 30 deletions src/common/compute/backends/ComputeBackend.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
/* globals define, requirejs */
define([
'module',
'q',
], function(
module,
Q,
) {

const ComputeBackend = function(id, metadata) {
const {name, dashboard, client} = metadata;
this.id = id;
this.name = name;
this.dashboardPath = dashboard;
this.clientPath = client || './Client';
};

ComputeBackend.prototype.getClient = function(logger, blobClient, config) {
if (require.isBrowser) {
throw new Error('Compute clients cannot be loaded in the browser.');
class ComputeBackend {
constructor (id, metadata) {
const {name, dashboard, client} = metadata;
this.id = id;
this.name = name;
this.dashboardPath = dashboard;
this.clientPath = client || './Client';
}

const Client = requirejs(`deepforge/compute/backends/${this.id}/${this.clientPath}`);
return new Client(logger, blobClient, config);
};
getClient (logger, blobClient, config) {
if (require.isBrowser) {
throw new Error('Compute clients cannot be loaded in the browser.');
}

const Client = requirejs(`deepforge/compute/backends/${this.id}/${this.clientPath}`);
return new Client(logger, blobClient, config);
}

ComputeBackend.prototype.getDashboard = async function() {
if (this.dashboardPath) {
const absPath = `deepforge/compute/backends/${this.id}/${this.dashboardPath}`;
return await this.require(absPath);
} else {
return null;
async getDashboard () {
if (this.dashboardPath) {
const absPath = `deepforge/compute/backends/${this.id}/${this.dashboardPath}`;
return await this.require(absPath);
} else {
return null;
}
}
};

ComputeBackend.prototype.require = function(path) { // helper for loading async
const deferred = Q.defer();
require([path], deferred.resolve, deferred.reject);
return deferred.promise;
};
require (path) { // helper for loading async
return new Promise((resolve, reject) =>
require([path], resolve, reject)
);
}
}

return ComputeBackend;
});
87 changes: 45 additions & 42 deletions src/common/compute/backends/ComputeClient.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
/* globals define */
define([], function() {
const ComputeClient = function(logger, blobClient) {
this.logger = logger.fork('compute');
this.blobClient = blobClient;
this._events = {};
};

ComputeClient.prototype.cancelJob = function(/*job*/) {
unimplemented(this.logger, 'cancelJob');
};

ComputeClient.prototype.createJob = async function(/*hash*/) {
unimplemented(this.logger, 'createJob');
};

ComputeClient.prototype.getStatus = async function(/*jobInfo*/) {
unimplemented(this.logger, 'getStatus');
};

ComputeClient.prototype.getResultsInfo = async function(/*jobInfo*/) {
unimplemented(this.logger, 'getResultsInfo');
};

ComputeClient.prototype.getConsoleOutput = async function(/*hash*/) {
unimplemented(this.logger, 'getConsoleOutput');
};

ComputeClient.prototype.isFinishedStatus = function(status) {
const notFinishedStatuses = [this.QUEUED, this.PENDING, this.RUNNING];
return !notFinishedStatuses.includes(status);
};

// Some functions for event support
ComputeClient.prototype.on = function(ev, cb) {
this._events[ev] = this._events[ev] || [];
this._events[ev].push(cb);
};

ComputeClient.prototype.emit = function(ev) {
const args = Array.prototype.slice.call(arguments, 1);
const handlers = this._events[ev] || [];
return Promise.all(handlers.map(fn => fn.apply(this, args)));
};

class ComputeClient {
constructor (logger, blobClient) {
this.logger = logger.fork('compute');
this.blobClient = blobClient;
this._events = {};
}

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

createJob (/*hash*/) {
unimplemented(this.logger, 'createJob');
}

getStatus (/*jobInfo*/) {
unimplemented(this.logger, 'getStatus');
}

getResultsInfo (/*jobInfo*/) {
unimplemented(this.logger, 'getResultsInfo');
}

getConsoleOutput (/*hash*/) {
unimplemented(this.logger, 'getConsoleOutput');
}

isFinishedStatus (status) {
const notFinishedStatuses = [this.QUEUED, this.PENDING, this.RUNNING];
return !notFinishedStatuses.includes(status);
}

// Some functions for event support
on (ev, cb) {
this._events[ev] = this._events[ev] || [];
this._events[ev].push(cb);
}

emit (ev) {
const args = Array.prototype.slice.call(arguments, 1);
const handlers = this._events[ev] || [];
return Promise.all(handlers.map(fn => fn.apply(this, args)));
}
}

ComputeClient.prototype.QUEUED = 'queued';
ComputeClient.prototype.PENDING = 'pending';
Expand Down
Loading

0 comments on commit 15c16a4

Please sign in to comment.