-
-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Process killer feature #838
Changes from all commits
734f46c
ea17713
41c57ad
2b6fb18
4f7591b
144585b
4d265a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -111,6 +111,8 @@ By default all drivers are loaded, you only control and change the versions or a | |||||
|
||||||
`opts.requestOpts` can be any valid [`got` options object](https://www.npmjs.com/package/got#proxies). You can use this for example to set a timeout. | ||||||
|
||||||
`opts.processKiller` set for that truthy value, for killing selenium server port. | ||||||
|
||||||
returns `Promise<ChildProcess>` | ||||||
|
||||||
## Error: Port 4444 is already in use. | ||||||
|
@@ -121,6 +123,8 @@ If you're getting this error, it means that you didn't shut down the server succ | |||||
pkill -f selenium-standalone | ||||||
``` | ||||||
|
||||||
or use truthy `opts.processKiller` in config | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## Set `selenium-standalone` Version as NodeJS environment parameter | ||||||
|
||||||
You can set any version by `process.env.SELENIUM_VERSION=3.141.59` before starting selenium-standalone. Default values are here: [lib/default-config.js](../lib/default-config.js) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,9 @@ selenium-standalone start --singleDriverStart=chrome | |
selenium-standalone install --config=/path/to/config.json | ||
selenium-standalone start --config=./config/seleniumConfig.js | ||
|
||
# killing process before starting | ||
selenium-standalone start --processKiller=true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A user would ask why they would need to set this given it feels like the expected behavior is that the selenium server or driver shuts down completely when the process is triggered to get shutdown. |
||
|
||
``` | ||
|
||
Config file can be a JSON file or a [module file](https://nodejs.org/api/modules.html#modules_file_modules) that exports options as an object: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
const fkill = require('fkill'); | ||
const findProcess = require('find-process'); | ||
const { command } = require('execa'); | ||
|
||
function getConfigProcessesName(drivers) { | ||
const driversName = Object.keys(drivers); | ||
const processesName = []; | ||
|
||
if (driversName && driversName.length) { | ||
for (const driverName of driversName) { | ||
if (driverName === 'chrome') { | ||
processesName.push('chromedriver'); | ||
} else if (driverName === 'firefox') { | ||
processesName.push('geckodriver'); | ||
} else if (driverName === 'chromiumedge') { | ||
processesName.push('msedgedriver'); | ||
} else if (driverName === 'ie') { | ||
processesName.push('IEDriverServer'); | ||
} else if (driverName === 'safari') { | ||
processesName.push('safaridriver'); | ||
} | ||
} | ||
} | ||
return processesName; | ||
} | ||
|
||
async function processKiller(drivers, portValue) { | ||
if (portValue) { | ||
if (!Number.isNaN(Number(`${portValue}`.startsWith(':') ? `${portValue}`.substring(1) : `${portValue}`))) { | ||
const portCast = `${portValue}`.startsWith(':') ? portValue : `:${portValue}`; | ||
|
||
await killProcessByFkill([portCast]); | ||
await killProcessByCmd([`${portValue}`.startsWith(':') ? `${portValue}`.substring(1) : portValue], 'port'); | ||
} | ||
} | ||
if (drivers && typeof drivers === 'object' && Object.keys(drivers).length) { | ||
await killProcess(getConfigProcessesName(drivers), 'name'); | ||
} | ||
} | ||
|
||
async function killProcess(processesNameArr, type) { | ||
await killProcessByFkill(processesNameArr); | ||
await killProcessByCmd(processesNameArr, type); | ||
} | ||
|
||
async function killProcessByCmd(processes, type) { | ||
if (processes && processes.length) { | ||
for (const processSingle of processes) { | ||
const results = await findProcess(type, processSingle, true); | ||
|
||
if (results && results.length) { | ||
for (const result of results) { | ||
try { | ||
if (process.platform === 'win32' && !result.name.includes('node')) { | ||
await command(`taskkill /F /IM ${result.name} /T`); | ||
|
||
console.log(`Killed process: "${processSingle}" system name is "${result.name}"`); | ||
} else if (!process.name.includes('node')) { | ||
await command(`pkill -f ${result.name}`); | ||
|
||
console.log(`Killed process: "${processSingle}" system name is "${result.name}"`); | ||
} | ||
} catch (_) { | ||
// eslint-disable-next-line no-empty | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
async function killProcessByFkill(processes) { | ||
for (const process of processes) { | ||
try { | ||
await fkill([process], { force: true, tree: true, ignoreCase: true }); | ||
|
||
console.log(`Killed process: "${process}"`); | ||
} catch (_) { | ||
// eslint-disable-next-line no-empty | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
processKiller, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.