# Pause a worker Pause an idle worker to stop the clock, and resume it where it stopped. | Template | `base` (2 vCPU, 512 MiB, $0.039 per hour) | |---|---| | Run time | About 30 seconds, with 20 seconds paused | | Cost of a run | Less than $0.001. Paused time is free for up to 72 hours. | | Cleanup | The script kills the sandbox. Find paused ones with `fdtn ls -s paused`. | ## 1. The code The worker counts up once a second in memory and writes the count to a file. Python (worker.py): ```python import time from fdtn import Sandbox WORKER = """ import time n = 0 # state lives in memory only while True: n += 1 with open('/home/user/progress.txt', 'w') as f: f.write(str(n)) time.sleep(1) """ # 1. Start a sandbox and a long-running worker in it. sbx = Sandbox.create( "base", timeout=600, metadata={"example": "worker"}, ) sbx.files.write("/home/user/worker.py", WORKER) sbx.commands.run("python3 /home/user/worker.py", background=True, timeout=0) time.sleep(5) print("before pause:", sbx.files.read("/home/user/progress.txt")) # 2. Pause it. Memory, files and the running worker are saved. The clock stops. sbx.pause() sandbox_id = sbx.sandbox_id print("paused:", Sandbox.get_info(sandbox_id).state) time.sleep(20) # a paused sandbox costs nothing # 3. Resume it later, from this or another process, with the ID. sbx = Sandbox.connect(sandbox_id, timeout=600) print("after resume:", sbx.files.read("/home/user/progress.txt")) time.sleep(3) print("3 s later:", sbx.files.read("/home/user/progress.txt")) sbx.kill() ``` TypeScript (worker.mjs): ```typescript import { Sandbox } from '@fdtn/sdk' const sleep = (ms) => new Promise((r) => setTimeout(r, ms)) const WORKER = ` import time n = 0 # state lives in memory only while True: n += 1 with open('/home/user/progress.txt', 'w') as f: f.write(str(n)) time.sleep(1) ` // 1. Start a sandbox and a long-running worker in it. let sbx = await Sandbox.create('base', { timeoutMs: 600_000, metadata: { example: 'worker' }, }) await sbx.files.write('/home/user/worker.py', WORKER) await sbx.commands.run('python3 /home/user/worker.py', { background: true, timeoutMs: 0 }) await sleep(5_000) console.log('before pause:', await sbx.files.read('/home/user/progress.txt')) // 2. Pause it. Memory, files and the running worker are saved. The clock stops. await sbx.pause() const sandboxId = sbx.sandboxId console.log('paused:', (await Sandbox.getInfo(sandboxId)).state) await sleep(20_000) // a paused sandbox costs nothing // 3. Resume it later, from this or another process, with the ID. sbx = await Sandbox.connect(sandboxId, { timeoutMs: 600_000 }) console.log('after resume:', await sbx.files.read('/home/user/progress.txt')) await sleep(3_000) console.log('3 s later:', await sbx.files.read('/home/user/progress.txt')) await sbx.kill() ``` ## 2. Run it ```bash $ python worker.py # or: node worker.mjs before pause: 6 paused: paused after resume: 6 3 s later: 9 ``` The count stays at 6 while paused, then the same process counts on from its memory. ## Notes - `pause()` saves memory, files and running programs, then stops billing. - Any process with your API key can resume with `Sandbox.connect(sandbox_id, timeout=...)`, which also sets a new lifetime. - To pause in place of a kill when the lifetime ends, create with `lifecycle={"on_timeout": "pause"}`. [Lifetime](https://fdtn.dev/docs/lifecycle.md#lifetime) - A resume counts against the spending limit like a create. - A worker that runs 1 hour a day costs $0.039 a day. Left running, it costs $0.94. A paused sandbox is deleted 72 hours after its last pause. [Pause](https://fdtn.dev/docs/lifecycle.md#pause) ## From the CLI ```bash $ fdtn create base -d --name worker --timeout 600 $ fdtn exec -b @worker python3 -m http.server 8000 $ fdtn pause @worker $ fdtn ls -s paused $ fdtn resume @worker $ fdtn kill @worker ``` ## Next - [**Pause, resume and snapshots**: States, lifetime and limits.](https://fdtn.dev/docs/lifecycle.md) - [**Webhooks**: Get a call on pause or resume.](https://fdtn.dev/docs/webhooks.md) Source: https://fdtn.dev/docs/examples/background-worker/