Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Extract

Manav Rathi5abf2cb35edb
diff --git a/desktop/src/main/log-worker.ts b/desktop/src/main/log-worker.ts
new file mode 100644
index 0000000000..042caa3804
--- /dev/null
+++ b/desktop/src/main/log-worker.ts
@@ -0,0 +1,42 @@
+/**
+ * A object that behaves similar to the default export of "./log", except this
+ * can be used from within a utility process.
+ *
+ * ---
+ *
+ * We cannot directly do
+ *
+ * import log from "../log";
+ *
+ * because that requires the Electron APIs that are not available to a utility
+ * process (See: [Note: Using Electron APIs in UtilityProcess]).
+ *
+ * But even if that were to work, logging will still be problematic since we'd
+ * try opening the log file from two different Node.js processes (this one, and
+ * the main one), and I didn't find any indication in the electron-log
+ * repository that the log file's integrity would be maintained in such cases.
+ *
+ * So instead we provide this proxy log object that uses the
+ * `process.parentPort` to transport the logs over to the main process, where
+ * the {@link processUtilityProcessLogMessage} function in the main process is
+ * expected to handle these (sending them to the actual log).
+ */
+export default {
+ /**
+ * Unlike the real {@link log.error}, this accepts only the first string
+ * argument, not the second optional error one.
+ */
+ errorString: (s: string) => mainProcess("log.errorString", s),
+ info: (...ms: unknown[]) => mainProcess("log.info", ms),
+ /**
+ * Unlike the real {@link log.debug}, this is (a) eagerly evaluated, and (b)
+ * accepts only strings.
+ */
+ debugString: (s: string) => mainProcess("log.debugString", s),
+};
+
+/**
+ * Send a message to the main process using a barebones RPC protocol.
+ */
+const mainProcess = (method: string, param: unknown) =>
+ process.parentPort.postMessage({ method, p: param });
diff --git a/desktop/src/main/log.ts b/desktop/src/main/log.ts
index 479b3c1666..e4a3029fc6 100644
--- a/desktop/src/main/log.ts
+++ b/desktop/src/main/log.ts
@@ -83,6 +83,50 @@ const logDebug = (param: () => unknown) => {
}
};
+/**
+ * Handle log messages posted from the utility process in the main process.
+ *
+ * See: [Note: Using Electron APIs in UtilityProcess]
+ *
+ * @param message The arbitrary message that was received as an argument to the
+ * "message" event invoked on a {@link UtilityProcess}.
+ *
+ * @returns true if the message was recognized and handled, and false otherwise.
+ */
+export const processUtilityProcessLogMessage = (
+ logTag: string,
+ message: unknown,
+) => {
+ const m = message; /* shorter alias */
+ if (m && typeof m == "object" && "method" in m && "p" in m) {
+ const p = m.p;
+ switch (m.method) {
+ case "log.errorString":
+ if (typeof p == "string") {
+ log.error(`${logTag} ${p}`);
+ return true;
+ }
+ break;
+ case "log.info":
+ if (Array.isArray(p)) {
+ // Need to cast from any[] to unknown[]
+ log.info(logTag, ...(p as unknown[]));
+ return true;
+ }
+ break;
+ case "log.debugString":
+ if (typeof p == "string") {
+ log.debug(() => `${logTag} ${p}`);
+ return true;
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ return false;
+};
+
/**
* Ente's logger.
*
diff --git a/desktop/src/main/services/ml-worker.ts b/desktop/src/main/services/ml-worker.ts
index 5158925d31..3397231fe7 100644
--- a/desktop/src/main/services/ml-worker.ts
+++ b/desktop/src/main/services/ml-worker.ts
@@ -15,47 +15,13 @@ import { existsSync } from "fs";
import fs from "node:fs/promises";
import path from "node:path";
import * as ort from "onnxruntime-node";
+import log from "../log-worker";
import { messagePortMainEndpoint } from "../utils/comlink";
import { wait } from "../utils/common";
import { writeStream } from "../utils/stream";
import { fsStatMtime } from "./fs";
-/**
- * We cannot do
- *
- * import log from "../log";
- *
- * because that requires the Electron APIs that are not available to a utility
- * process (See: [Note: Using Electron APIs in UtilityProcess]). But even if
- * that were to work, logging will still be problematic since we'd try opening
- * the log file from two different Node.js processes (this one, and the main
- * one), and I didn't find any indication in the electron-log repository that
- * the log file's integrity would be maintained in such cases.
- *
- * So instead we create this proxy log object that uses `process.parentPort` to
- * transport the logs over to the main process.
- */
-const log = {
- /**
- * Unlike the real {@link log.error}, this accepts only the first string
- * argument, not the second optional error one.
- */
- errorString: (s: string) => mainProcess("log.errorString", s),
- info: (...ms: unknown[]) => mainProcess("log.info", ms),
- /**
- * Unlike the real {@link log.debug}, this is (a) eagerly evaluated, and (b)
- * accepts only strings.
- */
- debugString: (s: string) => mainProcess("log.debugString", s),
-};
-
-/**
- * Send a message to the main process using a barebones RPC protocol.
- */
-const mainProcess = (method: string, param: unknown) =>
- process.parentPort.postMessage({ method, p: param });
-
-log.debugString(`Started ML worker process`);
+log.debugString("Started ML worker process");
process.parentPort.once("message", (e) => {
// Initialize ourselves with the data we got from our parent.
diff --git a/desktop/src/main/services/ml.ts b/desktop/src/main/services/ml.ts
index d142ad5326..91a0712c0a 100644
--- a/desktop/src/main/services/ml.ts
+++ b/desktop/src/main/services/ml.ts
@@ -9,7 +9,7 @@ import {
} from "electron";
import { app, utilityProcess } from "electron/main";
import path from "node:path";
-import log from "../log";
+import log, { processUtilityProcessLogMessage } from "../log";
/** The active ML worker (utility) process, if any. */
let _child: UtilityProcess | undefined;
@@ -114,33 +114,9 @@ export const createMLWorker = (window: BrowserWindow) => {
* we use the `parentPort` in the utility process.
*/
const handleMessagesFromUtilityProcess = (child: UtilityProcess) => {
- const logTag = "[ml-worker]";
child.on("message", (m: unknown) => {
- if (m && typeof m == "object" && "method" in m && "p" in m) {
- const p = m.p;
- switch (m.method) {
- case "log.errorString":
- if (typeof p == "string") {
- log.error(`${logTag} ${p}`);
- return;
- }
- break;
- case "log.info":
- if (Array.isArray(p)) {
- // Need to cast from any[] to unknown[]
- log.info(logTag, ...(p as unknown[]));
- return;
- }
- break;
- case "log.debugString":
- if (typeof p == "string") {
- log.debug(() => `${logTag} ${p}`);
- return;
- }
- break;
- default:
- break;
- }
+ if (processUtilityProcessLogMessage("[ml-worker]", m)) {
+ return;
}
log.info("Ignoring unknown message from ML worker", m);
});