Skip to content

Commit

Permalink
Stop backing up Tron EventBus events
Browse files Browse the repository at this point in the history
In the last N years, we've never had to do anything with the historical
files - since they're just eating up disk space, let's just go to using
a single file
  • Loading branch information
nemacysts committed Nov 10, 2023
1 parent 425b32f commit 9bb8f35
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions tron/eventbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,32 +142,25 @@ def sync_load_log(self):
duration = time.time() - started
log.info(f"log read from disk, took {duration:.4}s")

def sync_save_log(self, reason):
def sync_save_log(self, reason: str) -> None:
started = time.time()
new_file = os.path.join(self.log_dir, f"{int(started)}.pickle")
try:
with open(new_file, "xb") as f:
pickle.dump(self.event_log, f)
except FileExistsError:
log.exception(
f"unable to dump the log, file {new_file} already exists, "
f"too many updates/sec? current: {self.log_updates}, "
f"threshold: {self.log_save_updates}",
)
return False
pickle_file = os.path.join(self.log_dir, f"events.pickle")
with open(pickle_file, "wb") as f:
pickle.dump(self.event_log, f)

# XXX: cleanup symlink shenanigans once we're running with a
# XXX: single pickle file
# atomically replace `current` symlink
tmplink = os.path.join(self.log_dir, "tmp")
try:
os.remove(tmplink)
except FileNotFoundError:
pass
os.symlink(new_file, tmplink)
os.symlink(pickle_file, tmplink)
os.replace(tmplink, self.log_current)

duration = time.time() - started
log.info(f"log dumped to disk because {reason}, took {duration:.4}s")
return True

def sync_loop(self):
if not self.enabled:
Expand All @@ -192,7 +185,8 @@ def sync_process(self):
elif self.log_updates > self.log_save_updates:
save_reason = f"{self.log_save_updates} updates"

if save_reason and self.sync_save_log(save_reason):
if save_reason:
self.sync_save_log(save_reason)
self.log_last_save = time.time()
self.log_updates = 0

Expand Down

0 comments on commit 9bb8f35

Please sign in to comment.