@@ -19,6 +19,8 @@ import {
19
19
import { getLogger } from "./logger" ;
20
20
import { envToInt } from "./misc/env-to-number" ;
21
21
22
+ const dbg = getLogger ( "process-stats" ) . debug ;
23
+
22
24
const exec = promisify ( cp_exec ) ;
23
25
24
26
/**
@@ -31,35 +33,32 @@ const exec = promisify(cp_exec);
31
33
// be on the safe side to avoid processing too much data.
32
34
const LIMIT = envToInt ( "COCALC_PROJECT_INFO_PROC_LIMIT" , 1024 ) ;
33
35
34
- interface ProcessStatsOpts {
35
- procLimit ?: number ;
36
- testing ?: boolean ;
37
- dbg ?: Function ;
38
- }
39
-
40
36
export class ProcessStats {
41
37
private static instance : ProcessStats ;
42
38
43
- private readonly testing : boolean ;
44
39
private readonly procLimit : number ;
45
- private readonly dbg : Function ;
40
+
41
+ private testing : boolean ;
46
42
private ticks : number ;
47
43
private pagesize : number ;
48
44
private last ?: { timestamp : number ; processes : Processes } ;
49
45
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 ;
53
48
this . init ( ) ;
54
49
}
55
50
56
- public static getInstance ( opts ?: ProcessStatsOpts ) : ProcessStats {
51
+ public static getInstance ( ) : ProcessStats {
57
52
if ( ! ProcessStats . instance ) {
58
- ProcessStats . instance = new ProcessStats ( opts ) ;
53
+ ProcessStats . instance = new ProcessStats ( ) ;
59
54
}
60
55
return ProcessStats . instance ;
61
56
}
62
57
58
+ public setTesting ( testing : boolean ) : void {
59
+ this . testing = testing ;
60
+ }
61
+
63
62
// this grabs some kernel configuration values we need. they won't change
64
63
public init = reuseInFlight ( async ( ) => {
65
64
if ( this . ticks == null ) {
@@ -175,11 +174,11 @@ export class ProcessStats {
175
174
procs [ proc . pid ] = proc ;
176
175
} catch ( err ) {
177
176
if ( this . testing )
178
- this . dbg ( `process ${ pid } likely vanished – could happen – ${ err } ` ) ;
177
+ dbg ( `process ${ pid } likely vanished – could happen – ${ err } ` ) ;
179
178
}
180
179
// we avoid processing and sending too much data
181
180
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!` ) ;
183
182
break ;
184
183
} else {
185
184
n += 1 ;
0 commit comments