Reviewed Input and Output Waiters and improved logging in workers

This commit is contained in:
n1474335 2023-01-18 18:07:06 +00:00
parent 56d1a016da
commit 91f1be8c70
12 changed files with 204 additions and 111 deletions

26
package-lock.json generated
View File

@ -5843,11 +5843,14 @@
},
"node_modules/es6-object-assign": {
"version": "1.1.0",
"license": "MIT"
"resolved": "https://registry.npmjs.org/es6-object-assign/-/es6-object-assign-1.1.0.tgz",
"integrity": "sha512-MEl9uirslVwqQU369iHNWZXsI8yaZYGg/D65aOgZkeyFJwHYSxilf7rQzXKI7DdDuBPrBXbfk3sl9hJhmd5AUw=="
},
"node_modules/es6-polyfills": {
"version": "2.0.0",
"license": " LGPL-3.0+",
"resolved": "https://registry.npmjs.org/es6-polyfills/-/es6-polyfills-2.0.0.tgz",
"integrity": "sha512-daIt/MHqdYmxnuo5KcwAU9EqSxvaDRyajYOUU9fy+CLuU5+RFhpNCnL3oPsq7n+g673F3z/Vb+FXo/EmQjlkbw==",
"deprecated": "Use @natlibfi/es6-polyfills instead",
"dependencies": {
"es6-object-assign": "^1.0.3",
"es6-promise-polyfill": "^1.2.0"
@ -5855,7 +5858,8 @@
},
"node_modules/es6-promise-polyfill": {
"version": "1.2.0",
"license": "MIT"
"resolved": "https://registry.npmjs.org/es6-promise-polyfill/-/es6-promise-polyfill-1.2.0.tgz",
"integrity": "sha512-HHb0vydCpoclpd0ySPkRXMmBw80MRt1wM4RBJBlXkux97K7gleabZdsR0gvE1nNPM9mgOZIBTzjjXiPxf4lIqQ=="
},
"node_modules/es6-promisify": {
"version": "7.0.0",
@ -9252,7 +9256,9 @@
},
"node_modules/loglevel-message-prefix": {
"version": "3.0.0",
"license": "MIT",
"resolved": "https://registry.npmjs.org/loglevel-message-prefix/-/loglevel-message-prefix-3.0.0.tgz",
"integrity": "sha512-/cBEOqsuU0vJsFm4n92R7h6mkiKqt8vh+JOmW722DTZVVD7egEpVOx66re3vWxO7pii3B4eQuqm2qfqq5cAs0w==",
"deprecated": "Use @natlibfi/loglevel-message-prefix instead",
"dependencies": {
"es6-polyfills": "^2.0.0",
"loglevel": "^1.4.0"
@ -17690,17 +17696,23 @@
"dev": true
},
"es6-object-assign": {
"version": "1.1.0"
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/es6-object-assign/-/es6-object-assign-1.1.0.tgz",
"integrity": "sha512-MEl9uirslVwqQU369iHNWZXsI8yaZYGg/D65aOgZkeyFJwHYSxilf7rQzXKI7DdDuBPrBXbfk3sl9hJhmd5AUw=="
},
"es6-polyfills": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/es6-polyfills/-/es6-polyfills-2.0.0.tgz",
"integrity": "sha512-daIt/MHqdYmxnuo5KcwAU9EqSxvaDRyajYOUU9fy+CLuU5+RFhpNCnL3oPsq7n+g673F3z/Vb+FXo/EmQjlkbw==",
"requires": {
"es6-object-assign": "^1.0.3",
"es6-promise-polyfill": "^1.2.0"
}
},
"es6-promise-polyfill": {
"version": "1.2.0"
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/es6-promise-polyfill/-/es6-promise-polyfill-1.2.0.tgz",
"integrity": "sha512-HHb0vydCpoclpd0ySPkRXMmBw80MRt1wM4RBJBlXkux97K7gleabZdsR0gvE1nNPM9mgOZIBTzjjXiPxf4lIqQ=="
},
"es6-promisify": {
"version": "7.0.0"
@ -19960,6 +19972,8 @@
},
"loglevel-message-prefix": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/loglevel-message-prefix/-/loglevel-message-prefix-3.0.0.tgz",
"integrity": "sha512-/cBEOqsuU0vJsFm4n92R7h6mkiKqt8vh+JOmW722DTZVVD7egEpVOx66re3vWxO7pii3B4eQuqm2qfqq5cAs0w==",
"requires": {
"es6-polyfills": "^2.0.0",
"loglevel": "^1.4.0"

View File

@ -9,16 +9,8 @@
import Chef from "./Chef.mjs";
import OperationConfig from "./config/OperationConfig.json" assert {type: "json"};
import OpModules from "./config/modules/OpModules.mjs";
// Add ">" to the start of all log messages in the Chef Worker
import loglevelMessagePrefix from "loglevel-message-prefix";
loglevelMessagePrefix(log, {
prefixes: [],
staticPrefixes: [">"],
prefixFormat: "%p"
});
// Set up Chef instance
self.chef = new Chef();
@ -56,7 +48,7 @@ self.postMessage({
self.addEventListener("message", function(e) {
// Handle message
const r = e.data;
log.debug("ChefWorker receiving command '" + r.action + "'");
log.debug(`Receiving command '${r.action}'`);
switch (r.action) {
case "bake":
@ -86,6 +78,12 @@ self.addEventListener("message", function(e) {
case "setLogLevel":
log.setLevel(r.data, false);
break;
case "setLogPrefix":
loglevelMessagePrefix(log, {
prefixes: [],
staticPrefixes: [r.data]
});
break;
default:
break;
}

View File

@ -472,7 +472,7 @@ class Utils {
* Utils.strToArrayBuffer("你好");
*/
static strToArrayBuffer(str) {
log.debug("Converting string to array buffer");
log.debug(`Converting string[${str?.length}] to array buffer`);
if (!str) return new ArrayBuffer;
const arr = new Uint8Array(str.length);
let i = str.length, b;
@ -500,7 +500,7 @@ class Utils {
* Utils.strToUtf8ArrayBuffer("你好");
*/
static strToUtf8ArrayBuffer(str) {
log.debug("Converting string to UTF8 array buffer");
log.debug(`Converting string[${str?.length}] to UTF8 array buffer`);
if (!str) return new ArrayBuffer;
const utf8Str = utf8.encode(str);
@ -531,7 +531,7 @@ class Utils {
* Utils.strToByteArray("你好");
*/
static strToByteArray(str) {
log.debug("Converting string to byte array");
log.debug(`Converting string[${str?.length}] to byte array`);
if (!str) return [];
const byteArray = new Array(str.length);
let i = str.length, b;
@ -559,7 +559,7 @@ class Utils {
* Utils.strToUtf8ByteArray("你好");
*/
static strToUtf8ByteArray(str) {
log.debug("Converting string to UTF8 byte array");
log.debug(`Converting string[${str?.length}] to UTF8 byte array`);
if (!str) return [];
const utf8Str = utf8.encode(str);
@ -589,7 +589,7 @@ class Utils {
* Utils.strToCharcode("你好");
*/
static strToCharcode(str) {
log.debug("Converting string to charcode");
log.debug(`Converting string[${str?.length}] to charcode`);
if (!str) return [];
const charcode = [];
@ -625,7 +625,7 @@ class Utils {
* Utils.byteArrayToUtf8([228,189,160,229,165,189]);
*/
static byteArrayToUtf8(byteArray) {
log.debug("Converting byte array to UTF8");
log.debug(`Converting byte array[${byteArray?.length}] to UTF8`);
if (!byteArray || !byteArray.length) return "";
const str = Utils.byteArrayToChars(byteArray);
try {
@ -659,7 +659,7 @@ class Utils {
* Utils.byteArrayToChars([20320,22909]);
*/
static byteArrayToChars(byteArray) {
log.debug("Converting byte array to chars");
log.debug(`Converting byte array[${byteArray?.length}] to chars`);
if (!byteArray || !byteArray.length) return "";
let str = "";
// String concatenation appears to be faster than an array join
@ -682,7 +682,7 @@ class Utils {
* Utils.arrayBufferToStr(Uint8Array.from([104,101,108,108,111]).buffer);
*/
static arrayBufferToStr(arrayBuffer, utf8=true) {
log.debug("Converting array buffer to str");
log.debug(`Converting array buffer[${arrayBuffer?.byteLength}] to str`);
if (!arrayBuffer || !arrayBuffer.byteLength) return "";
const arr = new Uint8Array(arrayBuffer);
return utf8 ? Utils.byteArrayToUtf8(arr) : Utils.byteArrayToChars(arr);

View File

@ -35,6 +35,14 @@ class BackgroundWorkerWaiter {
log.debug("Registering new background ChefWorker");
this.chefWorker = new ChefWorker();
this.chefWorker.addEventListener("message", this.handleChefMessage.bind(this));
this.chefWorker.postMessage({
action: "setLogPrefix",
data: "BGChefWorker"
});
this.chefWorker.postMessage({
action: "setLogLevel",
data: log.getLevel()
});
let docURL = document.location.href.split(/[#?]/)[0];
const index = docURL.lastIndexOf("/");
@ -52,7 +60,7 @@ class BackgroundWorkerWaiter {
*/
handleChefMessage(e) {
const r = e.data;
log.debug("Receiving '" + r.action + "' from ChefWorker in the background");
log.debug(`Receiving '${r.action}' from BGChefWorker`);
switch (r.action) {
case "bakeComplete":
@ -152,6 +160,18 @@ class BackgroundWorkerWaiter {
this.manager.output.backgroundMagicResult(response.dish.value);
}
/**
* Sets the console log level in the workers.
*/
setLogLevel() {
if (!this.chefWorker) return;
this.chefWorker.postMessage({
action: "setLogLevel",
data: log.getLevel()
});
}
}

View File

@ -305,11 +305,20 @@ class InputWaiter {
if (this.loaderWorkers.length === this.maxWorkers) {
return -1;
}
log.debug("Adding new LoaderWorker.");
log.debug(`Adding new LoaderWorker (${this.loaderWorkers.length + 1}/${this.maxWorkers}).`);
const newWorker = new LoaderWorker();
const workerId = this.workerId++;
newWorker.addEventListener("message", this.handleLoaderMessage.bind(this));
newWorker.postMessage({id: workerId});
newWorker.postMessage({
action: "setLogLevel",
data: log.getLevel()
});
newWorker.postMessage({
action: "setID",
data: {
id: workerId
}
});
const newWorkerObj = {
worker: newWorker,
id: workerId
@ -374,8 +383,11 @@ class InputWaiter {
const idx = this.getLoaderWorkerIndex(inputData.workerId);
if (idx === -1) return;
this.loaderWorkers[idx].worker.postMessage({
file: inputData.file,
inputNum: inputData.inputNum
action: "loadFile",
data: {
file: inputData.file,
inputNum: inputData.inputNum
}
});
}
@ -402,7 +414,7 @@ class InputWaiter {
/**
* Handler for messages sent back by the inputWorker
* Handler for messages sent back by the InputWorker
*
* @param {MessageEvent} e
*/
@ -414,7 +426,7 @@ class InputWaiter {
return;
}
log.debug(`Receiving ${r.action} from InputWorker.`);
log.debug(`Receiving '${r.action}' from InputWorker.`);
switch (r.action) {
case "activateLoaderWorker":
@ -802,7 +814,6 @@ class InputWaiter {
else delay = 500;
debounce(function(e) {
console.log("inputChange", inputLength, delay);
const value = this.getInput();
const activeTab = this.manager.tabs.getActiveTab("input");
@ -1092,11 +1103,16 @@ class InputWaiter {
}
/**
* Sets the console log level in the worker.
*
* @param {string} level
* Sets the console log level in the workers.
*/
setLogLevel(level) {
setLogLevel() {
this.loaderWorkers.forEach(w => {
w.postMessage({
action: "setLogLevel",
data: log.getLevel()
});
});
if (!this.inputWorker) return;
this.inputWorker.postMessage({
action: "setLogLevel",

View File

@ -180,6 +180,8 @@ class OptionsWaiter {
log.setLevel(level, false);
this.manager.worker.setLogLevel();
this.manager.input.setLogLevel();
this.manager.output.setLogLevel();
this.manager.background.setLogLevel();
}
}

View File

@ -146,13 +146,11 @@ class OutputWaiter {
* @param {string} eolVal
*/
eolChange(eolVal) {
const oldOutputVal = this.getOutput();
const currentTabNum = this.manager.tabs.getActiveTab("output");
if (currentTabNum >= 0) {
this.outputs[currentTabNum].eolSequence = eolVal;
} else {
throw new Error("Cannot change output eol sequence to " + eolVal);
throw new Error(`Cannot change output ${currentTabNum} EOL sequence to ${eolVal}`);
}
// Update the EOL value
@ -161,7 +159,7 @@ class OutputWaiter {
});
// Reset the output so that lines are recalculated, preserving the old EOL values
this.setOutput(oldOutputVal, true);
this.setOutput(this.currentOutputCache, true);
// Update the URL manually since we aren't firing a statechange event
this.app.updateURL(true);
}
@ -191,7 +189,7 @@ class OutputWaiter {
if (currentTabNum >= 0) {
this.outputs[currentTabNum].encoding = chrEncVal;
} else {
throw new Error("Cannot change output chrEnc to " + chrEncVal);
throw new Error(`Cannot change output ${currentTabNum} chrEnc to ${chrEncVal}`);
}
// Reset the output, forcing it to re-decode the data with the new character encoding
@ -224,16 +222,6 @@ class OutputWaiter {
});
}
/**
* Gets the value of the current output
* @returns {string}
*/
getOutput() {
const doc = this.outputEditorView.state.doc;
const eol = this.outputEditorView.state.lineBreak;
return doc.sliceString(0, doc.length, eol);
}
/**
* Sets the value of the current output
* @param {string|ArrayBuffer} data
@ -498,7 +486,7 @@ class OutputWaiter {
removeOutput(inputNum) {
if (!this.outputExists(inputNum)) return;
delete (this.outputs[inputNum]);
delete this.outputs[inputNum];
}
/**
@ -600,7 +588,7 @@ class OutputWaiter {
}
/**
* Retrieves the dish as a string, returning the cached version if possible.
* Retrieves the dish as a string
*
* @param {Dish} dish
* @returns {string}
@ -614,7 +602,7 @@ class OutputWaiter {
}
/**
* Retrieves the dish as an ArrayBuffer, returning the cached version if possible.
* Retrieves the dish as an ArrayBuffer
*
* @param {Dish} dish
* @returns {ArrayBuffer}
@ -719,12 +707,12 @@ class OutputWaiter {
const data = await dish.get(Dish.ARRAY_BUFFER),
file = new File([data], fileName);
FileSaver.saveAs(file, fileName, false);
FileSaver.saveAs(file, fileName, {autoBom: false});
}
/**
* Handler for save all click event
* Saves all outputs to a single archvie file
* Saves all outputs to a single archive file
*/
async saveAllClick() {
const downloadButton = document.getElementById("save-all-to-file");
@ -745,7 +733,6 @@ class OutputWaiter {
}
}
/**
* Spawns a new ZipWorker and sends it the outputs so that they can
* be zipped for download
@ -801,9 +788,16 @@ class OutputWaiter {
log.debug("Creating ZipWorker");
this.zipWorker = new ZipWorker();
this.zipWorker.postMessage({
outputs: this.outputs,
filename: fileName,
fileExtension: fileExt
action: "setLogLevel",
data: log.getLevel()
});
this.zipWorker.postMessage({
action: "zipFiles",
data: {
outputs: this.outputs,
filename: fileName,
fileExtension: fileExt
}
});
this.zipWorker.addEventListener("message", this.handleZipWorkerMessage.bind(this));
}
@ -820,16 +814,12 @@ class OutputWaiter {
this.zipWorker = null;
const downloadButton = document.getElementById("save-all-to-file");
downloadButton.classList.remove("spin");
downloadButton.title = "Save all outputs to a zip file";
downloadButton.setAttribute("data-original-title", "Save all outputs to a zip file");
downloadButton.firstElementChild.innerHTML = "archive";
}
/**
* Handle messages sent back by the ZipWorker
*/
@ -847,7 +837,7 @@ class OutputWaiter {
}
const file = new File([r.zippedFile], r.filename);
FileSaver.saveAs(file, r.filename, false);
FileSaver.saveAs(file, r.filename, {autoBom: false});
this.terminateZipWorker();
}
@ -1050,9 +1040,7 @@ class OutputWaiter {
nums.push(newNum);
}
}
nums.sort(function(a, b) {
return a - b;
});
nums.sort((a, b) => a - b); // Forces the sort function to treat a and b as numbers
return nums;
}
@ -1139,6 +1127,7 @@ class OutputWaiter {
/**
* Redraw the entire tab bar to remove any outdated tabs
*
* @param {number} activeTab
* @param {string} direction - Either "left" or "right"
*/
@ -1152,7 +1141,6 @@ class OutputWaiter {
for (let i = 0; i < newNums.length; i++) {
this.displayTabInfo(newNums[i]);
}
}
/**
@ -1297,7 +1285,7 @@ class OutputWaiter {
return;
}
const output = await dish.get(Dish.STRING);
const output = await this.getDishStr(dish);
const self = this;
navigator.clipboard.writeText(output).then(function() {
@ -1313,8 +1301,8 @@ class OutputWaiter {
*/
async switchClick() {
const activeTab = this.manager.tabs.getActiveTab("output");
const switchButton = document.getElementById("switch");
switchButton.classList.add("spin");
switchButton.disabled = true;
switchButton.firstElementChild.innerHTML = "autorenew";
@ -1480,6 +1468,18 @@ class OutputWaiter {
$("#output-tab-modal").modal("hide");
this.changeTab(inputNum, this.app.options.syncTabs);
}
/**
* Sets the console log level in the workers.
*/
setLogLevel() {
if (!this.zipWorker) return;
this.zipWorker.postMessage({
action: "setLogLevel",
data: log.getLevel()
});
}
}
export default OutputWaiter;

View File

@ -72,6 +72,10 @@ class WorkerWaiter {
this.dishWorker.worker = new DishWorker();
this.dishWorker.worker.addEventListener("message", this.handleDishMessage.bind(this));
this.dishWorker.worker.postMessage({
action: "setLogLevel",
data: log.getLevel()
});
if (this.dishWorkerQueue.length > 0) {
this.postDishMessage(this.dishWorkerQueue.splice(0, 1)[0]);
@ -89,22 +93,27 @@ class WorkerWaiter {
return -1;
}
log.debug("Adding new ChefWorker");
log.debug(`Adding new ChefWorker (${this.chefWorkers.length + 1}/${this.maxWorkers})`);
// Create a new ChefWorker and send it the docURL
const newWorker = new ChefWorker();
newWorker.addEventListener("message", this.handleChefMessage.bind(this));
newWorker.postMessage({
action: "setLogPrefix",
data: "ChefWorker"
});
newWorker.postMessage({
action: "setLogLevel",
data: log.getLevel()
});
let docURL = document.location.href.split(/[#?]/)[0];
const index = docURL.lastIndexOf("/");
if (index > 0) {
docURL = docURL.substring(0, index);
}
newWorker.postMessage({"action": "docURL", "data": docURL});
newWorker.postMessage({
action: "setLogLevel",
data: log.getLevel()
});
// Store the worker, whether or not it's active, and the inputNum as an object
const newWorkerObj = {
@ -177,7 +186,7 @@ class WorkerWaiter {
handleChefMessage(e) {
const r = e.data;
let inputNum = 0;
log.debug(`Receiving ${r.action} from ChefWorker.`);
log.debug(`Receiving '${r.action}' from ChefWorker.`);
if (Object.prototype.hasOwnProperty.call(r.data, "inputNum")) {
inputNum = r.data.inputNum;
@ -626,7 +635,7 @@ class WorkerWaiter {
*/
handleDishMessage(e) {
const r = e.data;
log.debug(`Receiving ${r.action} from DishWorker`);
log.debug(`Receiving '${r.action}' from DishWorker`);
switch (r.action) {
case "dishReturned":
@ -729,12 +738,18 @@ class WorkerWaiter {
* Sets the console log level in the workers.
*/
setLogLevel() {
for (let i = 0; i < this.chefWorkers.length; i++) {
this.chefWorkers[i].worker.postMessage({
this.chefWorkers.forEach(w => {
w.postMessage({
action: "setLogLevel",
data: log.getLevel()
});
}
});
if (!this.dishWorker.worker) return;
this.dishWorker.worker.postMessage({
action: "setLogLevel",
data: log.getLevel()
});
}
/**

View File

@ -7,11 +7,17 @@
*/
import Dish from "../../core/Dish.mjs";
import loglevelMessagePrefix from "loglevel-message-prefix";
loglevelMessagePrefix(log, {
prefixes: [],
staticPrefixes: ["DishWorker"]
});
self.addEventListener("message", function(e) {
// Handle message from the main thread
const r = e.data;
log.debug(`DishWorker receiving command '${r.action}'`);
log.debug(`Receiving command '${r.action}'`);
switch (r.action) {
case "getDishAs":
@ -20,8 +26,11 @@ self.addEventListener("message", function(e) {
case "getDishTitle":
getDishTitle(r.data);
break;
case "setLogLevel":
log.setLevel(r.data, false);
break;
default:
log.error(`DishWorker sent invalid action: '${r.action}'`);
log.error(`Unknown action: '${r.action}'`);
}
});

View File

@ -9,6 +9,12 @@
*/
import Utils from "../../core/Utils.mjs";
import loglevelMessagePrefix from "loglevel-message-prefix";
loglevelMessagePrefix(log, {
prefixes: [],
staticPrefixes: ["InputWorker"]
});
// Default max values
// These will be correctly calculated automatically
@ -52,7 +58,7 @@ self.addEventListener("message", function(e) {
return;
}
log.debug(`Receiving ${r.action} from InputWaiter.`);
log.debug(`Receiving command '${r.action}'`);
switch (r.action) {
case "loadUIFiles":

View File

@ -6,32 +6,36 @@
* @license Apache-2.0
*/
import loglevelMessagePrefix from "loglevel-message-prefix";
loglevelMessagePrefix(log, {
prefixes: [],
staticPrefixes: ["LoaderWorker"]
});
self.id = null;
self.handleMessage = function(e) {
const r = e.data;
log.debug(`LoaderWorker receiving command '${r.action}'`);
switch (r.action) {
case "loadInput":
self.loadFile(r.data.file, r.data.inputNum);
break;
}
};
/**
* Respond to message from parent thread.
*/
self.addEventListener("message", function(e) {
// Handle message
const r = e.data;
if (Object.prototype.hasOwnProperty.call(r, "file") && Object.prototype.hasOwnProperty.call(r, "inputNum")) {
self.loadFile(r.file, r.inputNum);
} else if (Object.prototype.hasOwnProperty.call(r, "file")) {
self.loadFile(r.file, "");
} else if (Object.prototype.hasOwnProperty.call(r, "id")) {
self.id = r.id;
log.debug(`Receiving command '${r.action}'`);
switch (r.action) {
case "setID":
self.id = r.data.id;
break;
case "loadFile":
self.loadFile(r.data.file, r.data?.inputNum ?? "");
break;
case "setLogLevel":
log.setLevel(r.data, false);
break;
default:
log.error(`Unknown action '${r.action}'.`);
}
});

View File

@ -10,6 +10,12 @@ import zip from "zlibjs/bin/zip.min.js";
import Utils from "../../core/Utils.mjs";
import Dish from "../../core/Dish.mjs";
import {detectFileType} from "../../core/lib/FileType.mjs";
import loglevelMessagePrefix from "loglevel-message-prefix";
loglevelMessagePrefix(log, {
prefixes: [],
staticPrefixes: ["ZipWorker"],
});
const Zlib = zip.Zlib;
@ -17,17 +23,20 @@ const Zlib = zip.Zlib;
* Respond to message from parent thread.
*/
self.addEventListener("message", function(e) {
// Handle message from the main thread
const r = e.data;
if (!("outputs" in r)) {
log.error("No files were passed to the ZipWorker.");
return;
}
if (!("filename" in r)) {
log.error("No filename was passed to the ZipWorker");
return;
}
log.debug(`Receiving command '${r.action}'`);
self.zipFiles(r.outputs, r.filename, r.fileExtension);
switch (r.action) {
case "zipFiles":
self.zipFiles(r.data.outputs, r.data.filename, r.data.fileExtension);
break;
case "setLogLevel":
log.setLevel(r.data, false);
break;
default:
log.error(`Unknown action: '${r.action}'`);
}
});
self.setOption = function(...args) {};