Skip to content

Commit

Permalink
Backup history file on startup
Browse files Browse the repository at this point in the history
Prevent loosing past conversation history due to system crashes, etc.

Ref. isamert#112
  • Loading branch information
exquo committed Sep 25, 2021
1 parent ed6cbea commit 7d59fed
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions scli
Original file line number Diff line number Diff line change
Expand Up @@ -1648,15 +1648,26 @@ class ChatsData:
return tuple(o)
raise

with open(self._history, 'w') as history_file:
json.dump(items, history_file, ensure_ascii=False, cls=JSONSetEncoder, indent=2)
with open(self._history, 'w') as history_fileobj:
json.dump(items, history_fileobj, ensure_ascii=False, cls=JSONSetEncoder, indent=2)

def _load_history(self):
if not self._history or not os.path.exists(self._history):
history_backup_filename = self._history + '.bak'
for history_filename in (self._history, history_backup_filename):
try:
with open(history_filename, 'r') as history_fileobj:
history = json.load(history_fileobj)
except (FileNotFoundError, json.JSONDecodeError) as err:
if isinstance(err, json.JSONDecodeError):
logging.error("History file corrupted, attempting to read from backup.")
continue
else:
break
else:
logging.warning("Could not read history from file.")
return

with open(self._history, 'r') as history_file:
history = json.load(history_file)
os.replace(history_filename, history_backup_filename)
# If both `history` and `history.bak` are missing, the line above (amounting to `mv history.bak history.bak`) does not throw an error.

self.delivery_status.load(history.get('delivery_status', {}))

Expand Down

0 comments on commit 7d59fed

Please sign in to comment.