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

The server's job queue (design notes §12): first in, first out, one job running at a
time. Pure: the server keeps it in its state.

A job is any term with a `client` (the pid its output goes to). `position/2` is the
number of jobs ahead of a client's: 0 while it runs.

# `job`

```elixir
@type job() :: %{:client =&gt; pid(), optional(atom()) =&gt; term()}
```

# `t`

```elixir
@type t() :: %Juggernaut.Server.Queue{
  running: job() | nil,
  waiting: :queue.queue(job())
}
```

# `done`

```elixir
@spec done(t()) :: t()
```

Marks the running job done.

# `drop`

```elixir
@spec drop(t(), pid()) :: t()
```

Drops the waiting jobs of `client` (it went away).

# `idle?`

```elixir
@spec idle?(t()) :: boolean()
```

Whether nothing runs and nothing waits.

# `new`

```elixir
@spec new() :: t()
```

An empty queue.

# `next`

```elixir
@spec next(t()) :: {job(), t()} | :none
```

Takes the next job to run when none is running: `{job, queue}` or `:none`.

# `positions`

```elixir
@spec positions(t()) :: [{job(), pos_integer()}]
```

The waiting jobs with their positions, in order (the first is 1 when one runs).

# `push`

```elixir
@spec push(t(), job()) :: {non_neg_integer(), t()}
```

Adds `job` at the end; returns its position (jobs ahead of it, the running one included).

# `waiting`

```elixir
@spec waiting(t()) :: non_neg_integer()
```

The number of waiting jobs.

---

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