Juggernaut.Server.Queue (Juggernaut v0.4.0)

Copy Markdown View Source

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.

Summary

Functions

Marks the running job done.

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

Whether nothing runs and nothing waits.

An empty queue.

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

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

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

The number of waiting jobs.

Types

job()

@type job() :: %{:client => pid(), optional(atom()) => term()}

t()

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

Functions

done(queue)

@spec done(t()) :: t()

Marks the running job done.

drop(queue, client)

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

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

idle?(queue)

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

Whether nothing runs and nothing waits.

new()

@spec new() :: t()

An empty queue.

next(queue)

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

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

positions(queue)

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

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

push(queue, job)

@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(queue)

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

The number of waiting jobs.