Spawned API
TypeScript
Remotely execute commands within the model filesystem.
Events
This API has 3 events that can be subscribed to.
To receive events, call the the 'client.spawned.on' function on the API client. This will register a callback to fire when the Decthings server sends events to the client. In order for Decthings to send events, you must be subscribed to each spawned command. This can be done using the subscribeToEvents method. Alternatively, when you start a command using spawnCommand or spawnCommandForModel, you will by default atomatically be subscribed to events for that command.
/**
* Event emitted when a spawned command exits.
*/
on(event: 'exit', handler: (params: {
spawnedCommandId: string,
reason: SpawnedCommandTerminatedReason
}) => void): this
/**
* Event emitted when output for a spawned command is received on standard output (for example
* "console.log(msg)").
*/
on(event: 'stdout', handler: (params: {
spawnedCommandId: string,
data: Buffer
}) => void): this
/**
* Event emitted when output for a spawned command is received on standard output (for example
* "console.error(msg)").
*/
on(event: 'stderr', handler: (params: {
spawnedCommandId: string,
data: Buffer
}) => void): this
Where "SpawnedCommandTerminatedReason" is defined as:
export type SpawnedCommandTerminatedReason = {
code: 'terminated_on_request' | 'launcher_terminated' | 'inactive_timeout' | 'unknown'
} | {
code: 'process_exit',
exitCode?: number,
signal?: string,
oom: boolean
}
Subscribe to events example
exit
let client = new DecthingsClient({ apiKey: "<myapikey>" })
async function main() {
// First, subscribe to events so that Decthings will actually send us any
await client.spawned.subscribeToEvents({ spawnedCommandId: '<spawnedcommandid>' })
// Next, attach a listener which will be called when Decthings sends us events
client.spawned.on('exit', ({ spawnedCommandId, reason }) => {
// This function will run when an event is received
console.log(spawnedCommandId)
console.log(reason)
})
}
main()