HardSchedulingPython 3
Dependency Scheduler With Retries
Run a dependency graph deterministically, retry transient failures, and reject invalid graphs.
45m3 sample tests6 hidden tests
Implement schedule(tasks, run, max_attempts=2).
Requirements
tasksis a list of dictionaries withidand optionaldeps(default empty).- Deduplicate each task's dependency list when building the graph so repeated edges leave indegree accurate.
- A task can run when every unique dependency has completed.
- Pick ready tasks in lexicographic order for deterministic output.
- Call
run(task_id)for each attempt. - If
runraises, retry the same task immediately (before the next ready task) untilmax_attemptsis exhausted. - Return task IDs in successful completion order.
- Raise
ValueErrorfor cycles, missing dependencies, or duplicate task IDs. - Raise
ValueErrorifmax_attemptsis not positive (max_attempts < 1). - Raise
RuntimeErrorwhen a task fails all attempts.
Example
python
1tasks = [
2 {"id": "extract", "deps": []},
3 {"id": "embed", "deps": ["extract"]},
4 {"id": "index", "deps": ["embed"]},
5]
6
7assert schedule(tasks, lambda task_id: None) == ["extract", "embed", "index"]Constraints
- Single-threaded base version.
- Use standard-library Python only.
Editor