HardSchedulingPython 3
Dependency Scheduler With Retries
Run a dependency graph deterministically, retry transient failures, and reject invalid graphs.
45m3 sample tests7 hidden tests
Implement schedule(tasks, run, max_attempts=2).
Requirements
tasksis a list of dictionaries with a stringidand optional list of stringdeps(default empty).- Deduplicate each task's dependency list when building the dependency 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 an ordinary exception, retry the same task immediately (before the next ready task) untilmax_attemptsis exhausted.max_attemptscounts the initial attempt too; a return without an exception is success regardless of its return value. - 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.
The attempt limit is an integer. Invalid IDs or missing dependencies are rejected before calling run. Cycle detection may happen after runnable tasks have completed; this scheduler doesn't roll back their side effects. A failed task aborts the schedule without releasing its dependents.
Example
python
tasks = [
{"id": "extract", "deps": []},
{"id": "embed", "deps": ["extract"]},
{"id": "index", "deps": ["embed"]},
]
assert schedule(tasks, lambda task_id: None) == ["extract", "embed", "index"]
repeated = [{"id": "extract"}, {"id": "index", "deps": ["extract", "extract"]}]
assert schedule(repeated, lambda task_id: None) == ["extract", "index"]Reconsider the entire ready set after each success. If b and z are initially ready and b releases a, the order is b, a, z, rather than processing the initial ready set as a batch. If b fails once, the attempt log is b, b, a, z, but the successful completion order still contains b only once.
Constraints
- Single-threaded base version.
- Use standard-library Python only.
Editor