Skip to content

Commit fff13a7

Browse files
leosvelpereznartc
authored andcommitted
feat(core): add method to obtain system info and remove debugging lifecycle
1 parent ba006eb commit fff13a7

File tree

7 files changed

+52
-48
lines changed

7 files changed

+52
-48
lines changed

packages/nx/src/native/index.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ export declare class ProcessMetricsCollector {
110110
* Returns true if collection was stopped, false if not running
111111
*/
112112
stopCollection(): boolean
113+
/**
114+
* Get system information (CPU cores and total memory)
115+
* This is separate from the collection interval and meant to be called imperatively
116+
*/
117+
getSystemInfo(): SystemInfo
113118
/** Register the main CLI process for metrics collection */
114119
registerMainCliProcess(pid: number): void
115120
/** Register the daemon process for metrics collection */
@@ -415,6 +420,12 @@ export declare const enum SupportedEditor {
415420
Unknown = 5
416421
}
417422

423+
/** System information (static system-level data) */
424+
export interface SystemInfo {
425+
cpuCores: number
426+
totalMemory: number
427+
}
428+
418429
export interface Target {
419430
executor?: string
420431
inputs?: Array<JsInputs>

packages/nx/src/native/metrics/collector.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use sysinfo::{Pid, ProcessRefreshKind, System, UpdateKind};
1111
use crate::native::metrics::types::{
1212
BatchMetricsSnapshot, BatchRegistration, CollectorConfig, DaemonMetrics,
1313
IndividualTaskRegistration, MetricsError, MetricsUpdate, ProcessMetadata, ProcessMetrics,
14-
ProcessMetricsSnapshot,
14+
ProcessMetricsSnapshot, SystemInfo,
1515
};
1616
use napi::threadsafe_function::{
1717
ErrorStrategy::CalleeHandled, ThreadsafeFunction, ThreadsafeFunctionCallMode,
@@ -200,6 +200,15 @@ impl MetricsCollector {
200200
self.is_collecting.load(Ordering::Acquire)
201201
}
202202

203+
/// Get system information (CPU cores and total memory)
204+
pub fn get_system_info(&self) -> SystemInfo {
205+
let sys = self.system.lock();
206+
SystemInfo {
207+
cpu_cores: sys.cpus().len() as u32,
208+
total_memory: sys.total_memory() as i64,
209+
}
210+
}
211+
203212
/// Register the main CLI process
204213
/// Idempotent - safe to call multiple times with same PID
205214
/// No validation at registration time - validation happens during collection

packages/nx/src/native/metrics/napi_bindings.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use parking_lot::RwLock;
99
use tracing::error;
1010

1111
use crate::native::metrics::collector::MetricsCollector;
12-
use crate::native::metrics::types::MetricsUpdate;
12+
use crate::native::metrics::types::{MetricsUpdate, SystemInfo};
1313

1414
/// NAPI wrapper for the process metrics collector
1515
/// Provides a JavaScript-friendly interface for metrics collection
@@ -57,6 +57,14 @@ impl ProcessMetricsCollector {
5757
}
5858
}
5959

60+
/// Get system information (CPU cores and total memory)
61+
/// This is separate from the collection interval and meant to be called imperatively
62+
#[napi]
63+
pub fn get_system_info(&self) -> SystemInfo {
64+
let collector = self.collector.read();
65+
collector.get_system_info()
66+
}
67+
6068
/// Register the main CLI process for metrics collection
6169
#[napi]
6270
pub fn register_main_cli_process(&self, pid: i32) {

packages/nx/src/native/metrics/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,11 @@ pub struct MetricsUpdate {
168168
pub metrics: Arc<ProcessMetricsSnapshot>,
169169
pub metadata: Option<Arc<HashMap<String, ProcessMetadata>>>,
170170
}
171+
172+
/// System information (static system-level data)
173+
#[napi(object)]
174+
#[derive(Debug, Clone)]
175+
pub struct SystemInfo {
176+
pub cpu_cores: u32,
177+
pub total_memory: i64,
178+
}

packages/nx/src/tasks-runner/life-cycles/process-metrics-life-cycle.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

packages/nx/src/tasks-runner/process-metrics-service.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ProcessMetricsSnapshot,
77
BatchMetricsSnapshot,
88
MetricsUpdate,
9+
SystemInfo,
910
} from '../native';
1011
import { getDaemonProcessIdSync } from '../daemon/cache';
1112

@@ -16,6 +17,7 @@ export type {
1617
ProcessMetricsSnapshot,
1718
BatchMetricsSnapshot,
1819
MetricsUpdate,
20+
SystemInfo,
1921
};
2022

2123
export type MetricsCallback = (event: MetricsUpdate) => void;
@@ -134,6 +136,18 @@ class ProcessMetricsService {
134136
}
135137
}
136138

139+
/**
140+
* Get system information (CPU cores and total memory)
141+
*/
142+
getSystemInfo(): SystemInfo | null {
143+
try {
144+
return this.collector?.getSystemInfo();
145+
} catch {
146+
// Silent failure - metrics collection is optional
147+
return null;
148+
}
149+
}
150+
137151
/**
138152
* Stop collection and cleanup
139153
*/

packages/nx/src/tasks-runner/run-command.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ import { TaskProfilingLifeCycle } from './life-cycles/task-profiling-life-cycle'
5757
import { TaskResultsLifeCycle } from './life-cycles/task-results-life-cycle';
5858
import { TaskTimingsLifeCycle } from './life-cycles/task-timings-life-cycle';
5959
import { getTuiTerminalSummaryLifeCycle } from './life-cycles/tui-summary-life-cycle';
60-
import { ProcessMetricsLifeCycle } from './life-cycles/process-metrics-life-cycle';
6160
import {
6261
assertTaskGraphDoesNotContainInvalidTargets,
6362
findCycle,
@@ -1056,10 +1055,6 @@ export async function invokeTasksRunner({
10561055
export function constructLifeCycles(lifeCycle: LifeCycle): LifeCycle[] {
10571056
const lifeCycles = [] as LifeCycle[];
10581057
lifeCycles.push(new StoreRunInformationLifeCycle());
1059-
if (process.env.NX_DEBUG_PROCESS_METRICS === 'true') {
1060-
// Temporary throwaway life cycle only used for debugging purposes
1061-
lifeCycles.push(new ProcessMetricsLifeCycle());
1062-
}
10631058
lifeCycles.push(lifeCycle);
10641059
if (process.env.NX_PERF_LOGGING === 'true') {
10651060
lifeCycles.push(new TaskTimingsLifeCycle());

0 commit comments

Comments
 (0)