Feat: Add System Control (Standby Mode) and Resource Monitor

This commit is contained in:
cawcenter
2025-12-14 19:50:05 -05:00
parent f7997cdd88
commit 88d3157cd9
8 changed files with 274 additions and 1 deletions

View File

@@ -1,3 +1,5 @@
import { system } from '@/lib/system/SystemController';
interface BatchConfig {
batchSize: number; // How many items to grab at once (e.g. 100)
concurrency: number; // How many to process in parallel (e.g. 5)
@@ -17,6 +19,18 @@ export class BatchProcessor {
const chunk = items.slice(i, i + this.config.batchSize);
console.log(`Processing Batch ${(i / this.config.batchSize) + 1}...`);
// Check System State (Standby Mode)
if (!system.isActive()) {
console.log('[God Mode] System in STANDBY. Pausing Batch Processor...');
// Wait until active again (check every 2s)
while (!system.isActive()) {
await new Promise(r => setTimeout(r, 2000));
}
console.log('[God Mode] System RESUMED.');
}
// Within each chunk, limit concurrency
// Within each chunk, limit concurrency
const chunkResults = await this.runWithConcurrency(chunk, workerFunction);
results.push(...chunkResults);

View File

@@ -0,0 +1,68 @@
import pidusage from 'pidusage';
export type SystemState = 'active' | 'standby';
export interface SystemMetrics {
cpu: number;
memory: number; // in bytes
memoryMB: number;
uptime: number; // seconds
state: SystemState;
timestamp: number;
}
class SystemController {
private state: SystemState = 'active'; // Default to active
private lastMetrics: SystemMetrics | null = null;
// Toggle System State
toggle(): SystemState {
this.state = this.state === 'active' ? 'standby' : 'active';
console.log(`[God Mode] System State Toggled: ${this.state.toUpperCase()}`);
return this.state;
}
// Set conform state
setState(newState: SystemState) {
this.state = newState;
}
getState(): SystemState {
return this.state;
}
isActive(): boolean {
return this.state === 'active';
}
// Get Live Resource Usage
async getMetrics(): Promise<SystemMetrics> {
try {
const stats = await pidusage(process.pid);
this.lastMetrics = {
cpu: parseFloat(stats.cpu.toFixed(1)),
memory: stats.memory,
memoryMB: Math.round(stats.memory / 1024 / 1024),
uptime: stats.elapsed,
state: this.state,
timestamp: Date.now()
};
return this.lastMetrics;
} catch (e) {
console.error("Failed to get pidusage", e);
// Return cached or empty if fail
return this.lastMetrics || {
cpu: 0,
memory: 0,
memoryMB: 0,
uptime: 0,
state: this.state,
timestamp: Date.now()
};
}
}
}
export const system = new SystemController();