Move Windows metrics process launches off the main thread
CI / verify (push) Canceled after 0s

This commit is contained in:
Sucukdeluxe committed 2026-10-20 14:42:55 +02:00
1 parent fe3797fd18
commit 290b9db8ce
6 files changed
+98 -4

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
const path = require('node:path');
const { Worker } = require('node:worker_threads');
const { runtimePerformance } = require('./performance-monitor');
class BackgroundDiskReader {
constructor({ createWorker = () => new Worker(path.join(__dirname, 'system-metrics-worker.js')), timeoutMs = 15000 } = {}) {
this.createWorker = createWorker;
this.timeoutMs = timeoutMs;
this.sequence = 0;
this.worker = null;
this.pending = null;
}
connect() {
if (this.worker) return this.worker;
const worker = this.createWorker();
this.worker = worker;
worker.on('message', message => {
if (worker !== this.worker || message.id !== this.pending?.id) return;
const pending = this.pending;
this.pending = null;
if (Number.isFinite(message.spawnMs)) runtimePerformance.record('host.spawn.worker', message.spawnMs);
pending.finish(message.error ? Error('STORAGE_QUERY_FAILED') : null, message.value);
worker.unref();
});
const failed = () => {
if (worker !== this.worker) return;
this.worker = null;
this.pending?.finish(Error('STORAGE_WORKER_FAILED'));
this.pending = null;
};
worker.on('error', failed);
worker.on('exit', failed);
worker.unref();
return worker;
}
read({ signal } = {}) {
if (signal?.aborted) return Promise.reject(Error('STORAGE_QUERY_ABORTED'));
if (this.pending) return Promise.reject(Error('STORAGE_QUERY_BUSY'));
return new Promise((resolve, reject) => {
const worker = this.connect();
const id = ++this.sequence;
let settled = false;
const finish = (error, value) => {
if (settled) return;
settled = true;
clearTimeout(timer);
signal?.removeEventListener('abort', abort);
if (error) reject(error); else resolve(value);
};
const cancel = code => {
try { worker.postMessage({ type: 'cancel', id }); } catch {}
worker.unref();
finish(Error(code));
};
const abort = () => cancel('STORAGE_QUERY_ABORTED');
const timer = setTimeout(() => cancel('STORAGE_QUERY_TIMEOUT'), this.timeoutMs);
this.pending = { id, finish };
signal?.addEventListener('abort', abort, { once: true });
worker.ref();
try { worker.postMessage({ type: 'read', id }); }
catch {
this.pending = null;
worker.unref();
finish(Error('STORAGE_WORKER_FAILED'));
}
});
}
}
const reader = new BackgroundDiskReader();
module.exports = { BackgroundDiskReader, readWindowsCountersOffThread: options => reader.read(options) };
+20
View File
@@ -0,0 +1,20 @@
const { parentPort } = require('node:worker_threads');
const { readWindowsCounters } = require('./system-metrics');
const { runtimePerformance } = require('./performance-monitor');
let active = null;
parentPort.on('message', async message => {
if (message.type === 'cancel') {
if (active?.id === message.id) active.controller.abort();
return;
}
if (message.type !== 'read' || !Number.isSafeInteger(message.id) || active) return;
const controller = new AbortController();
active = { id: message.id, controller };
let value, error = false;
try { value = await readWindowsCounters({ signal: controller.signal }); }
catch { error = true; }
const spawnMs = runtimePerformance.snapshot().operations.find(operation => operation.name === 'host.spawn.sync')?.lastMs;
active = null;
parentPort.postMessage({ id: message.id, value, error, spawnMs });
});
+2 -1
View File
@@ -3,6 +3,7 @@ const { execFile } = require('node:child_process');
const { promisify } = require('node:util'); const { promisify } = require('node:util');
const { performance } = require('node:perf_hooks'); const { performance } = require('node:perf_hooks');
const { runtimePerformance } = require('./performance-monitor'); const { runtimePerformance } = require('./performance-monitor');
const { readWindowsCountersOffThread } = require('./system-metrics-reader');
const WINDOWS_PROBE = "$ErrorActionPreference='Stop'; $ProgressPreference='SilentlyContinue'; $d=@(Get-CimInstance Win32_PerfRawData_PerfDisk_PhysicalDisk | ForEach-Object { @{ Name=$_.Name; Timestamp_PerfTime=[string]$_.Timestamp_PerfTime; Frequency_PerfTime=[string]$_.Frequency_PerfTime; Timestamp_Sys100NS=[string]$_.Timestamp_Sys100NS; PercentIdleTime=[string]$_.PercentIdleTime; DiskReadBytesPersec=[string]$_.DiskReadBytesPersec; DiskWriteBytesPersec=[string]$_.DiskWriteBytesPersec; DiskReadsPersec=[string]$_.DiskReadsPersec; DiskWritesPersec=[string]$_.DiskWritesPersec; CurrentDiskQueueLength=[string]$_.CurrentDiskQueueLength } }); $v=@(Get-CimInstance Win32_LogicalDisk -Filter 'DriveType=3' | ForEach-Object { @{ DeviceID=$_.DeviceID; Size=[string]$_.Size; FreeSpace=[string]$_.FreeSpace } }); @{disks=$d;volumes=$v} | ConvertTo-Json -Depth 4 -Compress"; const WINDOWS_PROBE = "$ErrorActionPreference='Stop'; $ProgressPreference='SilentlyContinue'; $d=@(Get-CimInstance Win32_PerfRawData_PerfDisk_PhysicalDisk | ForEach-Object { @{ Name=$_.Name; Timestamp_PerfTime=[string]$_.Timestamp_PerfTime; Frequency_PerfTime=[string]$_.Frequency_PerfTime; Timestamp_Sys100NS=[string]$_.Timestamp_Sys100NS; PercentIdleTime=[string]$_.PercentIdleTime; DiskReadBytesPersec=[string]$_.DiskReadBytesPersec; DiskWriteBytesPersec=[string]$_.DiskWriteBytesPersec; DiskReadsPersec=[string]$_.DiskReadsPersec; DiskWritesPersec=[string]$_.DiskWritesPersec; CurrentDiskQueueLength=[string]$_.CurrentDiskQueueLength } }); $v=@(Get-CimInstance Win32_LogicalDisk -Filter 'DriveType=3' | ForEach-Object { @{ DeviceID=$_.DeviceID; Size=[string]$_.Size; FreeSpace=[string]$_.FreeSpace } }); @{disks=$d;volumes=$v} | ConvertTo-Json -Depth 4 -Compress";
@@ -61,7 +62,7 @@ class SystemMetrics {
this.processCpu = options.processCpu || process.cpuUsage; this.processCpu = options.processCpu || process.cpuUsage;
this.processMemory = options.processMemory || process.memoryUsage; this.processMemory = options.processMemory || process.memoryUsage;
this.shouldCollect = options.shouldCollect || (() => true); this.shouldCollect = options.shouldCollect || (() => true);
this.readDisks = options.readDisks || (process.platform === 'win32' ? readWindowsCounters : null); this.readDisks = options.readDisks || (process.platform === 'win32' ? readWindowsCountersOffThread : null);
this.limit = Math.max(1, Math.min(60, options.limit || 60)); this.limit = Math.max(1, Math.min(60, options.limit || 60));
this.samples = []; this.samples = [];
this.previous = null; this.previous = null;
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "multi-hoster-uploader", "name": "multi-hoster-uploader",
"version": "2.1.76", "version": "2.1.77",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "multi-hoster-uploader", "name": "multi-hoster-uploader",
"version": "2.1.76", "version": "2.1.77",
"dependencies": { "dependencies": {
"chokidar": "^4.0.3", "chokidar": "^4.0.3",
"undici": "7.29.1", "undici": "7.29.1",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "multi-hoster-uploader", "name": "multi-hoster-uploader",
"version": "2.1.76", "version": "2.1.77",
"description": "Upload files to doodstream, voe, vidmoly, byse simultaneously", "description": "Upload files to doodstream, voe, vidmoly, byse simultaneously",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {
+2
View File
@@ -53,6 +53,8 @@ const sourceFiles = [
'lib/performance-monitor.js', 'lib/performance-monitor.js',
'lib/runtime-profile.js', 'lib/runtime-profile.js',
'lib/system-metrics.js', 'lib/system-metrics.js',
'lib/system-metrics-reader.js',
'lib/system-metrics-worker.js',
'lib/upload-file-io.js', 'lib/upload-file-io.js',
'lib/online-backup.js', 'lib/online-backup.js',
'lib/online-backup-keyring.js', 'lib/online-backup-keyring.js',