Skip to content

Commit 08ad76d

Browse files
example for web server user case
1 parent 74a11e4 commit 08ad76d

1 file changed

Lines changed: 40 additions & 6 deletions

File tree

docs/sandbox/auto-resume.mdx

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

5656
## Use cases
5757

58-
1. Running a web server
59-
Configure `lifecycle` with `onTimeout: "pause"` and `resumeOn: "any"` so the sandbox can resume when traffic hits your service again. Useful if you:
58+
### Running a web server
6059

61-
- Have a webpage you only want to check periodically
62-
- Have an API you want to expose for testing or other purposes
60+
Use `onTimeout: "pause"` + `resumeOn: "any"` so traffic can wake a paused sandbox automatically.
6361

64-
2. Code execution workloads
65-
For agent/tool execution, use `resumeOn: "any"` so the sandbox can start back up automatically when the next command or code execution request arrives.
62+
<CodeGroup>
63+
```js JavaScript & TypeScript
64+
import { Sandbox } from 'e2b'
65+
66+
const sandbox = await Sandbox.create({
67+
timeoutMs: 5 * 60 * 1000,
68+
lifecycle: {
69+
onTimeout: 'pause',
70+
resumeOn: 'any',
71+
},
72+
})
73+
74+
await sandbox.commands.run('python -m http.server 8080', { background: true })
75+
76+
const host = sandbox.getHost(8080)
77+
console.log(`Web server URL: https://${host}`)
78+
```
79+
```python Python
80+
from e2b import Sandbox
81+
82+
sandbox = Sandbox.create(
83+
timeout=5 * 60,
84+
lifecycle={
85+
"on_timeout": "pause",
86+
"resume_on": "any",
87+
},
88+
)
89+
90+
sandbox.commands.run("python -m http.server 8080", background=True)
91+
92+
host = sandbox.get_host(8080)
93+
print(f"Web server URL: https://{host}")
94+
```
95+
</CodeGroup>
96+
97+
### Agent/tool execution
98+
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.
66100

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

0 commit comments

Comments
 (0)