Skip to content

Commit e74563f

Browse files
committed
fix timeout value
1 parent 5a3a97b commit e74563f

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

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

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ console.log('PID:', server.pid);
4141
console.log('Status:', server.status); // 'running'
4242
4343
// Process runs in background - your code continues
44+
4445
```
4546
</TypeScriptExample>
4647

@@ -50,17 +51,19 @@ Set working directory and environment variables:
5051

5152
<TypeScriptExample>
5253
```
54+
5355
const process = await sandbox.startProcess('node server.js', {
54-
cwd: '/workspace/api',
55-
env: {
56-
NODE_ENV: 'production',
57-
PORT: '8080',
58-
API_KEY: env.API_KEY,
59-
DATABASE_URL: env.DATABASE_URL
60-
}
56+
cwd: '/workspace/api',
57+
env: {
58+
NODE_ENV: 'production',
59+
PORT: '8080',
60+
API_KEY: env.API_KEY,
61+
DATABASE_URL: env.DATABASE_URL
62+
}
6163
});
6264
6365
console.log('API server started');
66+
6467
```
6568
</TypeScriptExample>
6669

@@ -70,16 +73,18 @@ List and check running processes:
7073

7174
<TypeScriptExample>
7275
```
76+
7377
const processes = await sandbox.listProcesses();
7478
7579
console.log(`Running ${processes.length} processes:`);
7680
7781
for (const proc of processes) {
78-
console.log(`${proc.id}: ${proc.command} (${proc.status})`);
82+
console.log(`${proc.id}: ${proc.command} (${proc.status})`);
7983
}
8084
8185
// Check if specific process is running
8286
const isRunning = processes.some(p => p.id === processId && p.status === 'running');
87+
8388
```
8489
</TypeScriptExample>
8590

@@ -89,6 +94,7 @@ Stream logs in real-time to detect when a service is ready:
8994

9095
<TypeScriptExample>
9196
```
97+
9298
import { parseSSEStream, type LogEvent } from '@cloudflare/sandbox';
9399
94100
const server = await sandbox.startProcess('node server.js');
@@ -97,29 +103,33 @@ const server = await sandbox.startProcess('node server.js');
97103
const logStream = await sandbox.streamProcessLogs(server.id);
98104
99105
for await (const log of parseSSEStream<LogEvent>(logStream)) {
100-
console.log(log.data);
106+
console.log(log.data);
101107
102-
if (log.data.includes('Server listening')) {
103-
console.log('Server is ready!');
104-
break;
105-
}
108+
if (log.data.includes('Server listening')) {
109+
console.log('Server is ready!');
110+
break;
106111
}
112+
}
113+
107114
```
108115
</TypeScriptExample>
109116

110117
Or get accumulated logs:
111118

112119
<TypeScriptExample>
113120
```
121+
114122
const logs = await sandbox.getProcessLogs(server.id);
115123
console.log('Logs:', logs);
124+
116125
```
117126
</TypeScriptExample>
118127

119128
## Stop processes
120129

121130
<TypeScriptExample>
122131
```
132+
123133
// Stop specific process
124134
await sandbox.killProcess(server.id);
125135
@@ -128,6 +138,7 @@ await sandbox.killProcess(server.id, 'SIGKILL');
128138
129139
// Stop all processes
130140
await sandbox.killAllProcesses();
141+
131142
```
132143
</TypeScriptExample>
133144

@@ -137,6 +148,7 @@ Start services in sequence, waiting for dependencies:
137148

138149
<TypeScriptExample>
139150
```
151+
140152
import { parseSSEStream, type LogEvent } from '@cloudflare/sandbox';
141153
142154
// Start database first
@@ -145,26 +157,28 @@ const db = await sandbox.startProcess('redis-server');
145157
// Wait for database to be ready
146158
const dbLogs = await sandbox.streamProcessLogs(db.id);
147159
for await (const log of parseSSEStream<LogEvent>(dbLogs)) {
148-
if (log.data.includes('Ready to accept connections')) {
149-
break;
150-
}
160+
if (log.data.includes('Ready to accept connections')) {
161+
break;
162+
}
151163
}
152164
153165
// Now start API server (depends on database)
154166
const api = await sandbox.startProcess('node api-server.js', {
155-
env: { DATABASE_URL: 'redis://localhost:6379' }
167+
env: { DATABASE_URL: 'redis://localhost:6379' }
156168
});
157169
158170
console.log('All services running');
171+
159172
```
160173
</TypeScriptExample>
161174

162175
## Keep containers alive for long-running processes
163176

164-
By default, containers automatically shut down after 3 minutes of inactivity. For long-running processes that may have idle periods (like CI/CD pipelines, batch jobs, or monitoring tasks), use the `keepAlive` option:
177+
By default, containers automatically shut down after 10 minutes of inactivity. For long-running processes that may have idle periods (like CI/CD pipelines, batch jobs, or monitoring tasks), use the `keepAlive` option:
165178

166179
<TypeScriptExample>
167180
```
181+
168182
import { getSandbox, parseSSEStream, type LogEvent } from '@cloudflare/sandbox';
169183
170184
export default {
@@ -194,8 +208,10 @@ export default {
194208
// Important: Must explicitly destroy when done
195209
await sandbox.destroy();
196210
}
197-
}
211+
212+
}
198213
};
214+
199215
```
200216
</TypeScriptExample>
201217

@@ -219,14 +235,16 @@ Check logs to see why:
219235

220236
<TypeScriptExample>
221237
```
238+
222239
const process = await sandbox.startProcess('node server.js');
223240
await new Promise(resolve => setTimeout(resolve, 1000));
224241
225242
const processes = await sandbox.listProcesses();
226243
if (!processes.find(p => p.id === process.id)) {
227-
const logs = await sandbox.getProcessLogs(process.id);
228-
console.error('Process exited:', logs);
244+
const logs = await sandbox.getProcessLogs(process.id);
245+
console.error('Process exited:', logs);
229246
}
247+
230248
```
231249
</TypeScriptExample>
232250

@@ -236,8 +254,10 @@ Kill existing processes before starting:
236254

237255
<TypeScriptExample>
238256
```
257+
239258
await sandbox.killAllProcesses();
240259
const server = await sandbox.startProcess('node server.js');
260+
241261
```
242262
</TypeScriptExample>
243263

@@ -248,3 +268,4 @@ const server = await sandbox.startProcess('node server.js');
248268
- [Execute commands guide](/sandbox/guides/execute-commands/) - One-time command execution
249269
- [Expose services guide](/sandbox/guides/expose-services/) - Make processes accessible
250270
- [Streaming output guide](/sandbox/guides/streaming-output/) - Monitor process output
271+
```

0 commit comments

Comments
 (0)