Skip to content

Commit 7a439b7

Browse files
Document process callback improvements and log capture fixes
Updates documentation to reflect bug fixes in sandbox-sdk PR #267: - Add onStart, onOutput, onExit, and onError callback documentation - Clarify that PID is now reliably available in process responses - Document that getProcessLogs() now correctly captures output from fast commands - Add guidance on when to use callbacks vs polling for process monitoring 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent cab1994 commit 7a439b7

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

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

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

99103
**Returns**: `Promise<ProcessInfo>` with `id`, `pid`, `command`, `status`
100104

@@ -108,6 +112,14 @@ const app = await sandbox.startProcess('node app.js', {
108112
cwd: '/workspace/my-app',
109113
env: { NODE_ENV: 'production', PORT: '3000' }
110114
});
115+
116+
// With callbacks for monitoring
117+
const service = await sandbox.startProcess('node service.js', {
118+
onStart: (process) => console.log(`Started with PID ${process.pid}`),
119+
onOutput: (stream, data) => console.log(`[${stream}] ${data}`),
120+
onExit: (exitCode) => console.log(`Exited with code ${exitCode}`),
121+
onError: (error) => console.error('Process error:', error)
122+
});
111123
```
112124
</TypeScriptExample>
113125

@@ -210,9 +222,23 @@ await new Promise(resolve => setTimeout(resolve, 5000));
210222
211223
const logs = await sandbox.getProcessLogs(server.id);
212224
console.log('Server logs:', logs);
225+
226+
// For fast commands, logs are available immediately after completion
227+
const build = await sandbox.startProcess('npm run build');
228+
// Wait for completion
229+
const processes = await sandbox.listProcesses();
230+
const buildProcess = processes.find(p => p.id === build.id);
231+
if (buildProcess?.status === 'completed') {
232+
const buildLogs = await sandbox.getProcessLogs(build.id);
233+
console.log('Build output:', buildLogs);
234+
}
213235
```
214236
</TypeScriptExample>
215237

238+
:::note
239+
For completed processes, `getProcessLogs()` automatically waits for all output to be captured before returning, ensuring complete logs even for fast-executing commands.
240+
:::
241+
216242
## Related resources
217243

218244
- [Background processes guide](/sandbox/guides/background-processes/) - Managing long-running processes

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,50 @@ console.log('API server started');
6464
```
6565
</TypeScriptExample>
6666

67+
## Use callbacks for real-time monitoring
68+
69+
Monitor process lifecycle and output using callbacks instead of polling:
70+
71+
<TypeScriptExample>
72+
```
73+
const server = await sandbox.startProcess('node server.js', {
74+
onStart: (process) => {
75+
console.log(`Server started with PID ${process.pid}`);
76+
},
77+
onOutput: (stream, data) => {
78+
// Handle output in real-time
79+
if (stream === 'stdout') {
80+
console.log('Output:', data);
81+
} else {
82+
console.error('Error:', data);
83+
}
84+
},
85+
onExit: (exitCode) => {
86+
console.log(`Server exited with code ${exitCode}`);
87+
},
88+
onError: (error) => {
89+
console.error('Process error:', error);
90+
}
91+
});
92+
93+
// Process continues running in background
94+
// Callbacks fire automatically as events occur
95+
```
96+
</TypeScriptExample>
97+
98+
**When to use callbacks:**
99+
100+
- **Real-time monitoring** - React immediately to process events
101+
- **Error detection** - Catch failures as they happen
102+
- **Progress tracking** - Monitor long-running operations
103+
- **Integration** - Forward output to logging systems or user interfaces
104+
105+
**When to use polling:**
106+
107+
- **Periodic checks** - Query status at intervals
108+
- **Batch processing** - Collect accumulated logs after completion
109+
- **Simpler logic** - When immediate notification is not required
110+
67111
## Monitor process status
68112

69113
List and check running processes:

0 commit comments

Comments
 (0)