# `Juggernaut.Server`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/server.ex#L1)

The warm daemon of one checkout (`mix juggernaut.server`, design notes §12): one pool of
workers kept between runs, a FIFO queue of jobs, and a socket clients attach to.

It holds the checkout lock for as long as it runs, so a run that does not attach (for
example `--no-server`) is refused with exit 3 naming the server, and there are never two
pools. It listens on `127.0.0.1` on a port the system picks; `server/server.json` names
the port and `server/token` (mode 0600) holds the token every request must carry.

A `run` or `check` request is a job: the job runs the task (`mix juggernaut` or
`mix juggernaut.check`) in a process of its own with the client's argv, exactly as the
task runs standalone, with its output sent to the client (`Juggernaut.Server.Shell`).
Each job loads `.juggernaut.exs` again, and the `refuse_env` check runs on the client's
variable names before the job is queued. The pool boots with the first job (its compile
builds the sandbox the workers load) and is reused while the config fingerprint, the
pool size, `mix.exs`, `config/` and the test files stay the same (`pool_key/1`);
otherwise it is stopped and a new one boots. A stopped acceptor stops the server.
Workers load the beams a run's compile changed before its jobs
(`Juggernaut.Pool.Worker.reload/1`).

Options of `start_link/1`: `:root` (required), `:env` (`System.get_env/0`),
`:idle_timeout_ms` (default 30 minutes; 0 never), `:workers` (pool size; default from
the config), `:deps_path`, `:worker_opts` (extra worker options, for tests),
`:force_unlock` (remove the checkout lock first, whoever holds it). A lock taken over
from a holder that is gone is named by `status/1` under `"takeover"`.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `files`

```elixir
@spec files(Path.t()) :: {Path.t(), Path.t()}
```

The files that name a running server: `{server.json, token}` under `root`'s store.

# `pool_key`

```elixir
@spec pool_key(map()) :: term()
```

What a warm pool rests on: the config fingerprint (which covers `mix.lock`), the pool
size and the files workers load once, at boot (paths and contents): `mix.exs`, every
file under `config/` (the application config `loadconfig` and `app.config` read) and
the test files. A request with another key gets a new pool. The size is the server's
`--workers`, else the config's: a run's own `--workers` does not resize a warm pool.

# `start`

```elixir
@spec start(keyword()) :: GenServer.on_start()
```

Starts the server unlinked (a task waits on it with a monitor).

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

Starts the server (see the module doc).

# `status`

```elixir
@spec status(GenServer.server()) :: map()
```

The server's address and state: port, pool, queue and the running job.

# `stop`

```elixir
@spec stop(GenServer.server()) :: :ok
```

Stops the server: the pool, the lock and the address files go with it. A server that is
already gone (its idle timeout, `--stop` from another shell) is `:ok` too.

---

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