Permalink
Browse files

Moved 5-minute autosaves into main heartbeat.

This works around an issue where the save timer would stop firing for unexplained reasons when the user went AFK and/or closed the lid of their laptop.
  • Loading branch information...
1 parent 92aa240 commit 0c8b984cd2e9a3ea79bea9d3e7415c54bfb3f95d @FunnyMan3595 FunnyMan3595 committed Oct 3, 2009
Showing with 10 additions and 7 deletions.
  1. +10 −7 timeclock.py
View
@@ -121,6 +121,7 @@ def __init__(self, default_mode="sleep"):
self.wTree = gtk.glade.XML(self.gladefile)
self.last_tick = time.time()
+ self.last_save = 0
self._init_widgets()
self.notify = True
@@ -213,13 +214,9 @@ def mode_changed(self, widget):
if widget.get_active():
self.selectedBtn = widget
- if self.selectedBtn.mode == SLEEP and self.save_timeout:
+ if self.selectedBtn.mode == SLEEP:
gobject.source_remove(self.save_timeout)
- self.save_timeout = None
self.doSave()
- elif self.selectedBtn.mode != SLEEP and not self.save_timeout:
- # Save timer states every five minutes in case of crashes
- self.save_timeout = gobject.timeout_add(1000 * 60 * 5, self.doSave)
def reset_clicked(self, widget):
"""Callback for the reset button"""
@@ -270,14 +267,19 @@ def prefs_commit(self, widget):
def tick(self):
"""Once-per-second timeout callback for updating progress bars."""
mode = self.selectedBtn.mode
+ now = time.time()
if mode != SLEEP:
- self.used[mode] += (time.time() - self.last_tick)
+ self.used[mode] += (now - self.last_tick)
self.update_progressBars()
if self.used[mode] >= self.total[mode] and self.notify:
notify_exhaustion(mode)
- self.last_tick = time.time()
+ if now >= (self.last_save + (5 * 60)):
+ self.doSave()
+
+ self.last_tick = now
+
return True
def doSave(self):
@@ -287,6 +289,7 @@ def doSave(self):
Saves the current timer values to disk."""
pickle.dump( (CURRENT_SAVE_VERSION, self.total, self.used, self.notify),
open(SAVE_FILE, "w") )
+ self.last_save = time.time()
return True
if __name__ == '__main__':

0 comments on commit 0c8b984

Please sign in to comment.