Module: RobotLab::To::AtomicFile
- Defined in:
- lib/robot_lab/to/atomic_file.rb
Overview
Crash-safe writes for run state (notes.md and friends).
An overnight run rewrites its state every iteration; a crash or kill mid-write must not leave a truncated file (notes.md is re-injected into the next robot's prompt). The pattern: serialize writers with an flock, stage the full content in a sibling temp file, fsync it, atomically rename it over the target, then fsync the directory. A crash at any point leaves either the previous complete file or the new complete file -- never a torn write.
Class Method Summary collapse
-
.append(path, text) ⇒ Object
Atomically append
texttopath(read-modify-write under lock). -
.write(path, content) ⇒ Object
Atomically replace
pathwithcontent.
Class Method Details
.append(path, text) ⇒ Object
Atomically append text to path (read-modify-write under lock).
26 27 28 29 30 31 32 33 |
# File 'lib/robot_lab/to/atomic_file.rb', line 26 def append(path, text) path = Pathname.new(path) path.parent.mkpath with_lock(path) do existing = path.exist? ? path.read : +"" replace(path, existing + text.to_s) end end |
.write(path, content) ⇒ Object
Atomically replace path with content.
19 20 21 22 23 |
# File 'lib/robot_lab/to/atomic_file.rb', line 19 def write(path, content) path = Pathname.new(path) path.parent.mkpath with_lock(path) { replace(path, content.to_s) } end |