Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heartbeat v2 #414

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions python3/vdebug/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self, ui, breakpoints):
self.__ex_handler = util.ExceptionHandler(self)
self.__session = None
self.listener = None
self.__heartbeat_timer = None

def dispatch_event(self, name, *args):
event.Dispatcher(self).dispatch_event(name, *args)
Expand All @@ -41,6 +42,23 @@ def listen(self):
else:
self.start_listener()

def heartbeat(self):
if not self.is_connected():
return

try:
res = self.session().api().status()

if str(res) in ("stopping", "stopped"):
self.dispatch_event("refresh", res)
return

self.__heartbeat_timer = threading.Timer(2.0, self.heartbeat)
self.__heartbeat_timer.daemon = True
self.__heartbeat_timer.start()
except:
self.dispatch_event("refresh", "stopped")

def start_listener(self):
self.listener = listener.Listener.create()
print("Vdebug will wait for a connection in the background")
Expand All @@ -65,6 +83,8 @@ def run(self):
self.listen()

def stop(self):
if self.__heartbeat_timer is not None:
self.__heartbeat_timer.cancel()
if self.is_connected():
self.__session.close_connection()
elif self.is_listening():
Expand All @@ -75,6 +95,8 @@ def stop(self):
self.__ui.say("Vdebug is not running")

def close(self):
if self.__heartbeat_timer is not None:
self.__heartbeat_timer.cancel()
self.stop_listening()
if self.is_connected():
self.__session.close_connection()
Expand Down Expand Up @@ -119,7 +141,11 @@ def __new_session(self):
log.Log("refresh event", log.Logger.DEBUG)
self.dispatch_event("refresh", status)

self.__heartbeat_timer = threading.Timer(2.0, self.heartbeat)
self.__heartbeat_timer.daemon = True
self.__heartbeat_timer.start()


class Session:

def __init__(self, ui, breakpoints, keymapper):
Expand Down