@phylum/process-task v3.1.0
Process Task
Create tasks that run child processes.
Installation
npm i @phylum/process-task
Usage
createProcessTask(spawn[, options])
Create a task that spawns a process and waits for it to exit.
const cp = require('child_process')
const {createProcessTask} = require('@phylum/process-task')
const task = createProcessTask(ctx => cp.exec('echo Hello World!'))
- spawn
<function>
- A function to spawn the process. + ctx<Context>
- The task context is passed with the first argument. + return<ChildProcess>
- A child process (or any object that exposes the same api) - options
<object>
- Optional. An object with the following options: + expect<number> | <string>
- The exit code or signal to expect. If the process exits with another code or signal, the task will reject. Default is0
+ killOnDispose<boolean>
- True to kill the process when the task is disposed and the process is still running. If your process will run forever, you should set this totrue
. Default isfalse
.
ProcessTaskState
The ProcessTaskState class manages a single child process that can be spawned and killed manually. It is killed automatically when the task is disposed.
const {ProcessTaskState} = require('@phylum/process-task')
new ProcessTaskState(ctx, spawn)
const cp = require('child_process')
async function example(ctx) {
const state = new ProcessTaskState(ctx, () => {
return cp.fork('foo.js')
})
state.spawn()
}
- ctx
<Pipeline.Context>
- The pipeline context to attach to. - spawn
<function>
- A function to spawn a child process. The context is passed with the first argument and the function must return the new child process.
state.ctx
Get the <Pipeline.Context>
the state was bound to.
state.ctx === ctx
state.process
Get the current <ChildProcess>
.
If the process has been killed using state.kill(..)
or the process emitted an exit event, this property will be set to null
if (state.process) {
state.process.send('Hello World!')
}
state.spawn()
Spawn the process if not alive.
if (state.spawn()) {
// A new process has been spawned.
} else {
// The process is alive.
}
- returns
<ChildProcess> | false
- The new child process if created or false otherwise.
state.respawn()
Kill the current process if alive and spawn a new one.
const proc = state.respawn()
- returns
<ChildProcess>
- The new child process.
state.kill([signal])
Kill the current process if alive.
Only use this function if you are expecting the process to exit after receiving the signal. Otherwise use state.process.kill(..)
state.kill()
state.process === null
Note that state.process
will be set to null
after calling this function.
Event: 'spawn'
The spawn
event is emitted when a new process has been spawned using state.spawn(..)
or state.respawn(..)
. The child process is passed with the first argument. This can be useful for listening to process related events like ipc messages.
state.on('spawn', process => {
process.on('message', msg => {
console.log('Message from child process:', msg)
})
})