Skip to content

Commit 6259cde

Browse files
Document process callback improvements and getLogs reliability
Add documentation for process lifecycle callbacks (onStart, onOutput, onExit, onError) in startProcess() that enable real-time process monitoring without manual log streaming. Clarify that getProcessLogs() reliably captures output from fast-completing commands following race condition fix. Related to cloudflare/sandbox-sdk#267
1 parent bed8245 commit 6259cde

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
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 when process starts: `(process: ProcessInfo) => void`
99+
- `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`
98102

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

@@ -108,6 +112,22 @@ 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 real-time monitoring
117+
const build = await sandbox.startProcess('npm run build', {
118+
onStart: (proc) => {
119+
console.log(`Build started with PID ${proc.pid}`);
120+
},
121+
onOutput: (stream, data) => {
122+
console.log(`[${stream}] ${data}`);
123+
},
124+
onExit: (exitCode) => {
125+
console.log(`Build finished with exit code ${exitCode}`);
126+
},
127+
onError: (error) => {
128+
console.error(`Build error: ${error.message}`);
129+
}
130+
});
111131
```
112132
</TypeScriptExample>
113133

@@ -192,7 +212,7 @@ for await (const log of parseSSEStream<LogEvent>(logStream)) {
192212

193213
### `getProcessLogs()`
194214

195-
Get accumulated logs from a process.
215+
Get accumulated logs from a process. Includes all output captured since the process started, including output from fast-completing commands.
196216

197217
```ts
198218
const logs = await sandbox.getProcessLogs(processId: string): Promise<string>
@@ -210,6 +230,11 @@ await new Promise(resolve => setTimeout(resolve, 5000));
210230
211231
const logs = await sandbox.getProcessLogs(server.id);
212232
console.log('Server logs:', logs);
233+
234+
// Works reliably for fast commands too
235+
const quickCommand = await sandbox.startProcess('echo "Hello World"');
236+
const output = await sandbox.getProcessLogs(quickCommand.id);
237+
console.log(output); // "Hello World"
213238
```
214239
</TypeScriptExample>
215240

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

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

67+
## Monitor with callbacks
68+
69+
Use callbacks for real-time process monitoring without manually streaming logs:
70+
71+
<TypeScriptExample>
72+
```
73+
const process = await sandbox.startProcess('npm run build', {
74+
onStart: (proc) => {
75+
console.log(`Build started with PID ${proc.pid}`);
76+
},
77+
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+
}
84+
},
85+
onExit: (exitCode) => {
86+
if (exitCode === 0) {
87+
console.log('Build completed successfully');
88+
} else {
89+
console.error(`Build failed with exit code ${exitCode}`);
90+
}
91+
},
92+
onError: (error) => {
93+
console.error(`Build error: ${error.message}`);
94+
}
95+
});
96+
97+
// Process monitoring happens in background
98+
// Your code continues immediately
99+
```
100+
</TypeScriptExample>
101+
102+
Callbacks are useful when you need to react to process events without blocking your Worker execution or manually managing log streams.
103+
67104
## Monitor process status
68105

69106
List and check running processes:

0 commit comments

Comments
 (0)