6
6
# Copyright (c) Jupyter Development Team.
7
7
# Distributed under the terms of the Modified BSD License.
8
8
9
+ import warnings
9
10
10
11
from datetime import timedelta
11
12
from jupyter_server ._tz import utcnow , isoformat
12
13
from terminado import NamedTermManager
13
14
from tornado import web
14
15
from tornado .ioloop import IOLoop , PeriodicCallback
15
- from traitlets import Integer
16
+ from traitlets import Integer , validate
16
17
from traitlets .config import Configurable
17
18
from ..prometheus .metrics import TERMINAL_CURRENTLY_RUNNING_TOTAL
18
19
@@ -25,7 +26,7 @@ class TerminalManager(Configurable, NamedTermManager):
25
26
_initialized_culler = False
26
27
27
28
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.
29
30
Values of 0 or lower disable culling."""
30
31
)
31
32
@@ -34,6 +35,15 @@ class TerminalManager(Configurable, NamedTermManager):
34
35
help = """The interval (in seconds) on which to check for terminals exceeding the inactive timeout value."""
35
36
)
36
37
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
+
37
47
# -------------------------------------------------------------------------
38
48
# Methods for managing terminals
39
49
# -------------------------------------------------------------------------
@@ -47,7 +57,7 @@ def create(self, **kwargs):
47
57
# more functionality per terminal, we can look into possible sub-
48
58
# classing or containment then.
49
59
term .last_activity = utcnow ()
50
- model = self .terminal_model (name )
60
+ model = self .get_terminal_model (name )
51
61
# Increase the metric by one because a new terminal was created
52
62
TERMINAL_CURRENTLY_RUNNING_TOTAL .inc ()
53
63
# Ensure culler is initialized
@@ -56,12 +66,12 @@ def create(self, **kwargs):
56
66
57
67
def get (self , name ):
58
68
"""Get terminal 'name'."""
59
- model = self .terminal_model (name )
69
+ model = self .get_terminal_model (name )
60
70
return model
61
71
62
72
def list (self ):
63
73
"""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 ]
65
75
66
76
# Update the metric below to the length of the list 'terms'
67
77
TERMINAL_CURRENTLY_RUNNING_TOTAL .set (
@@ -84,7 +94,7 @@ async def terminate_all(self):
84
94
for term in terms :
85
95
await self .terminate (term , force = True )
86
96
87
- def terminal_model (self , name ):
97
+ def get_terminal_model (self , name ):
88
98
"""Return a JSON-safe dict representing a terminal.
89
99
For use in representing terminals in the JSON APIs.
90
100
"""
@@ -108,10 +118,6 @@ def _initialize_culler(self):
108
118
if not self ._initialized_culler and self .cull_inactive_timeout > 0 :
109
119
if self ._culler_callback is None :
110
120
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
115
121
self ._culler_callback = PeriodicCallback (
116
122
self ._cull_terminals , 1000 * self .cull_interval )
117
123
self .log .info ("Culling terminals with inactivity > %s seconds at %s second intervals ..." ,
0 commit comments