Unify last activity calculation

Thomas Kluyver 9 years ago
parent c71483ce1e
commit 6b0d542ea9

@ -333,6 +333,26 @@ class NotebookWebApplication(web.Application):
new_handlers.append((r'(.*)', Template404))
return new_handlers
def last_activity(self):
"""Get a UTC timestamp for when the server last did something.
Includes: API activity, kernel activity, kernel shutdown, and terminal
activity.
"""
sources = [
self.settings['started'],
self.settings['kernel_manager'].last_kernel_activity,
]
try:
sources.append(self.settings['api_last_activity'])
except KeyError:
pass
try:
sources.append(self.settings['terminal_last_activity'])
except KeyError:
pass
return max(sources)
class NotebookPasswordApp(JupyterApp):
"""Set a password for the notebook server.
@ -1367,25 +1387,6 @@ class NotebookApp(JupyterApp):
# mimetype always needs to be text/css, so we override it here.
mimetypes.add_type('text/css', '.css')
def last_activity(self):
"""Get a UTC timestamp for when the server last did something.
Includes: API activity, kernel activity, kernel shutdown, and terminal
activity.
"""
sources = [
self.web_app.settings['started'],
self.kernel_manager.last_kernel_activity,
]
try:
sources.append(self.web_app.settings['api_last_activity'])
except KeyError:
pass
try:
sources.append(self.web_app.settings['terminal_last_activity'])
except KeyError:
pass
return max(sources)
def shutdown_no_activity(self):
"""Shutdown server on timeout when there are no kernels or terminals."""
@ -1402,7 +1403,7 @@ class NotebookApp(JupyterApp):
return # Terminals still running
seconds_since_active = \
(utcnow() - self.last_activity()).total_seconds()
(utcnow() - self.web_app.last_activity()).total_seconds()
self.log.debug("No activity for %d seconds.",
seconds_since_active)
if seconds_since_active > self.shutdown_no_activity_timeout:

@ -33,14 +33,11 @@ class APIStatusHandler(APIHandler):
def get(self):
# if started was missing, use unix epoch
started = self.settings.get('started', utcfromtimestamp(0))
# if we've never seen API activity, use started date
api_last_activity = self.settings.get('api_last_activity', started)
started = isoformat(started)
api_last_activity = isoformat(api_last_activity)
kernels = yield gen.maybe_future(self.kernel_manager.list_kernels())
total_connections = sum(k['connections'] for k in kernels)
last_activity = max(chain([api_last_activity], [k['last_activity'] for k in kernels]))
last_activity = isoformat(self.application.last_activity())
model = {
'started': started,
'last_activity': last_activity,

Loading…
Cancel
Save