Skip to content

Commit cc1084e

Browse files
minrkkevin-bates
authored andcommitted
Allow ?no_track_activity=1 to opt-out of activity tracking (#4235)
* Don't track API requests with `?no_track_activity=1` in the activity counter allows external idle-culling scripts to avoid updating the activity counter * Don't track kernel shutdown as kernel activity this causes idle-kernel shutdowns to restart the idle-shutdown timer user-requested shutdowns will still be tracked as api activity * test ?no_track_activity=1 tracking * Changelog for activity
1 parent ca5f38e commit cc1084e

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

jupyter_server/base/handlers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,11 @@ def content_security_policy(self):
575575
def update_api_activity(self):
576576
"""Update last_activity of API requests"""
577577
# record activity of authenticated requests
578-
if self._track_activity and getattr(self, '_user_cache', None):
578+
if (
579+
self._track_activity
580+
and getattr(self, '_user_cache', None)
581+
and self.get_argument('no_track_activity', None) is None
582+
):
579583
self.settings['api_last_activity'] = utcnow()
580584

581585
def finish(self, *args, **kwargs):

jupyter_server/services/api/tests/test_api.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Test the basic /api endpoints"""
22

3-
import requests
3+
from datetime import timedelta
44

5-
from jupyter_server._tz import isoformat
5+
from jupyter_server._tz import isoformat, utcnow
66
from jupyter_server.utils import url_path_join
77
from jupyter_server.tests.launchserver import ServerTestBase
88

@@ -30,3 +30,18 @@ def test_get_status(self):
3030
assert data['last_activity'].endswith('Z')
3131
assert data['started'].endswith('Z')
3232
assert data['started'] == isoformat(self.server.web_app.settings['started'])
33+
34+
def test_no_track_activity(self):
35+
# initialize with old last api activity
36+
old = utcnow() - timedelta(days=1)
37+
settings = self.server.web_app.settings
38+
settings['api_last_activity'] = old
39+
# accessing status doesn't update activity
40+
self.get('status')
41+
assert settings['api_last_activity'] == old
42+
# accessing with ?no_track_activity doesn't update activity
43+
self.get('contents?no_track_activity=1')
44+
assert settings['api_last_activity'] == old
45+
# accessing without ?no_track_activity does update activity
46+
self.get('contents')
47+
assert settings['api_last_activity'] > old

jupyter_server/services/kernels/kernelmanager.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ def shutdown_kernel(self, kernel_id, now=False):
296296
kernel._activity_stream = None
297297
self.stop_buffering(kernel_id)
298298
self._kernel_connections.pop(kernel_id, None)
299-
self.last_kernel_activity = utcnow()
300299

301300
# Decrease the metric of number of kernels
302301
# running for the relevant kernel type by 1

0 commit comments

Comments
 (0)