Path: blob/master/src/packages/backend/process-stats.worker.ts
6569 views
/*1* This file is part of CoCalc: Copyright © 2020–2026 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { parentPort } from "node:worker_threads";67import { scanProcessesSync } from "./process-stats-scan";8import type {9WorkerScanError,10WorkerScanRequest,11WorkerScanStarted,12WorkerScanResult,13} from "./process-stats-worker-types";1415const port = parentPort;16if (port == null) {17throw Error("process-stats.worker must run as a worker thread");18}1920port.on("message", (request: WorkerScanRequest) => {21if (request?.type !== "scan") {22return;23}24try {25const startedAtMs = Date.now();26const started: WorkerScanStarted = {27type: "scanStarted",28requestId: request.requestId,29startedAtMs,30};31port.postMessage(started);3233const result = scanProcessesSync({34timestamp: startedAtMs,35sampleKey: request.sampleKey,36procLimit: request.procLimit,37ticks: request.ticks,38pagesize: request.pagesize,39});40const response: WorkerScanResult = {41type: "scanResult",42requestId: request.requestId,43...result,44};45port.postMessage(response);46} catch (err) {47const response: WorkerScanError = {48type: "scanError",49requestId: request.requestId,50error: err instanceof Error ? (err.stack ?? err.message) : String(err),51};52port.postMessage(response);53}54});555657