diff --git a/src/content/docs/concepts/services.mdx b/src/content/docs/concepts/services.mdx index 2aab9c0..f5421d4 100644 --- a/src/content/docs/concepts/services.mdx +++ b/src/content/docs/concepts/services.mdx @@ -1,272 +1,223 @@ --- title: Services -description: Long-running processes that persist across Sprite reboots -draft: true +description: Processes the Sprite runtime starts at boot, restarts when they crash, and can wire to your Sprite's URL --- -import { LinkCard, CardGrid } from '@/components/react'; +import { Callout, LinkCard, CardGrid } from '@/components/react'; -Services are long-running processes managed by the Sprite runtime that automatically restart when your Sprite boots. Unlike detachable sessions, services survive full Sprite restarts and are ideal for background daemons, development servers, and databases. +A Sprite pauses when nothing is using it, and processes don't reliably survive the trip. A warm wake resumes them where they left off; a cold boot drops them entirely. Anything you started by hand in a shell, a dev server, a database, an agent, is gone after a cold wake unless something brings it back. -## Overview +That something is a service. A service is a process the Sprite runtime owns: it starts when the Sprite boots, restarts if it crashes, and can receive the HTTP traffic that hits your Sprite's URL. You define it once. The runtime keeps bringing it back. -Services are managed through the internal API at `/.sprite/api.sock`. The `sprite-env` command provides a convenient wrapper: +## Services and the Sprite lifecycle + +What happens to a service depends on how the Sprite went down and how it came back: + +- **Warm wake.** The VM was suspended with your process inside it. The process resumes mid-thought; it is not restarted. Wakes take 100–500ms. +- **Cold boot.** Process state was dropped. The runtime starts every service fresh, in dependency order. Wakes take 1–2s. +- **Crash.** A service process that exits on its own gets restarted by the runtime. +- **Stop.** A service you stop explicitly stays stopped until you start it again. + +Services don't keep a Sprite from pausing. A Sprite with ten services defined still pauses when it goes idle; the services come back on the next wake. A service that's actively handling HTTP requests counts as activity, the same as any other traffic, but a quiet one doesn't hold the Sprite in an active state. If you need a Sprite to stay up while work finishes, that's a job for the [Tasks API](/keeping-sprites-running), not a service. + +## Create a service + +The `sprite-env` CLI ships in every Sprite and talks to the runtime's management socket. Create a service with a name, a command, and its arguments: ```bash -# List all services -sprite-env services list +sprite-env services create web --cmd python3 --args "-m,http.server,3000" --http-port 3000 +``` + +`--cmd` takes the binary only. Arguments go in `--args`, comma-separated. + +The command starts the service and streams its first few seconds of output as NDJSON, so a service that dies on startup fails in front of you instead of silently in the background: -# Get help and see all endpoints -sprite-env services --help +```json +{"type":"started","timestamp":1780420274555} +{"type":"complete","log_files":{"combined":"/.sprite/logs/services/web.log","stderr":"/.sprite/logs/services/web.log","stdout":"/.sprite/logs/services/web.log"},"timestamp":1780420276551} ``` -## Creating Services +The stream watches for 5 seconds by default. Pass `--duration 30s` to watch longer, or `--no-stream` to return immediately. -Create a service with a PUT request: +Confirm it's serving: ```bash -# Simple service -sprite-env curl -X PUT /v1/services/myapp -d '{ - "cmd": "/usr/bin/myapp", - "args": ["--port", "8080"] -}' - -# Service with dependencies -sprite-env curl -X PUT /v1/services/webapp -d '{ - "cmd": "npm", - "args": ["run", "dev"], - "needs": ["database"] -}' +curl localhost:3000 ``` -### Service Configuration +### Create options + +| Flag | What it does | +|------|--------------| +| `--cmd ` | The executable to run. Required. Binary only, no arguments here. | +| `--args ` | Comma-separated arguments: `--args "-m,http.server,3000"` | +| `--env ` | Comma-separated environment variables: `--env "PORT=3000,DEBUG=1"` | +| `--dir ` | Working directory for the process | +| `--needs ` | Services that must start before this one | +| `--http-port ` | Route the Sprite's URL to this port and auto-start the service on incoming requests | +| `--duration