123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import {exec, spawn} from 'child_process';
- class ProcessManager {
- constructor() {
- this.processes = new Map();
- }
-
- startProcess(key, command, callback) {
-
- if (this.processes.has(key)) {
- this.killProcess(key);
- }
-
- const child = exec(command, (error, stdout, stderr) => {
- if (callback) callback(error, stdout, stderr);
-
- this.processes.delete(key);
- });
-
- this.processes.set(key, child);
- }
-
- killProcess(key) {
- const child = this.processes.get(key);
- if (child) {
- child.kill();
- this.processes.delete(key);
- }
- }
-
- killAll() {
- for (const [key, child] of this.processes.entries()) {
- child.kill();
- this.processes.delete(key);
- }
- }
- }
- export default ProcessManager;
|