Skip to content

Commit

Permalink
Add pidExists (#53)
Browse files Browse the repository at this point in the history
* Add pidExists
  • Loading branch information
JordanMartinez authored Jul 25, 2023
1 parent d9ad6d0 commit 320afb1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Breaking changes:

New features:
- Added `fromKillSignal` (#51 by @JordanMartinez)
- Added `pidExists` (#52 by @JordanMartinez)

Other improvements:
- Fix regression: add `ref`/`unref` APIs that were dropped in `v10.0.0` (#50 by @JordanMartinez)
Expand Down
4 changes: 4 additions & 0 deletions src/Node/ChildProcess.purs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module Node.ChildProcess
, stdout
, stderr
, pid
, pidExists
, connected
, disconnect
, exitCode
Expand Down Expand Up @@ -158,6 +159,9 @@ stderr = toUnsafeChildProcess >>> UnsafeCP.unsafeStderr >>> unsafeFromNull
pid :: ChildProcess -> Effect (Maybe Pid)
pid = unsafeCoerce SafeCP.pid

pidExists :: ChildProcess -> Effect Boolean
pidExists = unsafeCoerce SafeCP.pidExists

-- | Indicates whether it is still possible to send and receive
-- | messages from the child process.
connected :: ChildProcess -> Effect Boolean
Expand Down
8 changes: 7 additions & 1 deletion src/Node/UnsafeChildProcess/Safe.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Node.UnsafeChildProcess.Safe
, messageH
, spawnH
, pid
, pidExists
, connected
, disconnect
, exitCode
Expand All @@ -36,7 +37,7 @@ import Data.Posix.Signal as Signal
import Effect (Effect)
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, mkEffectFn2, runEffectFn1, runEffectFn2)
import Foreign (Foreign)
import Node.ChildProcess.Types (Exit(..), Handle, KillSignal, StdIO, UnsafeChildProcess, ipc, pipe, stringSignal)
import Node.ChildProcess.Types (Exit(..), Handle, KillSignal, StdIO, UnsafeChildProcess, intSignal, ipc, pipe, stringSignal)
import Node.Errors.SystemError (SystemError)
import Node.EventEmitter (EventEmitter, EventHandle(..))
import Node.EventEmitter.UtilTypes (EventHandle0, EventHandle1)
Expand Down Expand Up @@ -79,6 +80,11 @@ pid cp = map toMaybe $ runEffectFn1 pidImpl cp

foreign import pidImpl :: EffectFn1 (UnsafeChildProcess) (Nullable Pid)

-- | Note: this will not work if the user does not have permission to kill
-- | a `PID`. Uses `cp.kill(0)` underneath.
pidExists :: UnsafeChildProcess -> Effect Boolean
pidExists cp = kill' (intSignal 0) cp

-- | Indicates whether it is still possible to send and receive
-- | messages from the child process.
connected :: UnsafeChildProcess -> Effect Boolean
Expand Down

0 comments on commit 320afb1

Please sign in to comment.