Skip to content

Commit 7a702ff

Browse files
committed
refactor sub-functions
1 parent 422fa1d commit 7a702ff

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

packages/core/src/shared/utilities/processUtils.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class ChildProcessTracker {
104104
ChildProcessTracker.logger.warn(`Missing process with id ${pid}`)
105105
return
106106
}
107-
const stats = await this.getUsage(pid)
107+
const stats = this.getUsage(pid)
108108
if (stats) {
109109
ChildProcessTracker.logger.debug(`Process ${pid} usage: %O`, stats)
110110
if (stats.memory > ChildProcessTracker.thresholds.memory) {
@@ -143,8 +143,16 @@ export class ChildProcessTracker {
143143
this.#processByPid.clear()
144144
}
145145

146-
public async getUsage(pid: number): Promise<ProcessStats> {
147-
const getWindowsUsage = () => {
146+
public getUsage(pid: number): ProcessStats {
147+
try {
148+
// isWin() leads to circular dependency.
149+
return process.platform === 'win32' ? getWindowsUsage() : getUnixUsage()
150+
} catch (e) {
151+
ChildProcessTracker.logger.warn(`Failed to get process stats for ${pid}: ${e}`)
152+
return { cpu: 0, memory: 0 }
153+
}
154+
155+
function getWindowsUsage() {
148156
const cpuOutput = proc
149157
.execFileSync('wmic', [
150158
'path',
@@ -167,7 +175,8 @@ export class ChildProcessTracker {
167175
memory: memoryBytes,
168176
}
169177
}
170-
const getUnixUsage = () => {
178+
179+
function getUnixUsage() {
171180
const cpuMemOutput = proc.execFileSync('ps', ['-p', pid.toString(), '-o', '%cpu,%mem']).toString()
172181
const rssOutput = proc.execFileSync('ps', ['-p', pid.toString(), '-o', 'rss']).toString()
173182

@@ -180,13 +189,6 @@ export class ChildProcessTracker {
180189
memory: memoryBytes,
181190
}
182191
}
183-
try {
184-
// isWin() leads to circular dependency.
185-
return process.platform === 'win32' ? getWindowsUsage() : getUnixUsage()
186-
} catch (e) {
187-
ChildProcessTracker.logger.warn(`Failed to get process stats for ${pid}: ${e}`)
188-
return { cpu: 0, memory: 0 }
189-
}
190192
}
191193
}
192194

packages/core/src/test/shared/utilities/processUtils.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,12 @@ describe('ChildProcessTracker', function () {
458458
memory: ChildProcessTracker.thresholds.memory + 1,
459459
}
460460

461-
usageMock.resolves(highCpu)
461+
usageMock.returns(highCpu)
462462

463463
await clock.tickAsync(ChildProcessTracker.pollingInterval)
464464
assertLogsContain('exceeded cpu threshold', false, 'warn')
465465

466-
usageMock.resolves(highMemory)
466+
usageMock.returns(highMemory)
467467
await clock.tickAsync(ChildProcessTracker.pollingInterval)
468468
assertLogsContain('exceeded memory threshold', false, 'warn')
469469

@@ -474,7 +474,7 @@ describe('ChildProcessTracker', function () {
474474
const runningProcess = startSleepProcess()
475475
tracker.add(runningProcess.childProcess)
476476

477-
usageMock.resolves({
477+
usageMock.returns({
478478
cpu: ChildProcessTracker.thresholds.cpu + 1,
479479
memory: 0,
480480
})
@@ -488,7 +488,7 @@ describe('ChildProcessTracker', function () {
488488
it('does not log for processes within threshold', async function () {
489489
const runningProcess = startSleepProcess()
490490

491-
usageMock.resolves({
491+
usageMock.returns({
492492
cpu: ChildProcessTracker.thresholds.cpu - 1,
493493
memory: ChildProcessTracker.thresholds.memory - 1,
494494
})

0 commit comments

Comments
 (0)