Skip to content

Commit

Permalink
Tried a new approach. Timer should be working now.
Browse files Browse the repository at this point in the history
  • Loading branch information
janeliu-slac committed Jun 27, 2024
1 parent 1809287 commit 06ad824
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions hutch_python/ipython_session_timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,62 @@ class IPythonSessionTimer:
Attributes
----------
curr_time: float
The current time in seconds.
max_idle_time: int
The maximum number of seconds a user session can be idle (currently set
to 172800 seconds or 48 hours).
last_active_time: float
The time of the last user activity in this session.
idle_time: float
The amount of time the user session has been idle.
'''

def __init__(self, ipython):
global max_idle_time
self.max_idle_time = max_idle_time
self.curr_time = 0
self.max_idle_time = 30 # max_idle_time
self.last_active_time = 0
self.idle_time = 0
self.user_active = False

# _set_last_active_time() function will trigger every time user runs a cell
ipython.events.register('pre_run_cell', self._set_user_active)
ipython.events.register('post_run_cell', self._set_user_inactive)

def _set_user_active(self, result):
def _set_user_active(self):
self.user_active = True
self.last_active_time = time.time()
print("pre_cell_run")

def _set_user_inactive(self, result):
def _set_user_inactive(self):
self.user_active = False
self.last_active_time = time.time()
print("post_cell_run")

def _get_time_passed(self):
self.curr_time = time.time()
self.idle_time = self.curr_time - self.last_active_time

def _timer(self, sleep_time):
time.sleep(sleep_time)

def _start_session(self):
# poll for user activity
while (1):
if not self.user_active:
time.sleep(self.max_idle_time)
if not self.user_active:
break
else:
# check if the user has become inactive once every minute
time.sleep(60)

# Close the user session
print("This hutch-python session has timed out and automatically closed. Please start a new session")

# Close this ipython session
# Check if idle_time has exceeded max_idle_time
while (self.idle_time < self.max_idle_time):

while (self.user_active):
time.sleep(6)
self.idle_time = 0

self._timer(self.max_idle_time - self.idle_time)
self._get_time_passed()

# Close the ipython session
print("This hutch-python session has timed out. Please start a new session.")

ip = get_ipython()
ip.ask_exit()
ip.pt_app.app.exit()
Expand Down

0 comments on commit 06ad824

Please sign in to comment.