Force save on exit

This commit is contained in:
Fabian 2024-03-01 23:26:55 +01:00
parent 33f357587e
commit 746ff52c8a
2 changed files with 13 additions and 11 deletions

View File

@ -61,16 +61,15 @@ class Data(MutableMapping):
def _executor(self):
while self._trunning:
self._tevent.wait(save_delay)
self._save()
self.save()
def _save(self):
def save(self):
for key in self._hot_keys:
logger.info(f"saving dict {key}")
to_write = self._cache[key] # apparently thread-safe
with open(self.data_path.format(key), "w+") as f:
f.write(json.dumps(to_write))
self._hot_keys = set()
self._saving = False
def __delitem__(self, key):
return NotImplemented

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python3
import os
import signal
import sys
from functools import partial
@ -31,9 +32,9 @@ class App:
self.app = QtWidgets.QApplication(sys.argv)
self.app.setQuitOnLastWindowClosed(False)
data = Data()
self.tasks = Tasks(data)
lcd = LogCommentsData(data)
self.data = Data()
self.tasks = Tasks(self.data)
lcd = LogCommentsData(self.data)
self.log = Log(lcd)
self._active_task = self.log.last_log() or "Nothing"
@ -153,7 +154,7 @@ class App:
menu_items.append(("Settings", partial(self.open_new_dialog, self.settings)))
menu_items.append(("About", partial(self.open_new_dialog, self.about)))
menu_items.append(("Close", self.app.quit))
menu_items.append(("Close", self.quit_handler))
if self.config.flip_menu:
menu_items.reverse()
@ -169,8 +170,10 @@ class App:
action.setIcon(get_icon("go-next"))
action.triggered.connect(item[1])
def sigterm_handler(self, signo, _frame):
def quit_handler(self, signo=None, _frame=None):
if signo:
logger.debug(f'handling signal "{signal.strsignal(signo)}"')
self.data.save()
self.app.quit()
def run(self):
@ -178,8 +181,8 @@ class App:
# interrupt event loop regularly for signal handling
timer.timeout.connect(lambda: None)
timer.start(500)
signal.signal(signal.SIGTERM, self.sigterm_handler)
signal.signal(signal.SIGINT, self.sigterm_handler)
signal.signal(signal.SIGTERM, self.quit_handler)
signal.signal(signal.SIGINT, self.quit_handler)
if PYSIDE_6:
self.app.exec()
else: