I'm trying to modernize some code for work, and noticed a lot of .list & .kill commands to find & terminate errant processes. It appears in the latest 1.x beta, the list function doesn't correctly pass PIDs in a way the kill function understands. I've provided some obfuscated code for your review. Thanks!
const wincmd = require('node-windows')
const userprogramProc = ["userprogram.exe", "userprogram2.exe"]
// returns the pids of exes that match userprogramProc
async function checkuserprogramProcesses(svc){
let svcPromise
if (svc) {
svcPromise = Promise.resolve(svc)
} else {
svcPromise = new Promise((resolve) => wincmd.list((svc) => resolve(svc)))
}
let pids = await svcPromise.then(svc => {
const processes = _.filter(svc, (p) => userprogramProc.includes(p.ImageName))
return _.map(processes, 'PID')
})
return pids
}
async function closeuserprogramProcesses (){
await closeProcessIfRunning("otherprogram.exe", false)
let pids = await checkuserprogramProcesses()
return Promise.map(pids, (pid) => new Promise((resolve) => wincmd.kill(pid, true, () => {
logger.info('closed userprogram process')
return resolve()
})), {concurrency: 2})
}
I'm trying to modernize some code for work, and noticed a lot of .list & .kill commands to find & terminate errant processes. It appears in the latest 1.x beta, the list function doesn't correctly pass PIDs in a way the kill function understands. I've provided some obfuscated code for your review. Thanks!