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

ECS-5104 Hutch-python feature: auto-close after timeout #385

Merged
Merged
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
9e03ced
Added new ipython_session_timer.py file to automatically time out idl…
janeliu-slac Jun 22, 2024
e6197eb
Updated ipy_config.InteractiveShellApp.extensions to include 'hutch_p…
janeliu-slac Jun 22, 2024
a2eac2d
Removed test values and replaced with actual values for maximum user …
janeliu-slac Jun 22, 2024
ace5003
Removed some other test data.
janeliu-slac Jun 22, 2024
9c1d3d3
Updated to set the maximum session duration to the session_duration v…
janeliu-slac Jun 25, 2024
7ede978
Added logic for new yaml value 'session_timer'.
janeliu-slac Jun 25, 2024
4455848
Added missing 'global max_idle_time' statement in def configure_timeo…
janeliu-slac Jun 25, 2024
7e9d351
Updated with a simpler way to set or cancel session timer.
janeliu-slac Jun 26, 2024
28920dd
Removed test code.
janeliu-slac Jun 26, 2024
1809287
Fixed synatx to be more pythonic.
janeliu-slac Jun 26, 2024
06ad824
Tried a new approach. Timer should be working now.
janeliu-slac Jun 27, 2024
0edcbba
Removed test values.
janeliu-slac Jun 27, 2024
e4b1fd7
Changed time.time to time.monotonic. Updated documentation. Changed l…
janeliu-slac Jun 27, 2024
a0f611f
Added 'session_timer' to VALID_KEYS to prevent warnings on startup.
janeliu-slac Jun 27, 2024
b2c187b
Removed previous release note (named incorrectly) and added new relea…
janeliu-slac Jun 28, 2024
548b8ef
Updated hardcoded time values to floating point numbers for consisten…
janeliu-slac Jun 28, 2024
d0bfee1
Merge branch 'master' into ECS-5104_ipython_session_timer
janeliu-slac Jun 28, 2024
d1d121d
Added new ipython_session_timer.py file to automatically time out idl…
janeliu-slac Jun 22, 2024
fdf997a
Updated ipy_config.InteractiveShellApp.extensions to include 'hutch_p…
janeliu-slac Jun 22, 2024
4010d8f
Removed test values and replaced with actual values for maximum user …
janeliu-slac Jun 22, 2024
302d05e
Removed some other test data.
janeliu-slac Jun 22, 2024
f229b9c
Updated to set the maximum session duration to the session_duration v…
janeliu-slac Jun 25, 2024
53acfab
Added logic for new yaml value 'session_timer'.
janeliu-slac Jun 25, 2024
64b644f
Added missing 'global max_idle_time' statement in def configure_timeo…
janeliu-slac Jun 25, 2024
73b1e36
Updated with a simpler way to set or cancel session timer.
janeliu-slac Jun 26, 2024
cc1da97
Removed test code.
janeliu-slac Jun 26, 2024
6396513
Fixed synatx to be more pythonic.
janeliu-slac Jun 26, 2024
78b7c58
Tried a new approach. Timer should be working now.
janeliu-slac Jun 27, 2024
6ca5371
Removed test values.
janeliu-slac Jun 27, 2024
89ce8ab
Changed time.time to time.monotonic. Updated documentation. Changed l…
janeliu-slac Jun 27, 2024
21ae070
Added 'session_timer' to VALID_KEYS to prevent warnings on startup.
janeliu-slac Jun 27, 2024
5e6fb81
Removed previous release note (named incorrectly) and added new relea…
janeliu-slac Jun 28, 2024
1d8a5f3
Updated hardcoded time values to floating point numbers for consisten…
janeliu-slac Jun 28, 2024
a083e29
In the release notes removed comments in API Changes. Nothing in this…
janeliu-slac Jun 28, 2024
78c8aa4
Merge remote-tracking branch 'origin/ECS-5104_ipython_session_timer' …
janeliu-slac Jun 28, 2024
323fe52
Renamed 385-ECS-5104_Hutch-python_feature:_auto-close_after_timeout.r…
janeliu-slac Jun 28, 2024
647b1b8
Replaced get_ipython() with the 'ipython' parameter passed in to IPyt…
janeliu-slac Jul 1, 2024
3a43139
Removed self._timer() function and replaced it with simple time.sleep…
janeliu-slac Jul 2, 2024
ae7ccaa
Added unit tests for ipython_session_timer.py.
janeliu-slac Jul 3, 2024
673df98
Removed unused import 'from IPython import get_ipython'.
janeliu-slac Jul 3, 2024
186d86e
Updated comments for clarity.
janeliu-slac Jul 3, 2024
67be8ec
Updated comments for clarity.
janeliu-slac Jul 3, 2024
5ee0778
Updated comments for clarity.
janeliu-slac Jul 3, 2024
cac9e36
Fixed a bunch of items from PR comments.
janeliu-slac Jul 3, 2024
8ad5a8f
Fixed issues from PR comments.
janeliu-slac Jul 3, 2024
ca8e9b2
Added the 'while(self.user_active)' statement back.
janeliu-slac Jul 9, 2024
70d75aa
Updated unit test for _start_session() function.
janeliu-slac Jul 10, 2024
d876afa
Updated class FakeIpython to handle ask_exit and pt_app.app.exit func…
janeliu-slac Jul 10, 2024
c764479
Updated name of test_start_session function.
janeliu-slac Jul 10, 2024
c6d118f
Replace previous changes with unittest.mock.MagicMock.
janeliu-slac Jul 10, 2024
8a09d3c
Added asserts to check session_timer.ip.ask_exit and session_timer.ip…
janeliu-slac Jul 10, 2024
70252f8
Fixed a few items.
janeliu-slac Jul 10, 2024
ebfa8c7
Merge branch 'master' into ECS-5104_ipython_session_timer
janeliu-slac Jul 10, 2024
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
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
janeliu-slac marked this conversation as resolved.
Show resolved Hide resolved
self.max_idle_time = max_idle_time
self.curr_time = 0
self.max_idle_time = 30 # max_idle_time
self.last_active_time = 0
janeliu-slac marked this conversation as resolved.
Show resolved Hide resolved
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()
janeliu-slac marked this conversation as resolved.
Show resolved Hide resolved
print("pre_cell_run")
janeliu-slac marked this conversation as resolved.
Show resolved Hide resolved

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):
janeliu-slac marked this conversation as resolved.
Show resolved Hide resolved

while (self.user_active):
time.sleep(6)
janeliu-slac marked this conversation as resolved.
Show resolved Hide resolved
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
Loading