Merge pull request #1173 from NousResearch/hermes/hermes-4cde5efa
fix(cron): use atomic write in save_job_output to prevent data loss on crash
This commit is contained in:
commit
4644f71faf
1 changed files with 14 additions and 3 deletions
17
cron/jobs.py
17
cron/jobs.py
|
|
@ -431,8 +431,19 @@ def save_job_output(job_id: str, output: str):
|
||||||
timestamp = _hermes_now().strftime("%Y-%m-%d_%H-%M-%S")
|
timestamp = _hermes_now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
output_file = job_output_dir / f"{timestamp}.md"
|
output_file = job_output_dir / f"{timestamp}.md"
|
||||||
|
|
||||||
with open(output_file, 'w', encoding='utf-8') as f:
|
fd, tmp_path = tempfile.mkstemp(dir=str(job_output_dir), suffix='.tmp', prefix='.output_')
|
||||||
f.write(output)
|
try:
|
||||||
_secure_file(output_file)
|
with os.fdopen(fd, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(output)
|
||||||
|
f.flush()
|
||||||
|
os.fsync(f.fileno())
|
||||||
|
os.replace(tmp_path, output_file)
|
||||||
|
_secure_file(output_file)
|
||||||
|
except BaseException:
|
||||||
|
try:
|
||||||
|
os.unlink(tmp_path)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
raise
|
||||||
|
|
||||||
return output_file
|
return output_file
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue