[ norboten ]
python · lab python-04

The Worker That Goes Silent

· about 35 minutes · runs on ubuntu-26.04-devops · unrated

An unrated lab. It runs on your machine with no account and no network, and everything about it — the faults, the checks, the hints and the reference solution — is in the repository. An attempt on it is recorded on your profile and never moves a rating: only rated labs do. Rated and unrated labs.

The briefing

ingest.service runs /opt/ingest/worker.py, which takes each file dropped into /srv/ingest/queue, appends a row to /var/lib/ingest/ledger.csv, and prints ingested <name>.

Two complaints. journalctl -fu ingest shows nothing for hours, although the ledger grows, so nobody can tell whether the worker is alive. And every deploy — which stops the service — costs a broken ledger row and a handful of files ingested twice; last month that meant duplicate invoices.

What is expected, and graded — the grader runs worker.py itself, through the INGEST_QUEUE, INGEST_LEDGER and INGEST_STATE variables the script already reads:

  1. Each ingested <name> line reaches the reader — a pipe, as the journal is — within a couple of seconds of the file being ingested, not when the program ends.
  2. Stopped with SIGTERM, the worker exits within five seconds and leaves no half-written row in the ledger and no unreadable state file.
  3. After such a stop and a restart, every file in the queue appears in the ledger exactly once.
  4. ingest.service is running and its journal shows ingested files from this boot — and still does after a reboot.

You have root through sudo. The queue keeps its files; they are removed by a different job.

What is graded

The machine is checked, rebooted, and checked again. A check passes only if it passes both times.

CheckObjective
01_progress_appears_while_it_runsMake a long-running program's output appear while it runs, not when it ends
02_a_stop_leaves_no_half_rowStop on SIGTERM without leaving half-written work behind
03_nothing_is_ingested_twiceRecord what is finished so that a restart does not do it twice
04_the_service_reports_progressMake a long-running program's output appear while it runs, not when it ends

Start it

  1. 2Labs
  2. python-04select this one
  3. udownload it
  4. sstart the machine

Reading

Where the lab's hints send you, level by level, as you ask for them (h, then l opens a journal section in the TUI).

Theory for this lab

4 questions on the same topic, in the lab's Theory tab. They never affect the lab's grade. Three of them, to answer here:

Question 1

What does this print?

import subprocess, sys
code = "import sys; print('to a pipe'); print(sys.stdout.line_buffering, sys.stdout.isatty())"
out = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True).stdout
print(" ".join(out.split()))
Question 2

A Python program with no signal handlers is running `time.sleep(30)` inside a `try: … finally: cleanup()` when it receives SIGTERM. What happens?

Question 3

A worker appends a ledger row for each item, then records the item in a state file it rewrites with `json.dump(done, open(state, "w"))`. It is stopped between the two. What does the next start do?