66# Copyright (c) Jupyter Development Team.
77# Distributed under the terms of the Modified BSD License.
88
9+ import warnings
910
1011from datetime import timedelta
1112from notebook ._tz import utcnow , isoformat
1213from terminado import NamedTermManager
1314from tornado import web
1415from tornado .ioloop import IOLoop , PeriodicCallback
15- from traitlets import Integer
16+ from traitlets import Integer , validate
1617from traitlets .config import Configurable
1718from ..prometheus .metrics import TERMINAL_CURRENTLY_RUNNING_TOTAL
1819
@@ -25,7 +26,7 @@ class TerminalManager(Configurable, NamedTermManager):
2526 _initialized_culler = False
2627
2728 cull_inactive_timeout = Integer (0 , config = True ,
28- help = """Timeout (in seconds) in which a terminal has been inactive and ready to be culled.
29+ help = """Timeout (in seconds) in which a terminal has been inactive and ready to be culled.
2930 Values of 0 or lower disable culling."""
3031 )
3132
@@ -34,6 +35,15 @@ class TerminalManager(Configurable, NamedTermManager):
3435 help = """The interval (in seconds) on which to check for terminals exceeding the inactive timeout value."""
3536 )
3637
38+ @validate ('cull_interval' )
39+ def _cull_interval_validate (self , proposal ):
40+ value = proposal ['value' ]
41+ if value <= 0 :
42+ warnings .warn ("Invalid value for 'cull_interval' detected ({}) - using default value ({})." .
43+ format (value , self .cull_interval_default ))
44+ value = self .cull_interval_default
45+ return value
46+
3747 # -------------------------------------------------------------------------
3848 # Methods for managing terminals
3949 # -------------------------------------------------------------------------
@@ -47,7 +57,7 @@ def create(self):
4757 # more functionality per terminal, we can look into possible sub-
4858 # classing or containment then.
4959 term .last_activity = utcnow ()
50- model = self .terminal_model (name )
60+ model = self .get_terminal_model (name )
5161 # Increase the metric by one because a new terminal was created
5262 TERMINAL_CURRENTLY_RUNNING_TOTAL .inc ()
5363 # Ensure culler is initialized
@@ -56,12 +66,12 @@ def create(self):
5666
5767 def get (self , name ):
5868 """Get terminal 'name'."""
59- model = self .terminal_model (name )
69+ model = self .get_terminal_model (name )
6070 return model
6171
6272 def list (self ):
6373 """Get a list of all running terminals."""
64- models = [self .terminal_model (name ) for name in self .terminals ]
74+ models = [self .get_terminal_model (name ) for name in self .terminals ]
6575
6676 # Update the metric below to the length of the list 'terms'
6777 TERMINAL_CURRENTLY_RUNNING_TOTAL .set (
@@ -84,7 +94,7 @@ async def terminate_all(self):
8494 for term in terms :
8595 await self .terminate (term , force = True )
8696
87- def terminal_model (self , name ):
97+ def get_terminal_model (self , name ):
8898 """Return a JSON-safe dict representing a terminal.
8999 For use in representing terminals in the JSON APIs.
90100 """
@@ -108,10 +118,6 @@ def _initialize_culler(self):
108118 if not self ._initialized_culler and self .cull_inactive_timeout > 0 :
109119 if self ._culler_callback is None :
110120 loop = IOLoop .current ()
111- if self .cull_interval <= 0 : # handle case where user set invalid value
112- self .log .warning ("Invalid value for 'cull_interval' detected (%s) - using default value (%s)." ,
113- self .cull_interval , self .cull_interval_default )
114- self .cull_interval = self .cull_interval_default
115121 self ._culler_callback = PeriodicCallback (
116122 self ._cull_terminals , 1000 * self .cull_interval )
117123 self .log .info ("Culling terminals with inactivity > %s seconds at %s second intervals ..." ,
0 commit comments