8
8
9
9
10
10
from datetime import timedelta
11
- from notebook ._tz import utcnow , isoformat
11
+ from jupyter_server ._tz import utcnow , isoformat
12
12
from terminado import NamedTermManager
13
13
from tornado import web
14
14
from tornado .ioloop import IOLoop , PeriodicCallback
@@ -40,8 +40,9 @@ class TerminalManager(Configurable, NamedTermManager):
40
40
def __init__ (self , * args , ** kwargs ):
41
41
super (TerminalManager , self ).__init__ (* args , ** kwargs )
42
42
43
- def create (self ):
44
- name , term = self .new_named_terminal ()
43
+ def create (self , ** kwargs ):
44
+ """Create a new terminal."""
45
+ name , term = self .new_named_terminal (** kwargs )
45
46
# Monkey-patch last-activity, similar to kernels. Should we need
46
47
# more functionality per terminal, we can look into possible sub-
47
48
# classing or containment then.
@@ -50,14 +51,16 @@ def create(self):
50
51
# Increase the metric by one because a new terminal was created
51
52
TERMINAL_CURRENTLY_RUNNING_TOTAL .inc ()
52
53
# Ensure culler is initialized
53
- self .initialize_culler ()
54
+ self ._initialize_culler ()
54
55
return model
55
56
56
57
def get (self , name ):
58
+ """Get terminal 'name'."""
57
59
model = self .terminal_model (name )
58
60
return model
59
61
60
62
def list (self ):
63
+ """Get a list of all running terminals."""
61
64
models = [self .terminal_model (name ) for name in self .terminals ]
62
65
63
66
# Update the metric below to the length of the list 'terms'
@@ -67,13 +70,20 @@ def list(self):
67
70
return models
68
71
69
72
async def terminate (self , name , force = False ):
73
+ """Terminate terminal 'name'."""
70
74
self ._check_terminal (name )
71
75
await super (TerminalManager , self ).terminate (name , force = force )
72
76
73
77
# Decrease the metric below by one
74
78
# because a terminal has been shutdown
75
79
TERMINAL_CURRENTLY_RUNNING_TOTAL .dec ()
76
80
81
+ async def terminate_all (self ):
82
+ """Terminate all terminals."""
83
+ terms = [name for name in self .terminals ]
84
+ for term in terms :
85
+ await self .terminate (term , force = True )
86
+
77
87
def terminal_model (self , name ):
78
88
"""Return a JSON-safe dict representing a terminal.
79
89
For use in representing terminals in the JSON APIs.
@@ -91,7 +101,7 @@ def _check_terminal(self, name):
91
101
if name not in self .terminals :
92
102
raise web .HTTPError (404 , u'Terminal not found: %s' % name )
93
103
94
- def initialize_culler (self ):
104
+ def _initialize_culler (self ):
95
105
"""Start culler if 'cull_inactive_timeout' is greater than zero.
96
106
Regardless of that value, set flag that we've been here.
97
107
"""
@@ -103,25 +113,25 @@ def initialize_culler(self):
103
113
self .cull_interval , self .cull_interval_default )
104
114
self .cull_interval = self .cull_interval_default
105
115
self ._culler_callback = PeriodicCallback (
106
- self .cull_terminals , 1000 * self .cull_interval )
116
+ self ._cull_terminals , 1000 * self .cull_interval )
107
117
self .log .info ("Culling terminals with inactivity > %s seconds at %s second intervals ..." ,
108
118
self .cull_inactive_timeout , self .cull_interval )
109
119
self ._culler_callback .start ()
110
120
111
121
self ._initialized_culler = True
112
122
113
- async def cull_terminals (self ):
123
+ async def _cull_terminals (self ):
114
124
self .log .debug ("Polling every %s seconds for terminals inactive for > %s seconds..." ,
115
125
self .cull_interval , self .cull_inactive_timeout )
116
126
# Create a separate list of terminals to avoid conflicting updates while iterating
117
127
for name in list (self .terminals ):
118
128
try :
119
- await self .cull_inactive_terminal (name )
129
+ await self ._cull_inactive_terminal (name )
120
130
except Exception as e :
121
131
self .log .exception ("The following exception was encountered while checking the "
122
132
"activity of terminal {}: {}" .format (name , e ))
123
133
124
- async def cull_inactive_terminal (self , name ):
134
+ async def _cull_inactive_terminal (self , name ):
125
135
try :
126
136
term = self .terminals [name ]
127
137
except KeyError :
0 commit comments