Skip to content

Commit b20830d

Browse files
Document process callback options and fix
Add comprehensive documentation for startProcess() callback options (onStart, onOutput, onExit, onError) that were fixed in PR 267. Changes: - Add all ProcessOptions parameters to Commands API reference - Add callback monitoring section to background processes guide - Include practical examples showing real-time process monitoring Fixes process callback documentation gap discovered in issue 262 where callbacks were broken and undocumented. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 64e0b36 commit b20830d

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

src/content/docs/sandbox/api/commands.mdx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ const process = await sandbox.startProcess(command: string, options?: ProcessOpt
9595
- `options` (optional):
9696
- `cwd` - Working directory
9797
- `env` - Environment variables
98-
- `onStart` - Callback when process starts: `(process: ProcessInfo) => void`
98+
- `timeout` - Maximum execution time in milliseconds
99+
- `onStart` - Callback when process starts: `(process: Process) => void`
99100
- `onOutput` - Callback for real-time output: `(stream: 'stdout' | 'stderr', data: string) => void`
100-
- `onExit` - Callback when process completes: `(exitCode: number | null) => void`
101-
- `onError` - Callback for errors: `(error: Error) => void`
101+
- `onExit` - Callback when process exits: `(exitCode: number | null) => void`
102+
- `onError` - Callback for process errors: `(error: Error) => void`
102103

103104
**Returns**: `Promise<ProcessInfo>` with `id`, `pid`, `command`, `status`
104105

@@ -113,19 +114,19 @@ const app = await sandbox.startProcess('node app.js', {
113114
env: { NODE_ENV: 'production', PORT: '3000' }
114115
});
115116
116-
// With callbacks for real-time monitoring
117+
// With callbacks for monitoring
117118
const build = await sandbox.startProcess('npm run build', {
118-
onStart: (proc) => {
119-
console.log(`Build started with PID ${proc.pid}`);
119+
onStart: (process) => {
120+
console.log(`Process started with PID: ${process.pid}`);
120121
},
121122
onOutput: (stream, data) => {
122123
console.log(`[${stream}] ${data}`);
123124
},
124125
onExit: (exitCode) => {
125-
console.log(`Build finished with exit code ${exitCode}`);
126+
console.log(`Process exited with code: ${exitCode}`);
126127
},
127128
onError: (error) => {
128-
console.error(`Build error: ${error.message}`)
129+
console.error('Process error:', error.message);
129130
}
130131
});
131132
```

src/content/docs/sandbox/guides/background-processes.mdx

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,36 +70,33 @@ Use callbacks for real-time process monitoring without manually streaming logs:
7070

7171
<TypeScriptExample>
7272
```
73-
const process = await sandbox.startProcess('npm run build', {
74-
onStart: (proc) => {
75-
console.log(`Build started with PID ${proc.pid}`);
73+
const server = await sandbox.startProcess('node server.js', {
74+
onStart: (process) => {
75+
console.log(`Server started with PID: ${process.pid}`);
7676
},
7777
onOutput: (stream, data) => {
78-
// Handle output in real-time as it's produced
79-
if (stream === 'stdout') {
80-
console.log(data);
81-
} else {
82-
console.error(data);
83-
}
78+
// Real-time output from stdout/stderr
79+
console.log(`[${stream}] ${data}`);
8480
},
8581
onExit: (exitCode) => {
8682
if (exitCode === 0) {
87-
console.log('Build completed successfully');
83+
console.log('Server stopped gracefully');
8884
} else {
89-
console.error(`Build failed with exit code ${exitCode}`);
85+
console.error(`Server crashed with code: ${exitCode}`);
9086
}
9187
},
9288
onError: (error) => {
93-
console.error(`Build error: ${error.message}`);
89+
console.error('Server error:', error.message);
9490
}
9591
});
9692
97-
// Process monitoring happens in background
98-
// Your code continues immediately
93+
// Your code continues - callbacks fire in background
9994
```
10095
</TypeScriptExample>
10196

102-
Callbacks are useful when you need to react to process events without blocking your Worker execution or manually managing log streams.
97+
:::note[Callbacks vs streaming]
98+
Callbacks are ideal for background monitoring where you want to react to events without blocking. For detailed log analysis or waiting for specific output, use [`streamProcessLogs()`](/sandbox/api/commands/#streamprocesslogs) instead.
99+
:::
103100

104101
## Monitor process status
105102

0 commit comments

Comments
 (0)