Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
761f9ca1b6 | |||
ad2c180cfe | |||
36eb1a79a7 | |||
8a0a9dedb1 | |||
00c4488cc3 | |||
2ffaeff4b5 | |||
594e006a5a |
28
changelog.md
28
changelog.md
@ -1,5 +1,33 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2025-03-01 - 1.2.0 - feat(core)
|
||||||
|
Introduce ProcessMonitor with memory management and spawning features
|
||||||
|
|
||||||
|
- Added ProcessMonitor class with functionality to manage process execution and memory usage.
|
||||||
|
- Implemented process spawning with ability to handle command arguments and directories.
|
||||||
|
- Added periodic memory monitoring and automatic restarts when memory thresholds are exceeded.
|
||||||
|
- ProcessMonitor now logs its actions with optional configuration name for better identification.
|
||||||
|
- Updated test file to include example usage of ProcessMonitor.
|
||||||
|
|
||||||
|
## 2025-03-01 - 1.1.1 - fix(package)
|
||||||
|
Update dependencies and pnpm configuration
|
||||||
|
|
||||||
|
- Updated @types/node to 22.13.8
|
||||||
|
- Updated pnpm configuration to include onlyBuiltDependencies with esbuild, mongodb-memory-server, and puppeteer
|
||||||
|
|
||||||
|
## 2025-03-01 - 1.1.0 - feat(core)
|
||||||
|
Introduce ProcessMonitor class and integrate native and external plugins
|
||||||
|
|
||||||
|
- Added a new ProcessMonitor class to manage and monitor child processes with memory constraints.
|
||||||
|
- Integrated native 'path' and external '@push.rocks/smartpath' packages in a unified plugins file.
|
||||||
|
- Adjusted index and related files for improved modular structure.
|
||||||
|
|
||||||
|
## 2025-02-24 - 1.0.3 - fix(core)
|
||||||
|
Corrected description in package.json and readme.md from 'task manager' to 'process manager'.
|
||||||
|
|
||||||
|
- Updated the project description in package.json.
|
||||||
|
- Aligned the description in readme.md with package.json.
|
||||||
|
|
||||||
## 2025-02-24 - 1.0.2 - fix(core)
|
## 2025-02-24 - 1.0.2 - fix(core)
|
||||||
Internal changes with no functional impact.
|
Internal changes with no functional impact.
|
||||||
|
|
||||||
|
20
package.json
20
package.json
@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "@git.zone/tspm",
|
"name": "@git.zone/tspm",
|
||||||
"version": "1.0.2",
|
"version": "1.2.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "a no fuzz task manager",
|
"description": "a no fuzz process manager",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
@ -19,9 +19,14 @@
|
|||||||
"@git.zone/tsrun": "^1.2.46",
|
"@git.zone/tsrun": "^1.2.46",
|
||||||
"@git.zone/tstest": "^1.0.44",
|
"@git.zone/tstest": "^1.0.44",
|
||||||
"@push.rocks/tapbundle": "^5.0.15",
|
"@push.rocks/tapbundle": "^5.0.15",
|
||||||
"@types/node": "^20.8.7"
|
"@types/node": "^22.13.8"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@push.rocks/smartcli": "^4.0.11",
|
||||||
|
"@push.rocks/smartpath": "^5.0.18",
|
||||||
|
"pidusage": "^4.0.0",
|
||||||
|
"ps-tree": "^1.2.0"
|
||||||
},
|
},
|
||||||
"dependencies": {},
|
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://code.foss.global/git.zone/tspm.git"
|
"url": "https://code.foss.global/git.zone/tspm.git"
|
||||||
@ -43,6 +48,11 @@
|
|||||||
"readme.md"
|
"readme.md"
|
||||||
],
|
],
|
||||||
"pnpm": {
|
"pnpm": {
|
||||||
"overrides": {}
|
"overrides": {},
|
||||||
|
"onlyBuiltDependencies": [
|
||||||
|
"esbuild",
|
||||||
|
"mongodb-memory-server",
|
||||||
|
"puppeteer"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1260
pnpm-lock.yaml
generated
1260
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
# @git.zone/tspm
|
# @git.zone/tspm
|
||||||
|
|
||||||
a no fuzz task manager
|
a no fuzz process manager
|
||||||
|
|
||||||
## How to create the docs
|
## How to create the docs
|
||||||
|
|
||||||
|
19
test/test.ts
19
test/test.ts
@ -6,3 +6,22 @@ tap.test('first test', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
|
||||||
|
// Example usage:
|
||||||
|
const config: IMonitorConfig = {
|
||||||
|
name: 'Project XYZ Monitor', // Identifier for the instance
|
||||||
|
projectDir: '/path/to/your/project', // Set the project directory here
|
||||||
|
command: 'npm run xyz', // Full command string (no need for args)
|
||||||
|
memoryLimitBytes: 500 * 1024 * 1024, // 500 MB memory limit
|
||||||
|
monitorIntervalMs: 5000, // Check memory usage every 5 seconds
|
||||||
|
};
|
||||||
|
|
||||||
|
const monitor = new ProcessMonitor(config);
|
||||||
|
monitor.start();
|
||||||
|
|
||||||
|
// Ensure that on process exit (e.g. Ctrl+C) we clean up the child process and prevent respawns.
|
||||||
|
process.on('SIGINT', () => {
|
||||||
|
monitor.log('Received SIGINT, stopping monitor...');
|
||||||
|
monitor.stop();
|
||||||
|
process.exit();
|
||||||
|
});
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@git.zone/tspm',
|
name: '@git.zone/tspm',
|
||||||
version: '1.0.2',
|
version: '1.2.0',
|
||||||
description: 'a no fuzz task manager'
|
description: 'a no fuzz process manager'
|
||||||
}
|
}
|
||||||
|
157
ts/classes.processmonitor.ts
Normal file
157
ts/classes.processmonitor.ts
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
import * as plugins from './plugins.js';
|
||||||
|
|
||||||
|
export interface IMonitorConfig {
|
||||||
|
name?: string; // Optional name to identify the instance
|
||||||
|
projectDir: string; // Directory where the command will run
|
||||||
|
command: string; // Full command to run (e.g., "npm run xyz")
|
||||||
|
args?: string[]; // Optional: arguments for the command
|
||||||
|
memoryLimitBytes: number; // Maximum allowed memory (in bytes) for the process group
|
||||||
|
monitorIntervalMs?: number; // Interval (in ms) at which memory is checked (default: 5000)
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ProcessMonitor {
|
||||||
|
private child: plugins.childProcess.ChildProcess | null = null;
|
||||||
|
private config: IMonitorConfig;
|
||||||
|
private intervalId: NodeJS.Timeout | null = null;
|
||||||
|
private stopped: boolean = true; // Initially stopped until start() is called
|
||||||
|
|
||||||
|
constructor(config: IMonitorConfig) {
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public start(): void {
|
||||||
|
// Reset the stopped flag so that new processes can spawn.
|
||||||
|
this.stopped = false;
|
||||||
|
this.log(`Starting process monitor.`);
|
||||||
|
this.spawnChild();
|
||||||
|
|
||||||
|
// Set the monitoring interval.
|
||||||
|
const interval = this.config.monitorIntervalMs || 5000;
|
||||||
|
this.intervalId = setInterval(() => {
|
||||||
|
if (this.child && this.child.pid) {
|
||||||
|
this.monitorProcessGroup(this.child.pid, this.config.memoryLimitBytes);
|
||||||
|
}
|
||||||
|
}, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
private spawnChild(): void {
|
||||||
|
// Don't spawn if the monitor has been stopped.
|
||||||
|
if (this.stopped) return;
|
||||||
|
|
||||||
|
if (this.config.args && this.config.args.length > 0) {
|
||||||
|
this.log(
|
||||||
|
`Spawning command "${this.config.command}" with args [${this.config.args.join(
|
||||||
|
', '
|
||||||
|
)}] in directory: ${this.config.projectDir}`
|
||||||
|
);
|
||||||
|
this.child = plugins.childProcess.spawn(this.config.command, this.config.args, {
|
||||||
|
cwd: this.config.projectDir,
|
||||||
|
detached: true,
|
||||||
|
stdio: 'inherit',
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.log(
|
||||||
|
`Spawning command "${this.config.command}" in directory: ${this.config.projectDir}`
|
||||||
|
);
|
||||||
|
// Use shell mode to allow a full command string.
|
||||||
|
this.child = plugins.childProcess.spawn(this.config.command, {
|
||||||
|
cwd: this.config.projectDir,
|
||||||
|
detached: true,
|
||||||
|
stdio: 'inherit',
|
||||||
|
shell: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
this.log(`Spawned process with PID ${this.child.pid}`);
|
||||||
|
|
||||||
|
// When the child process exits, restart it if the monitor isn't stopped.
|
||||||
|
this.child.on('exit', (code, signal) => {
|
||||||
|
this.log(`Child process exited with code ${code}, signal ${signal}.`);
|
||||||
|
if (!this.stopped) {
|
||||||
|
this.log('Restarting process...');
|
||||||
|
this.spawnChild();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Monitor the process group’s memory usage. If the total memory exceeds the limit,
|
||||||
|
* kill the process group so that the 'exit' handler can restart it.
|
||||||
|
*/
|
||||||
|
private async monitorProcessGroup(pid: number, memoryLimit: number): Promise<void> {
|
||||||
|
try {
|
||||||
|
const memoryUsage = await this.getProcessGroupMemory(pid);
|
||||||
|
this.log(
|
||||||
|
`Current memory usage for process group (PID ${pid}): ${this.humanReadableBytes(
|
||||||
|
memoryUsage
|
||||||
|
)} (${memoryUsage} bytes)`
|
||||||
|
);
|
||||||
|
if (memoryUsage > memoryLimit) {
|
||||||
|
this.log(
|
||||||
|
`Memory usage ${this.humanReadableBytes(
|
||||||
|
memoryUsage
|
||||||
|
)} exceeds limit of ${this.humanReadableBytes(memoryLimit)}. Restarting process.`
|
||||||
|
);
|
||||||
|
// Kill the entire process group by sending a signal to -PID.
|
||||||
|
process.kill(-pid, 'SIGKILL');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.log('Error monitoring process group: ' + error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the total memory usage (in bytes) for the process group (the main process and its children).
|
||||||
|
*/
|
||||||
|
private getProcessGroupMemory(pid: number): Promise<number> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
plugins.psTree(pid, (err, children) => {
|
||||||
|
if (err) return reject(err);
|
||||||
|
// Include the main process and its children.
|
||||||
|
const pids: number[] = [pid, ...children.map(child => Number(child.PID))];
|
||||||
|
plugins.pidusage(pids, (err, stats) => {
|
||||||
|
if (err) return reject(err);
|
||||||
|
let totalMemory = 0;
|
||||||
|
for (const key in stats) {
|
||||||
|
totalMemory += stats[key].memory;
|
||||||
|
}
|
||||||
|
resolve(totalMemory);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a number of bytes into a human-readable string (e.g. "1.23 MB").
|
||||||
|
*/
|
||||||
|
private humanReadableBytes(bytes: number, decimals: number = 2): string {
|
||||||
|
if (bytes === 0) return '0 Bytes';
|
||||||
|
const k = 1024;
|
||||||
|
const dm = decimals < 0 ? 0 : decimals;
|
||||||
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop the monitor and prevent any further respawns.
|
||||||
|
*/
|
||||||
|
public stop(): void {
|
||||||
|
this.log('Stopping process monitor.');
|
||||||
|
this.stopped = true;
|
||||||
|
if (this.intervalId) {
|
||||||
|
clearInterval(this.intervalId);
|
||||||
|
}
|
||||||
|
if (this.child && this.child.pid) {
|
||||||
|
process.kill(-this.child.pid, 'SIGKILL');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method for logging messages with the instance name.
|
||||||
|
*/
|
||||||
|
private log(message: string): void {
|
||||||
|
const prefix = this.config.name ? `[${this.config.name}] ` : '';
|
||||||
|
console.log(prefix + message);
|
||||||
|
}
|
||||||
|
}
|
6
ts/classes.tspm.ts
Normal file
6
ts/classes.tspm.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import * as plugins from './plugins.js';
|
||||||
|
import * as paths from './paths.js';
|
||||||
|
|
||||||
|
export class Tspm {
|
||||||
|
|
||||||
|
}
|
@ -1,3 +1,2 @@
|
|||||||
import * as plugins from './tspm.plugins.js';
|
export * from './classes.tspm.js';
|
||||||
|
export * from './classes.processmonitor.js';
|
||||||
export let demoExport = 'Hi there! :) This is an exported string';
|
|
||||||
|
4
ts/paths.ts
Normal file
4
ts/paths.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import * as plugins from './plugins.js';
|
||||||
|
|
||||||
|
export const packageDir = plugins.path.join(plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '..');
|
||||||
|
export const cwd = process.cwd();
|
26
ts/plugins.ts
Normal file
26
ts/plugins.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// native scope
|
||||||
|
import * as childProcess from 'child_process';
|
||||||
|
import * as path from 'node:path';
|
||||||
|
|
||||||
|
export {
|
||||||
|
childProcess,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
|
||||||
|
// @push.rocks scope
|
||||||
|
import * as smartpath from '@push.rocks/smartpath';
|
||||||
|
import * as smartcli from '@push.rocks/smartcli';
|
||||||
|
|
||||||
|
export {
|
||||||
|
smartpath,
|
||||||
|
smartcli,
|
||||||
|
}
|
||||||
|
|
||||||
|
// third-party scope
|
||||||
|
import psTree from 'ps-tree';
|
||||||
|
import pidusage from 'pidusage';
|
||||||
|
|
||||||
|
export {
|
||||||
|
psTree,
|
||||||
|
pidusage,
|
||||||
|
}
|
@ -1,2 +0,0 @@
|
|||||||
const removeme = {};
|
|
||||||
export { removeme };
|
|
Reference in New Issue
Block a user