question 285 Advanced OOP & Design · medium
Shut down whatever can be shut down
When a worker process stops, it must flush and close the resources it holds: files, caches, database sessions, written by different teams with no common base class. Write two runtime-checkable Protocols, Flushable (flush()) and Closable (close()), and shutdown(resources). For each resource, in order: flush it if it can be flushed, then close it if it can be closed. A failure (OSError) in one step is reported and the shutdown carries on with the next resource. Return one line per resource.
Protocols
@runtime_checkable Flushable with flush(self) -> None; Closable with close(self) -> NoneLine
"<ClassName>: <steps>", steps joined by ", ": flushed, closed, flush failed, close failed; "<ClassName>: nothing to do" if neither appliesRulesafter a failed flush, still try to close; catch only OSError