Skip to content

Commit 64e0b36

Browse files
Document process callbacks and fixes
Sync changes from cloudflare/sandbox-sdk PR #267: - Add onOutput, onExit, onStart, and onError callbacks to startProcess() - Document callback-based monitoring as preferred pattern - Show accumulated logs pattern for fast commands - Update API reference with complete ProcessOptions parameters Fixes documented: - Process callbacks now work (onOutput/onExit previously never fired) - PID field now properly populated (was always undefined) - getLogs() race condition fixed for fast commands 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 6259cde commit 64e0b36

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ const build = await sandbox.startProcess('npm run build', {
125125
console.log(`Build finished with exit code ${exitCode}`);
126126
},
127127
onError: (error) => {
128-
console.error(`Build error: ${error.message}`);
128+
console.error(`Build error: ${error.message}`)
129129
}
130130
});
131131
```

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,40 @@ const isRunning = processes.some(p => p.id === processId && p.status === 'runnin
120120
```
121121
</TypeScriptExample>
122122

123-
## Monitor process logs
123+
## Monitor process output
124124

125-
Stream logs in real-time to detect when a service is ready:
125+
### Using callbacks
126+
127+
Use callbacks to handle output in real-time without manually managing streams:
128+
129+
<TypeScriptExample>
130+
```
131+
const server = await sandbox.startProcess('node server.js', {
132+
onStart: (process) => {
133+
console.log(`Server started with PID: ${process.pid}`);
134+
},
135+
onOutput: (stream, data) => {
136+
console.log(`[${stream}] ${data}`);
137+
138+
if (data.includes('Server listening')) {
139+
console.log('Server is ready!');
140+
}
141+
},
142+
onExit: (exitCode) => {
143+
console.log(`Server exited with code: ${exitCode}`);
144+
},
145+
onError: (error) => {
146+
console.error('Streaming error:', error);
147+
}
148+
});
149+
150+
// Your code continues - callbacks handle output in the background
151+
```
152+
</TypeScriptExample>
153+
154+
### Using streams
155+
156+
Stream logs in real-time with manual stream handling:
126157

127158
<TypeScriptExample>
128159
```
@@ -144,10 +175,17 @@ for await (const log of parseSSEStream<LogEvent>(logStream)) {
144175
```
145176
</TypeScriptExample>
146177

147-
Or get accumulated logs:
178+
### Using accumulated logs
179+
180+
Get all accumulated logs at once:
148181

149182
<TypeScriptExample>
150183
```
184+
const server = await sandbox.startProcess('echo "Hello, World!"');
185+
186+
// Wait briefly for fast commands to complete
187+
await new Promise(resolve => setTimeout(resolve, 100));
188+
151189
const logs = await sandbox.getProcessLogs(server.id);
152190
console.log('Logs:', logs);
153191
```

0 commit comments

Comments
 (0)