@@ -9,6 +9,7 @@ import * as logger from '../logger/logger'
99import { Timeout , CancellationError , waitUntil } from './timeoutUtils'
1010import { PollingSet } from './pollingSet'
1111import { CircularBuffer } from './collectionUtils'
12+ import { oncePerUniqueArg } from './functionUtils'
1213
1314export interface RunParameterContext {
1415 /** Reports an error parsed from the stdin/stdout streams. */
@@ -74,7 +75,6 @@ export class ChildProcessTracker {
7475 cpu : 50 ,
7576 }
7677 static readonly logger = logger . getLogger ( 'childProcess' )
77- static readonly loggedPids = new CircularBuffer < number > ( 1000 )
7878 #processByPid: Map < number , ChildProcess > = new Map < number , ChildProcess > ( )
7979 #pids: PollingSet < number >
8080
@@ -102,27 +102,32 @@ export class ChildProcessTracker {
102102
103103 private async checkProcessUsage ( pid : number ) : Promise < void > {
104104 if ( ! this . #pids. has ( pid ) ) {
105- ChildProcessTracker . logOnce ( pid , `Missing process with id ${ pid } ` )
105+ ChildProcessTracker . logMissingPid ( pid )
106106 return
107107 }
108108 const stats = this . getUsage ( pid )
109109 if ( stats ) {
110110 ChildProcessTracker . logger . debug ( `Process ${ pid } usage: %O` , stats )
111111 if ( stats . memory > ChildProcessTracker . thresholds . memory ) {
112- ChildProcessTracker . logOnce ( pid , `Process ${ pid } exceeded memory threshold: ${ stats . memory } ` )
112+ ChildProcessTracker . logExceededThreshold ( pid , ' memory' )
113113 }
114114 if ( stats . cpu > ChildProcessTracker . thresholds . cpu ) {
115- ChildProcessTracker . logOnce ( pid , `Process ${ pid } exceeded cpu threshold: ${ stats . cpu } ` )
115+ ChildProcessTracker . logExceededThreshold ( pid , ' cpu' )
116116 }
117117 }
118118 }
119119
120- public static logOnce ( pid : number , msg : string ) {
121- if ( ! ChildProcessTracker . loggedPids . contains ( pid ) ) {
122- ChildProcessTracker . loggedPids . add ( pid )
123- ChildProcessTracker . logger . warn ( msg )
124- }
125- }
120+ private static logMissingPid = oncePerUniqueArg ( ( pid : number ) =>
121+ ChildProcessTracker . logger . warn ( `Missing process with id ${ pid } ` )
122+ )
123+
124+ private static logStatFailure = oncePerUniqueArg ( ( pid : number , e : unknown ) =>
125+ ChildProcessTracker . logger . warn ( `Failed to get process stats for ${ pid } : ${ e } ` )
126+ )
127+
128+ private static logExceededThreshold = oncePerUniqueArg ( ( pid : number , threshold : keyof ProcessStats ) =>
129+ ChildProcessTracker . logger . warn ( `Process ${ pid } exceeded ${ threshold } threshold` )
130+ )
126131
127132 public add ( childProcess : ChildProcess ) {
128133 const pid = childProcess . pid ( )
@@ -156,7 +161,7 @@ export class ChildProcessTracker {
156161 // isWin() leads to circular dependency.
157162 return process . platform === 'win32' ? getWindowsUsage ( ) : getUnixUsage ( )
158163 } catch ( e ) {
159- ChildProcessTracker . logOnce ( pid , `Failed to get process stats for ${ pid } : ${ e } ` )
164+ ChildProcessTracker . logStatFailure ( pid , e )
160165 return { cpu : 0 , memory : 0 }
161166 }
162167
0 commit comments