Skip to content

Commit

Permalink
Miscellaneous updates:
Browse files Browse the repository at this point in the history
- add backend data to job comm channel tests
- add comment to all job/cell listeners for ease of finding them
- update docs in jobCommChannel.js
- add flake8/black precommit hooks to package.json
- update package-lock.json'
  • Loading branch information
ialarmedalien committed Jan 21, 2022
1 parent c8b336c commit 3629d50
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 86 deletions.
33 changes: 6 additions & 27 deletions kbase-extension/static/kbase/js/common/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ define(['kb_service/client/workspace', 'kb_service/utils', 'common/runtime'], (
ServiceUtils,
Runtime
) => {
'use strict';
function filterObjectInfoByType(objects, types) {
return objects
.map((objectInfo) => {
Expand All @@ -16,26 +17,6 @@ define(['kb_service/client/workspace', 'kb_service/utils', 'common/runtime'], (
});
}

// function getObjectsByTypes(types, bus, onUpdated) {
// return bus.channel('data').when('workspace-data-updated')
// .then(function(message) {
// if (onUpdated) {
// bus.channel('data').on('workspace-data-updated', function(message) {
// var result = {
// data: filterObjectInfoByType(message.objectInfo, types),
// timestamp: message.timestamp
// }
// onUpdated(result);
// });
// }
// var result = {
// data: filterObjectInfoByType(message.objectInfo, types),
// timestamp: message.timestamp
// }
// return result;
// })
// }

function getObjectsByTypes(types, connection, onUpdated) {
const listener = connection.channel('data').plisten({
key: {
Expand All @@ -50,11 +31,10 @@ define(['kb_service/client/workspace', 'kb_service/utils', 'common/runtime'], (
},
});
return listener.promise.then((message) => {
const result = {
return {
data: filterObjectInfoByType(message.objectInfo, types),
timestamp: message.timestamp,
};
return result;
});
}

Expand All @@ -71,9 +51,8 @@ define(['kb_service/client/workspace', 'kb_service/utils', 'common/runtime'], (
});

// assume (for now) that the refs are valid and not random strings or numbers or objects.
const refList = [];
refs.forEach((ref) => {
refList.push({ ref: ref });
const refList = refs.map((ref) => {
return { ref };
});
return workspace.get_object_info_new({ objects: refList }).then((infos) => {
const objInfos = {};
Expand All @@ -89,7 +68,7 @@ define(['kb_service/client/workspace', 'kb_service/utils', 'common/runtime'], (
}

return {
getObjectsByTypes: getObjectsByTypes,
getObjectsByRef: getObjectsByRef,
getObjectsByTypes,
getObjectsByRef,
};
});
55 changes: 26 additions & 29 deletions kbase-extension/static/kbase/js/common/iframe/messages.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
define(['bluebird', 'uuid'], (Promise, Uuid) => {
function factory(config) {
const root = config.root;
const name = config.name;
'use strict';

function factory(config) {
const { root, name } = config;
const serviceId = new Uuid(4).format();

let lastId = 0; //: number;
let sentCount; //: number;
let receivedCount; //: number;
// let sentCount; //: number;
// let receivedCount; //: number;
const partners = {}; // Map<String, any>;
const listeners = {}; //Map<String, Array<any>>;
const awaitingResponse = {}; //: Map<String, any>;
Expand All @@ -17,8 +17,8 @@ define(['bluebird', 'uuid'], (Promise, Uuid) => {
return 'msg_' + String(lastId);
}

function addPartner(config) {
partners[config.name] = config;
function addPartner(_config) {
partners[_config.name] = _config;
}

function listen(listener) {
Expand All @@ -29,20 +29,17 @@ define(['bluebird', 'uuid'], (Promise, Uuid) => {
}

function receive(event) {
let origin = event.origin || event.originalEvent.origin,
message = event.data,
listener,
response;
const message = event.data;
let response;

receivedCount += 1;
// receivedCount += 1;

if (!message.address && !message.address.to) {
console.warn('Message without address.to - ignored (iframe)', message);
return;
}

if (message.address.to !== serviceId) {
// console.log('not for us (iframe) ... ignoring', message, serviceId);
return;
}

Expand All @@ -58,9 +55,9 @@ define(['bluebird', 'uuid'], (Promise, Uuid) => {
}

if (listeners[message.name]) {
listeners[message.name].forEach((listener) => {
listeners[message.name].forEach((_listener) => {
try {
listener.handler(message);
_listener.handler(message);
return;
} catch (ex) {
console.error('Error handling listener for message ', message, ex);
Expand All @@ -69,11 +66,11 @@ define(['bluebird', 'uuid'], (Promise, Uuid) => {
}
}

function getPartner(name) {
if (!partners[name]) {
throw new Error('Partner ' + name + ' not registered');
function getPartner(_name) {
if (!partners[_name]) {
throw new Error('Partner ' + _name + ' not registered');
}
return partners[name];
return partners[_name];
}

function send(partnerName, message) {
Expand All @@ -83,7 +80,7 @@ define(['bluebird', 'uuid'], (Promise, Uuid) => {
to: partner.serviceId,
from: serviceId,
};
sentCount += 1;
// sentCount += 1;
partner.window.postMessage(message, partner.host);
}

Expand All @@ -98,7 +95,7 @@ define(['bluebird', 'uuid'], (Promise, Uuid) => {
}

function request(partnerName, message) {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
sendRequest(partnerName, message, (response) => {
resolve(response);
});
Expand All @@ -114,14 +111,14 @@ define(['bluebird', 'uuid'], (Promise, Uuid) => {
}

return Object.freeze({
start: start,
stop: stop,
send: send,
request: request,
receive: receive,
listen: listen,
addPartner: addPartner,
serviceId: serviceId,
start,
stop,
send,
request,
receive,
listen,
addPartner,
serviceId,
});
}

Expand Down
37 changes: 19 additions & 18 deletions kbase-extension/static/kbase/js/common/jobCommChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
* request.
* 2. This gets captured by one of the callbacks in handleBusMessages()
* 3. These all invoke sendCommMessage(). This builds a message packet and sends it across the
* comm channel where it gets heard by a capturing function in biokbase.narrative.jobmanager.
* ...that's it. These messages are asynchronous by design. They're meant to be requests that
* get responses eventually.
* comm channel where it gets heard by a capturing function in biokbase.narrative.jobs.jobcomm
*
* These messages are asynchronous by design. They're meant to be requests that get responses
* eventually.
*
* From the back end to the front, the flow is slightly different. On Comm channel creation time,
* the handleCommMessages function is set up as the callback for anything that comes across the
* channel to the front end. Each of these has a message type associated with it.
* 1. The type is interepreted (a big ol' switch statement) and responded to.
* 2. Most responses (job status updates, log messages) are parceled out to the App Cells that
* invoked them. Or, really, anything listening on the appropriate channel (either the cell,
* or the job id)
*
* 1. The type is interpreted (a big ol' switch statement) and responded to.
* 2. Responses (job status updates, log messages) are "addressed" using either a job or a cell
* ID and a message type. Any frontend component with a listener of the appropriate type on
* the appropriate channel can pick up and act upon the content of the messages.
*/
define([
'bluebird',
Expand Down Expand Up @@ -116,7 +118,7 @@ define([

/**
* Sends a message over the bus. The channel should have a single key of either
* cell or jobId.
* cell or job ID.
* @param {string} channelType - either CELL or JOB
* @param {string} channelId - id for the channel
* @param {string} msgType - one of the msg types
Expand All @@ -136,9 +138,9 @@ define([
}

/**
* Registers callbacks for handling bus messages. This listens to the global runtime bus.
* Mostly, it relays bus messages into comm channel requests that are satisfied by the
* kernel.
* Registers callbacks for handling bus messages. This listens to the global runtime
* bus, and relays requests from frontend components to the narrative backend to be
* completed by the kernel.
*/
handleBusMessages() {
const bus = this.runtime.bus();
Expand Down Expand Up @@ -192,11 +194,12 @@ define([
* If no arguments are supplied to sendCommMessage, the stored messages (if any)
* will be sent.
*
* @param {string} msgType - message type; will be one of the
* values in the REQUESTS object (optional)
* @param {string} msgType - message type; will be one of the
* values in the REQUESTS object (optional)
*
* @param {object} msgData - additional parameters for the request,
* such as jobId or jobIdList, or an 'options' object (optional)
* @param {object} msgData - additional parameters for the request,
* such as one of the values in the PARAMS object or
* a request-specific param (optional)
*/
sendCommMessage(msgType, msgData) {
if (msgType && msgData) {
Expand Down Expand Up @@ -244,9 +247,7 @@ define([
* }
* }
* }
* Where msg_type is one of:
* start, new_job, job_status, job_status_all, job_comm_err, job_init_err, job_init_lookup_err,
* or any of the values of RESPONSES
* where msg_type is one of the values of RESPONSES
*
* @param {object} msg
*/
Expand Down
1 change: 1 addition & 0 deletions kbase-extension/static/kbase/js/common/jobManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ define([
channelObject = { [jcm.CHANNELS.CELL]: channel };
}

// listen for job-related bus messages
this.listeners[channel][type] = this.bus.listen({
channel: channelObject,
key: {
Expand Down
1 change: 1 addition & 0 deletions kbase-extension/static/kbase/js/util/jobLogViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ define([
Object.keys(handlers).forEach((type) => {
// ensure that the correct `this` context is bound
const handle = handlers[type].bind(this);
// listen for job-related bus messages
this.listenersByType[jcm.RESPONSES[type]] = this.bus.listen({
channel: { [jcm.CHANNELS.JOB]: this.jobId },
key: { type: jcm.RESPONSES[type] },
Expand Down
1 change: 1 addition & 0 deletions kbase-extension/static/kbase/js/util/jobStateViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ define([
*/
function listenForJobStatus() {
listeners.push(
// listen for job-related bus messages
runtime.bus().listen({
channel: {
[jcm.CHANNELS.JOB]: jobId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ define(['bluebird', 'jquery', 'base/js/namespace', 'common/runtime', 'common/job
},
}
);
// listen for cell-related bus messages
runtime.bus().listen({
channel: {
[jcm.CHANNELS.CELL]: cellId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ define([
Semaphore.make()
.when('comm', 'ready', Config.get('comm_wait_timeout'))
.then(() => {
// listen for job-related bus messages
this.busConnection.listen({
channel: {
[jcm.CHANNELS.JOB]: this.jobId,
Expand All @@ -153,6 +154,7 @@ define([
}.bind(this),
});

// listen for job-related bus messages
this.busConnection.listen({
channel: {
[jcm.CHANNELS.JOB]: this.jobId,
Expand Down
1 change: 1 addition & 0 deletions nbextensions/appCell2/widgets/appCellWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,7 @@ define(

function startListeningForJobMessages(jobId) {
jobListeners.push(
// listen for job-related bus messages
runtime.bus().listen({
channel: {
jobId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ define([
}

function startParamsListener() {
// listen for job-related bus messages
paramsListener = runtime.bus().listen({
channel: {
[jcm.CHANNELS.JOB]: jobId,
Expand Down
1 change: 1 addition & 0 deletions nbextensions/appCell2/widgets/tabs/status/jobStateList.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ define([
}

function startParentListener() {
// listen for job-related bus messages
parentListener = runtime.bus().listen({
channel: {
[jcm.CHANNELS.JOB]: parentJobId,
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@
"*.js": [
"eslint --fix",
"prettier --ignore-unknown --write"
],
"*.py": [
"npm run flake8",
"npm run black"
]
},
"dependencies": {
Expand Down
Loading

0 comments on commit 3629d50

Please sign in to comment.