-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reuse processes using a process pool
- Loading branch information
1 parent
96ca197
commit 60abaa5
Showing
13 changed files
with
222 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ insert_final_newline = true | |
indent_style = space | ||
indent_size = 2 | ||
|
||
[{*.js,test/index.sh}] | ||
[{*.js}] | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { ChildProcess, fork } from 'child_process'; | ||
import * as os from 'os'; | ||
import { resolve as pathResolve } from 'path'; | ||
import { removeDebugArgs } from './util'; | ||
|
||
interface IMochaProcess extends ChildProcess { | ||
destroy: () => void; | ||
} | ||
|
||
export default class ProcessPool { | ||
private maxParallel: number; | ||
private waitingList: Array<(process: IMochaProcess) => void> = []; | ||
private unusedProcesses: IMochaProcess[] = []; | ||
private processes: IMochaProcess[] = []; | ||
|
||
constructor() { | ||
this.maxParallel = os.cpus().length; | ||
} | ||
|
||
setMaxParallel(n) { | ||
this.maxParallel = n; | ||
} | ||
|
||
async getOrCreate(isTypescriptRunMode): Promise<IMochaProcess> { | ||
const extension = isTypescriptRunMode ? 'ts' : 'js'; | ||
const runnerPath = pathResolve(__dirname, `../subprocess/runner.${extension}`); | ||
|
||
const lastUnusedProcess = this.unusedProcesses.pop(); | ||
if (lastUnusedProcess) { | ||
return lastUnusedProcess; | ||
} | ||
|
||
if (this.processes.length >= this.maxParallel) { | ||
const process: IMochaProcess = await new Promise<IMochaProcess>((resolve) => { | ||
this.waitingList.push((proc: IMochaProcess) => { | ||
resolve(proc); | ||
}); | ||
}); | ||
return process; | ||
} | ||
return this.create(runnerPath, { | ||
// otherwise `--inspect-brk` and other params will be passed to subprocess | ||
execArgv: process.execArgv.filter(removeDebugArgs), | ||
stdio: ['ipc'], | ||
}); | ||
} | ||
|
||
create(runnerPath, opt) { | ||
const process = fork(runnerPath, [], opt) as IMochaProcess; | ||
|
||
this.processes.push(process); | ||
|
||
process.destroy = () => { | ||
const nextOnWaitingList = this.waitingList.pop(); | ||
if (nextOnWaitingList) { | ||
nextOnWaitingList(process); | ||
} else { | ||
this.unusedProcesses.push(process); | ||
} | ||
}; | ||
|
||
return process; | ||
} | ||
|
||
destroyAll() { | ||
this.processes.forEach((proc) => { | ||
proc.kill(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.