Skip to content

Commit

Permalink
feat: return pid as kill response
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Mar 26, 2024
1 parent 3ae0dcb commit b267d07
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
31 changes: 14 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ Searches for the process by the specified `pid`.
```ts
import {lookup} from '@webpod/ps'

// A simple pid lookup
lookup({pid: 12345}, (err, resultList) => {
// Both callback and promise styles are supported
const list = await lookup({pid: 12345})

// or
lookup({pid: 12345}, (err, list) => {
if (err) {
throw new Error(err)
}

var process = resultList[0]
if (process) {
console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments)
const [found] = list
if (found) {
console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s', found.pid, found.command, found.arguments)
} else {
console.log('No such process found!')
}
Expand All @@ -45,19 +48,13 @@ lookup({pid: 12345}, (err, resultList) => {

Define a query opts to filter the results by `command` and/or `arguments` predicates:
```ts
lookup({
const list = await lookup({
command: 'node', // it will be used to build a regex
arguments: '--debug',
}, (err, resultList) => {
if (err) {
throw new Error(err)
}
})

resultList.forEach(process => {
if (process) {
console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments);
}
})
list.forEach(entry => {
console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s', entry.pid, entry.command, entry.arguments);
})
```

Expand Down Expand Up @@ -88,7 +85,7 @@ Eliminates the process by its `pid`.
```ts
import { kill } from '@webpod/ps'

kill('12345', err => {
kill('12345', (err, pid) => {
if (err) {
throw new Error(err)
} else {
Expand All @@ -103,7 +100,7 @@ Method `kill` also supports a `signal` option to be passed. It's only a wrapper
import { kill } from '@webpod/ps'

// Pass signal SIGKILL for killing the process without allowing it to clean up
kill('12345', 'SIGKILL', err => {
kill('12345', 'SIGKILL', (err, pid) => {
if (err) {
throw new Error(err)
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/main/ts/ps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export type TPsKillOptions = {
signal?: string | number | NodeJS.Signals
}

export type TPsNext = (err?: any) => void
export type TPsNext = (err?: any, data?: any) => void

/**
* Query Process: Focus on pid & cmd
Expand Down Expand Up @@ -159,7 +159,7 @@ export const kill = (pid: string | number, opts?: TPsNext | TPsKillOptions | TPs
if (err) {
clearTimeout(checkTimeoutTimer)
reject(err)
finishCallback?.(err)
finishCallback?.(err, pid)
}

else if (list.length > 0) {
Expand All @@ -171,8 +171,8 @@ export const kill = (pid: string | number, opts?: TPsNext | TPsKillOptions | TPs
checkConfident++
if (checkConfident === 5) {
clearTimeout(checkTimeoutTimer)
resolve()
finishCallback?.()
resolve(pid)
finishCallback?.(null, pid)
} else {
checkKilled(finishCallback)
}
Expand All @@ -186,7 +186,7 @@ export const kill = (pid: string | number, opts?: TPsNext | TPsKillOptions | TPs
next(new Error('Kill process timeout'))
}, timeout * 1000)
} else {
resolve()
resolve(pid)
}

return promise
Expand Down
3 changes: 2 additions & 1 deletion src/test/ts/ps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ describe('kill()', () => {
const pid = cp.fork(testScript, testScriptArgs).pid as number
assert.equal((await lookup({ pid })).length, 1)

await kill(pid, {timeout: 1}, cb)
const _pid = await kill(pid, {timeout: 1}, cb)
assert.equal(pid, _pid)
assert.equal((await lookup({ pid })).length, 0)
assert.equal(cheked, true)
})
Expand Down

0 comments on commit b267d07

Please sign in to comment.