18/28
Part 4 of 6 · Tasks that wait on othersLearn and do

When a task dies, the tasks that wait on it die too

Say parse fails every attempt and goes to the dead-letter queue. It will never be done. So index, which waits on it, can never run. If the queue leaves index waiting, it waits forever, and nothing tells anyone.

The practice problem's rule: when a task dies, every task that depends on it, directly or through others, dies with it. They go into the dead-letter queue right after it, in enqueue order. That's a design choice, and it's the one to say out loud: a task that can never run should show up where people look.

To find them, walk the dependents map from the dead task. Keep a stack of tasks to visit and a set of tasks found. Pop a task, and for each task that waits on it, add it to the set and push it on the stack, unless it's already in the set. The set does two jobs. It lists each task once, though index can be reached from both parse and thumb. And it guarantees the walk stops.

Sort what you found by enqueue order at the end. The walk finds tasks in whatever order the map lists them, and the dead-letter queue needs an order you can test.

parse dies. Who goes with it?
fetchdoneparsedeadthumbindexpublish

thumb is fine: it waits only on fetch, which is done. index and publish both go.

The function doomed in the editor lists every task below a dead one. The stack and the set are set up for you. Fill in the loop that walks the dependents map.