# `Condukt.Sandbox.Services`
[🔗](https://github.com/tuist/tuist/blob/condukt@3.0.0/condukt/lib/condukt/sandbox/services.ex#L1)

Long-lived processes in a sandbox, such as a dev server or a database,
that belong to a session rather than a turn: aborting a turn or a tool
call leaves them alone, and they run until the services process stops.

Each service is a sandbox process (`c:Condukt.Sandbox.start_process/4`)
named `condukt-service:<owner>:<name>`, outside the ids `Condukt.Tools.Bash`
uses, so nothing a turn does reaches it. The services process starts them,
checks their readiness, restarts one that exits, and exposes its port when
the sandbox implements `c:Condukt.Sandbox.expose_port/3`.

    {:ok, services} =
      Condukt.Sandbox.Services.start_link(sandbox,
        owner: session_id,
        services: [
          %{name: "web", command: "bin/dev", port: 4000, ready: {:port, 4000}},
          %{name: "db", command: "postgres -D tmp/db", ready: {:command, "pg_isready"}}
        ]
      )

    Condukt.Sandbox.Services.list(services)
    #=> [%{name: "web", status: :ready, url: "https://…", restarts: 0}, …]

A service map takes `:name`, `:command`, and optionally `:cwd`, `:env`,
`:port` (exposed once ready), `:ready` (`{:port, port}`, checked with
bash's `/dev/tcp`, or `{:command, shell}`, which must exit 0; without it a
running service is ready), and `:restart` (`:always`, the default,
`:on_failure`, or `:never`). Restarts back off from one second to a minute,
and the backoff starts over once a service has stayed ready for 30 seconds,
so one that crashes soon after it is ready keeps backing off. A failed
status read leaves a service alone until the next check; only a process the
sandbox no longer has counts as exited.

Subscribers get `{Condukt.Sandbox.Services, name, status}` whenever a
service's status changes. Pass `subscribers: [pid]` to `start_link/2` to
hear the first changes, which may come before a later `subscribe/1`. A host shows `list/1` beside the session's view;
the session itself does not hold services.

Environment and secrets are given once, at start. `restart/2` with `:env`
starts a service again with new ones, as refreshing credentials needs.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `list`

Each service's name, `status` (`:starting | :ready | :exited | :failed`), `url`, and `restarts`.

# `process_id`

The sandbox process id of a service.

# `restart`

Starts a service again, with `:env` replacing its environment when given.

# `start_link`

# `subscribe`

Subscribes the caller to status changes.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
