Skip to content

Commit

Permalink
Update scheduler.py
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored Dec 5, 2024
1 parent 14f21d1 commit 2ea0015
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions ovos_bus_client/util/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,13 @@ def __init__(self, bus, schedule_file: str = 'schedule.json', autostart: bool =

self.events = {}
self.event_lock = Lock()
clock_cache = get_xdg_cache_save_path("ovos_clock")
os.makedirs(clock_cache, exist_ok=True)
self.clock_cache = os.path.join(clock_cache, "ovos_clock_sync.ts")
self.last_sync = time.time()
if os.path.isfile(self.clock_cache):
with open(self.clock_cache) as f:
self.last_sync = float(f.read())
else:
with open(self.clock_cache, "w") as f:
f.write(str(self.last_sync))
self._dropped_events = 0

# to check if its our first connection to the internet via clock_skew
self._last_sync = time.time()
self._dropped_events = 0
self._past_date = datetime.datetime(day=1, month=12, year=2024)

# Convert Unix timestamp to human-readable datetime
pretty_last_sync = datetime.datetime.fromtimestamp(self.last_sync).strftime("%Y-%m-%d %H:%M:%S")
pretty_last_sync = datetime.datetime.fromtimestamp(self._last_sync).strftime("%Y-%m-%d %H:%M:%S")
LOG.debug(f"Last clock sync: {pretty_last_sync}")

self.bus = bus
Expand Down Expand Up @@ -214,7 +205,7 @@ def schedule_event(self, event: str, sched_time: float,
context to send when the
handler is called
"""
if datetime.datetime.fromtimestamp(self.last_sync) < self._past_date:
if datetime.datetime.fromtimestamp(self._last_sync) < self._past_date:
# this works around problems in raspOVOS images and other
# systems without RTC that didnt sync clock with the internet yet
# eg. issue demonstration without this:
Expand Down Expand Up @@ -277,15 +268,13 @@ def remove_event(self, event: str):

def handle_system_clock_sync(self, message: Message):
# clock sync, are we in the past?
if datetime.datetime.fromtimestamp(self.last_sync) < self._past_date:
if datetime.datetime.fromtimestamp(self._last_sync) < self._past_date:
LOG.warning(f"Clock was in the past!!! {self._dropped_events} scheduled events have been dropped")

self.last_sync = time.time()
self._last_sync = time.time()
# Convert Unix timestamp to human-readable datetime
pretty_last_sync = datetime.datetime.fromtimestamp(self.last_sync).strftime("%Y-%m-%d %H:%M:%S")
pretty_last_sync = datetime.datetime.fromtimestamp(self._last_sync).strftime("%Y-%m-%d %H:%M:%S")
LOG.info(f"clock sync: {pretty_last_sync}")
with open(self.clock_cache, "w") as f:
f.write(str(self.last_sync))

def remove_event_handler(self, message: Message):
"""
Expand Down

0 comments on commit 2ea0015

Please sign in to comment.