Skip to content

Commit

Permalink
Allow ?no_track_activity=1 to opt-out of activity tracking (#4235)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
minrk authored and kevin-bates committed Sep 27, 2019
1 parent ca5f38e commit cc1084e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion jupyter_server/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,11 @@ def content_security_policy(self):
def update_api_activity(self):
"""Update last_activity of API requests"""
# record activity of authenticated requests
if self._track_activity and getattr(self, '_user_cache', None):
if (
self._track_activity
and getattr(self, '_user_cache', None)
and self.get_argument('no_track_activity', None) is None
):
self.settings['api_last_activity'] = utcnow()

def finish(self, *args, **kwargs):
Expand Down
19 changes: 17 additions & 2 deletions jupyter_server/services/api/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Test the basic /api endpoints"""

import requests
from datetime import timedelta

from jupyter_server._tz import isoformat
from jupyter_server._tz import isoformat, utcnow
from jupyter_server.utils import url_path_join
from jupyter_server.tests.launchserver import ServerTestBase

Expand Down Expand Up @@ -30,3 +30,18 @@ def test_get_status(self):
assert data['last_activity'].endswith('Z')
assert data['started'].endswith('Z')
assert data['started'] == isoformat(self.server.web_app.settings['started'])

def test_no_track_activity(self):
# initialize with old last api activity
old = utcnow() - timedelta(days=1)
settings = self.server.web_app.settings
settings['api_last_activity'] = old
# accessing status doesn't update activity
self.get('status')
assert settings['api_last_activity'] == old
# accessing with ?no_track_activity doesn't update activity
self.get('contents?no_track_activity=1')
assert settings['api_last_activity'] == old
# accessing without ?no_track_activity does update activity
self.get('contents')
assert settings['api_last_activity'] > old
1 change: 0 additions & 1 deletion jupyter_server/services/kernels/kernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ def shutdown_kernel(self, kernel_id, now=False):
kernel._activity_stream = None
self.stop_buffering(kernel_id)
self._kernel_connections.pop(kernel_id, None)
self.last_kernel_activity = utcnow()

# Decrease the metric of number of kernels
# running for the relevant kernel type by 1
Expand Down

0 comments on commit cc1084e

Please sign in to comment.