Skip to content

armgit / ente

Public Imported from GitHub
0

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

Logging 1

Manav Rathi37367f72603e
diff --git a/desktop/src/main/services/ml-util-test.ts b/desktop/src/main/services/ml-util-test.ts
index 34bed7eec1..9d312a4d04 100644
--- a/desktop/src/main/services/ml-util-test.ts
+++ b/desktop/src/main/services/ml-util-test.ts
@@ -1,30 +1,39 @@
+//
+import { ensure, wait } from "../utils/common";
+
/**
- * [Note: Using Electron APIs in UtilityProcess]
+ * We cannot do
*
- * Only a small subset of the Electron APIs are available to a UtilityProcess.
- * As of writing (Jul 2024, Electron 30), only the following are available:
- * - net
- * - systemPreferences
+ * import log from "../log";
*
- * In particular, `app` is not available.
+ * 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.
*
- * We structure our code so that it doesn't need anything apart from `net`.
+ * So instead we create this proxy log object that uses `process.parentPort` to
+ * transport the logs over to the main process.
*/
-
-// import log from "../log";
-import { ensure, wait } from "../utils/common";
-
const log = {
- info: (...ms: unknown[]) => console.log(...ms),
+ info: (...ms: unknown[]) => mainProcess("log.info", ms),
debug: (fn: () => unknown) => console.log(fn()),
};
+/**
+ * Send a message to the main process using a barebones protocol.
+ */
+const mainProcess = (method: string, params: unknown[]) => {
+ process.parentPort.postMessage({ method, params });
+};
+
log.debug(() => "Started ML worker process");
process.parentPort.once("message", (e) => {
const port = ensure(e.ports[0]);
port.on("message", (event) => {
- void handleMessage(event.data).then((response) => {
+ void handleMessageFromRenderer(event.data).then((response) => {
if (response) port.postMessage(response);
});
});
@@ -36,7 +45,7 @@ process.parentPort.once("message", (e) => {
*
* Sibling of the electronMLWorker function (in `ml/worker.ts`) in the web code.
*/
-const handleMessage = async (m: unknown) => {
+const handleMessageFromRenderer = async (m: unknown) => {
if (m && typeof m == "object" && "type" in m && "id" in m) {
const id = m.id;
switch (m.type) {
diff --git a/desktop/src/main/services/ml.ts b/desktop/src/main/services/ml.ts
index 05de34fc49..cb7ac17d68 100644
--- a/desktop/src/main/services/ml.ts
+++ b/desktop/src/main/services/ml.ts
@@ -2,9 +2,14 @@
* @file ML related functionality. This code runs in the main process.
*/
-import { MessageChannelMain, type BrowserWindow } from "electron";
+import {
+ MessageChannelMain,
+ type BrowserWindow,
+ type UtilityProcess,
+} from "electron";
import { utilityProcess } from "electron/main";
import path from "node:path";
+import log from "../log";
/**
* Create a new ML worker process.
@@ -58,7 +63,6 @@ import path from "node:path";
* worker can directly talk to each other!
*
* Node.js utility process <-> Renderer web worker
- *
*/
export const createMLWorker = (window: BrowserWindow) => {
const { port1, port2 } = new MessageChannelMain();
@@ -67,4 +71,40 @@ export const createMLWorker = (window: BrowserWindow) => {
child.postMessage(undefined, [port1]);
window.webContents.postMessage("createMLWorker/port", undefined, [port2]);
+
+ handleMLWorkerRequests(child);
+};
+
+/**
+ * Handle requests from the utility process.
+ *
+ * [Note: Using Electron APIs in UtilityProcess]
+ *
+ * Only a small subset of the Electron APIs are available to a UtilityProcess.
+ * As of writing (Jul 2024, Electron 30), only the following are available:
+ *
+ * - net
+ * - systemPreferences
+ *
+ * In particular, `app` is not available.
+ *
+ * We structure our code so that it doesn't need anything apart from `net`.
+ *
+ * For the other cases,
+ *
+ * - When we need to communicate from the utility process to the main process,
+ * we use the `parentPort` in the utility process.
+ */
+const handleMLWorkerRequests = (child: UtilityProcess) => {
+ child.on("message", (m: unknown) => {
+ if (m && typeof m == "object" && "method" in m) {
+ switch (m.method) {
+ default:
+ break;
+ }
+ }
+
+ log.info("Ignoring unexpected message", m);
+ return undefined;
+ });
};
diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts
index 68a1e81c21..540b15eafd 100644
--- a/web/packages/new/photos/services/ml/worker.ts
+++ b/web/packages/new/photos/services/ml/worker.ts
@@ -69,7 +69,8 @@ const IPCResponse = z.object({
/**
* Our hand-rolled IPC handler and router - the web worker end.
*
- * Sibling of the handleMessage function (in `ml-worker.ts`) in the desktop app.
+ * Sibling of the handleMessageFromRenderer function (in `ml-worker.ts`) in the
+ * desktop code.
*/
const electronMLWorker = async (type: string, data: string) => {
const port = _port;