Skip to content

Commit efc0f00

Browse files
minrktakluyver
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 f68e34f commit efc0f00

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

docs/source/changelog.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ We strongly recommend that you upgrade pip to version 9+ of pip before upgrading
2121
Use ``pip install pip --upgrade`` to upgrade pip. Check pip version with
2222
``pip --version``.
2323

24+
.. _release-6.0.0:
25+
26+
6.0.0
27+
-----
28+
29+
- add ``?no_track_activity=1`` argument to allow API requests
30+
to not be registered as activity (e.g. API calls by external activity monitors).
31+
- Kernels shutting down due to an idle timeout is no longer considered
32+
an activity-updating event.
33+
2434
.. _release-5.7.8:
2535

2636
5.7.8

notebook/base/handlers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,11 @@ def content_security_policy(self):
650650
def update_api_activity(self):
651651
"""Update last_activity of API requests"""
652652
# record activity of authenticated requests
653-
if self._track_activity and getattr(self, '_user_cache', None):
653+
if (
654+
self._track_activity
655+
and getattr(self, '_user_cache', None)
656+
and self.get_argument('no_track_activity', None) is None
657+
):
654658
self.settings['api_last_activity'] = utcnow()
655659

656660
def finish(self, *args, **kwargs):
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
"""Test the basic /api endpoints"""
22

3-
import requests
3+
from datetime import timedelta
44

5-
from notebook._tz import isoformat
5+
from notebook._tz import isoformat, utcnow
66
from notebook.utils import url_path_join
77
from notebook.tests.launchnotebook import NotebookTestBase
88

99

10-
class KernelAPITest(NotebookTestBase):
10+
class APITest(NotebookTestBase):
1111
"""Test the kernels web service API"""
12-
12+
1313
def _req(self, verb, path, **kwargs):
1414
r = self.request(verb, url_path_join('api', path))
1515
r.raise_for_status()
1616
return r
17-
17+
1818
def get(self, path, **kwargs):
1919
return self._req('GET', path)
20-
20+
2121
def test_get_spec(self):
2222
r = self.get('spec.yaml')
2323
assert r.text
24-
24+
2525
def test_get_status(self):
2626
r = self.get('status')
2727
data = r.json()
@@ -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.notebook.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.notebook.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

notebook/services/kernels/kernelmanager.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ def shutdown_kernel(self, kernel_id, now=False):
292292
kernel._activity_stream = None
293293
self.stop_buffering(kernel_id)
294294
self._kernel_connections.pop(kernel_id, None)
295-
self.last_kernel_activity = utcnow()
296295

297296
# Decrease the metric of number of kernels
298297
# running for the relevant kernel type by 1

0 commit comments

Comments
 (0)