Skip to content

Commit

Permalink
Fixing one old bug to make update_static atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
jakep-allenai committed Oct 23, 2024
1 parent 38dc5a2 commit 4047258
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pdelfin/data/runopenaibatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,19 @@ def update_state(folder_path: str, filename: str, **kwargs):
all_state[filename]["last_checked"] = datetime.datetime.now()

state_file = os.path.join(folder_path, UPLOAD_STATE_FILENAME)
with open(state_file, "w") as f:
return json.dump(all_state, f, default=_json_datetime_encoder)
temp_file = state_file + '.tmp'

# Write to temporary file first
with open(temp_file, "w") as f:
json.dump(all_state, f, default=_json_datetime_encoder)
f.flush()
os.fsync(f.fileno())

# Atomic rename of temporary file to target file
os.replace(temp_file, state_file)

return all_state


def get_total_space_usage():
return sum(file.bytes for file in client.files.list())
Expand Down

0 comments on commit 4047258

Please sign in to comment.