Skip to content

Commit 2452cc2

Browse files
add dev server to it
1 parent 08ad76d commit 2452cc2

1 file changed

Lines changed: 75 additions & 11 deletions

File tree

docs/sandbox/auto-resume.mdx

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,48 +55,112 @@ If you use `resumeOn: "off"`, resume explicitly with [`Sandbox.connect()`](/docs
5555

5656
## Use cases
5757

58-
### Running a web server
58+
### Web and dev/preview servers
5959

60-
Use `onTimeout: "pause"` + `resumeOn: "any"` so traffic can wake a paused sandbox automatically.
60+
Use `onTimeout: "pause"` + `resumeOn: "any"` so inbound traffic can wake a paused sandbox automatically.
61+
This works for both:
62+
- Basic web/API servers
63+
- Dev or preview servers you open occasionally
6164

6265
<CodeGroup>
6366
```js JavaScript & TypeScript
6467
import { Sandbox } from 'e2b'
6568

6669
const sandbox = await Sandbox.create({
67-
timeoutMs: 5 * 60 * 1000,
70+
timeoutMs: 10 * 60 * 1000,
6871
lifecycle: {
6972
onTimeout: 'pause',
7073
resumeOn: 'any',
7174
},
7275
})
7376

74-
await sandbox.commands.run('python -m http.server 8080', { background: true })
77+
// Example: app source already exists in /home/user/app.
78+
// Replace this command with your API server, Next.js, Vite, etc.
79+
await sandbox.commands.run(
80+
'cd /home/user/app && npm install && npm run dev -- --host 0.0.0.0 --port 3000',
81+
{ background: true }
82+
)
7583

76-
const host = sandbox.getHost(8080)
77-
console.log(`Web server URL: https://${host}`)
84+
const previewHost = sandbox.getHost(3000)
85+
console.log(`Preview URL: https://${previewHost}`)
7886
```
7987
```python Python
8088
from e2b import Sandbox
8189

8290
sandbox = Sandbox.create(
83-
timeout=5 * 60,
91+
timeout=10 * 60,
8492
lifecycle={
8593
"on_timeout": "pause",
8694
"resume_on": "any",
8795
},
8896
)
8997

90-
sandbox.commands.run("python -m http.server 8080", background=True)
98+
# Example: app source already exists in /home/user/app.
99+
# Replace this command with your API server, Next.js, Vite, etc.
100+
sandbox.commands.run(
101+
"cd /home/user/app && npm install && npm run dev -- --host 0.0.0.0 --port 3000",
102+
background=True,
103+
)
91104

92-
host = sandbox.get_host(8080)
93-
print(f"Web server URL: https://{host}")
105+
preview_host = sandbox.get_host(3000)
106+
print(f"Preview URL: https://{preview_host}")
94107
```
95108
</CodeGroup>
96109

97110
### Agent/tool execution
98111

99-
The same lifecycle configuration works for agent and tool workloads: after timeout, the sandbox pauses, and the next execution request can wake it automatically.
112+
For queued tasks or tool calls, create once, store the sandbox ID, and reconnect per task. If the sandbox is paused, `connect` resumes it.
113+
114+
<CodeGroup>
115+
```js JavaScript & TypeScript
116+
import { Sandbox } from 'e2b'
117+
118+
// One-time setup
119+
const sandbox = await Sandbox.create({
120+
timeoutMs: 5 * 60 * 1000,
121+
lifecycle: {
122+
onTimeout: 'pause',
123+
resumeOn: 'any',
124+
},
125+
})
126+
const sandboxId = sandbox.sandboxId
127+
128+
// Later: called for each agent/tool task
129+
async function runToolTask(command) {
130+
const worker = await Sandbox.connect(sandboxId, {
131+
timeoutMs: 5 * 60 * 1000,
132+
})
133+
const result = await worker.commands.run(command)
134+
return result.stdout
135+
}
136+
137+
console.log(await runToolTask('python -c "print(2 + 2)"'))
138+
```
139+
```python Python
140+
from e2b import Sandbox
141+
142+
# One-time setup
143+
sandbox = Sandbox.create(
144+
timeout=5 * 60,
145+
lifecycle={
146+
"on_timeout": "pause",
147+
"resume_on": "any",
148+
},
149+
)
150+
sandbox_id = sandbox.sandbox_id
151+
152+
# Later: called for each agent/tool task
153+
def run_tool_task(command: str) -> str:
154+
worker = Sandbox.connect(
155+
sandbox_id,
156+
timeout=5 * 60,
157+
)
158+
result = worker.commands.run(command)
159+
return result.stdout
160+
161+
print(run_tool_task('python -c "print(2 + 2)"'))
162+
```
163+
</CodeGroup>
100164

101165
## Cleanup
102166
Auto-resume is persistent, meaning if your sandbox resumes and later times out again, it will pause again.

0 commit comments

Comments
 (0)