11/29
Part 2 of 6 · Data model and one pathLearn and do
One path for reads, one for writes
Copied rules drift apart
Here are three scheduler methods. Each one carries its own copy of the rule for a live task.
class DriftedScheduler:
def __init__(self) -> None:
self._tasks: dict[str, Task] = {}
def schedule(self, name: str, command: str, deadline: int | None = None) -> None:
self._tasks[name] = Task(command, deadline)
def status(self, name: str, now: int) -> str | None:
task = self._tasks.get(name)
if task and (task.deadline is None or now < task.deadline):
return task.command
return None
def cancel(self, name: str, now: int) -> bool:
task = self._tasks.get(name)
# drifted to <=
if task and (task.deadline is None or now <= task.deadline):
del self._tasks[name]
return True
return False
def pending(self, now: int) -> list[str]:
# no rule at all
return sorted(self._tasks)
drifted = DriftedScheduler()
drifted.schedule("backup", "tar", deadline=100)
print(drifted.status("backup", 100))
print(drifted.pending(100))
print(drifted.cancel("backup", 100))
Each method carries its own copy of the live-task rule. backup is due at 100, and the clock reads 100. What do status, pending and cancel say?
Pick one to see what it prints.
Pick an answer above to see the rest