Structures · Coordination

Complexity Advanced

FifoWorkQueue

Dispatch trail jobs in the order they entered the queue.

One bridge-inspection card rests on a notebook outside an empty work tray; two other notebooks have no job card.

Three hikers share one job list

Unlike a claim, a job can change hands. Every notebook holds the same queue of three jobs in arrival order: inspect the bridge, clear a fallen branch, and restock the first-aid cache. Alice, Bob, and Carol take work from it.

Workers do not pick jobs. A worker asks for the next job, and the queue gives that worker the oldest one. If Alice and Bob ask at the same moment, a merge cannot help: both would think they hold the bridge inspection. The ranger numbers the two requests, so Alice gets the first job and Bob the second.

A take, completion, or release does not change the shared board until the ranger numbers it, so no hiker sees a job as theirs before it is.

From queued work to one active holder

  1. Add at the tail. New work joins the back of the queue.
  2. Take the oldest job. A worker asks for the next job, not a named job. The oldest queued entry becomes that worker's held job.
  3. Complete or release. Completion removes held work. Release returns it to the tail, after jobs that were already waiting.
  4. Recover from worker loss. The runtime can remove a disconnected client and return its held work to the tail. Another worker can then run it.

The queue does not keep a completed-job archive. The demo's Done column is a separate session history, like Watershed's example.

Run the conflict

Follow several jobs through queued, in progress, released, and done states.

Dispatch the trail jobs

Take the oldest queued job. Complete it, or release it to the back of the queue.

Queue ruleFirst in, first out

Queued

Oldest job is first.

  1. 1. inspect bridge
  2. 2. clear fallen branch
  3. 3. restock first-aid cache

In progress

Each job has one holder.

  • No jobs in progress.

Done

Completion history is separate.

  • No completed jobs yet.

Alice

Held jobNone

Bob

Held jobNone

Carol

Held jobNone

Run the worked example

Alice and Bob take work. Alice releases hers. Carol then gets the next FIFO job, and Bob completes his.

Enable JavaScript to run the queue. The expected result remains in the lesson.

Ready: three jobs queued.

Read the result

Alice gets the oldest job, and Bob gets the next one. When Alice releases the bridge inspection, it returns behind the first-aid job. Carol therefore gets the first-aid job next. Bob's completed branch job leaves the queue; the demo records it in a separate session history.

A job can run twice

The queue gives each job one active holder, but it does not guarantee exactly-once execution. A worker can perform part of a job and fail before completion is accepted. The returned job can then run again, so job handlers should tolerate retries. Like every sequenced structure, the queue also needs the ranger station.

The next lesson, TaskManager, keeps one hiker on a duty and lines up the others as replacements.

Sources and implementation

Sources: Watershed's ordered_collection_kernel.

Implementation: The demo calls ordered_collection_kernel.add, ordered_collection_kernel.acquire, ordered_collection_kernel.complete, ordered_collection_kernel.release, ordered_collection_kernel.ack_local, ordered_collection_kernel.apply_remote, and ordered_collection_kernel.summary_jobs through the Gleam toolkit. The displayed client records and shared view come from those kernel results.