Skip to content

Commit e371c30

Browse files
committed
backend: ensure only one ProcessStats instance in total
1 parent b2f0a63 commit e371c30

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/packages/backend/process-stats.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
import { getLogger } from "./logger";
2020
import { envToInt } from "./misc/env-to-number";
2121

22+
const dbg = getLogger("process-stats").debug;
23+
2224
const exec = promisify(cp_exec);
2325

2426
/**
@@ -31,35 +33,32 @@ const exec = promisify(cp_exec);
3133
// be on the safe side to avoid processing too much data.
3234
const LIMIT = envToInt("COCALC_PROJECT_INFO_PROC_LIMIT", 1024);
3335

34-
interface ProcessStatsOpts {
35-
procLimit?: number;
36-
testing?: boolean;
37-
dbg?: Function;
38-
}
39-
4036
export class ProcessStats {
4137
private static instance: ProcessStats;
4238

43-
private readonly testing: boolean;
4439
private readonly procLimit: number;
45-
private readonly dbg: Function;
40+
41+
private testing: boolean;
4642
private ticks: number;
4743
private pagesize: number;
4844
private last?: { timestamp: number; processes: Processes };
4945

50-
private constructor(opts?: ProcessStatsOpts) {
51-
this.procLimit = opts?.procLimit ?? LIMIT;
52-
this.dbg = opts?.dbg ?? getLogger("process-stats").debug;
46+
private constructor() {
47+
this.procLimit = LIMIT;
5348
this.init();
5449
}
5550

56-
public static getInstance(opts?: ProcessStatsOpts): ProcessStats {
51+
public static getInstance(): ProcessStats {
5752
if (!ProcessStats.instance) {
58-
ProcessStats.instance = new ProcessStats(opts);
53+
ProcessStats.instance = new ProcessStats();
5954
}
6055
return ProcessStats.instance;
6156
}
6257

58+
public setTesting(testing: boolean): void {
59+
this.testing = testing;
60+
}
61+
6362
// this grabs some kernel configuration values we need. they won't change
6463
public init = reuseInFlight(async () => {
6564
if (this.ticks == null) {
@@ -175,11 +174,11 @@ export class ProcessStats {
175174
procs[proc.pid] = proc;
176175
} catch (err) {
177176
if (this.testing)
178-
this.dbg(`process ${pid} likely vanished – could happen – ${err}`);
177+
dbg(`process ${pid} likely vanished – could happen – ${err}`);
179178
}
180179
// we avoid processing and sending too much data
181180
if (n > this.procLimit) {
182-
this.dbg(`too many processes – limit of ${this.procLimit} reached!`);
181+
dbg(`too many processes – limit of ${this.procLimit} reached!`);
183182
break;
184183
} else {
185184
n += 1;

src/packages/project/project-info/server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,10 @@ export class ProjectInfoServer extends EventEmitter {
567567
// Initialize tmpfs detection once at startup
568568
this.tmpIsMemoryBased = await isTmpMemoryBased();
569569
this.running = true;
570-
this.processStats = ProcessStats.getInstance({
571-
testing: this.testing,
572-
dbg: this.dbg,
573-
});
570+
this.processStats = ProcessStats.getInstance();
571+
if (this.testing) {
572+
this.processStats.setTesting(true);
573+
}
574574
await this.processStats.init();
575575
while (true) {
576576
//this.dbg(`listeners on 'info': ${this.listenerCount("info")}`);

0 commit comments

Comments
 (0)