@@ -24,18 +24,18 @@ class SessionManager(LoggingConfigurable):
2424
2525 kernel_manager = Instance ('jupyter_server.services.kernels.kernelmanager.MappingKernelManager' )
2626 contents_manager = Instance ('jupyter_server.services.contents.manager.ContentsManager' )
27-
27+
2828 # Session database initialized below
2929 _cursor = None
3030 _connection = None
3131 _columns = {'session_id' , 'path' , 'name' , 'type' , 'kernel_id' }
32-
32+
3333 @property
3434 def cursor (self ):
3535 """Start a cursor and create a database called 'session'"""
3636 if self ._cursor is None :
3737 self ._cursor = self .connection .cursor ()
38- self ._cursor .execute ("""CREATE TABLE session
38+ self ._cursor .execute ("""CREATE TABLE session
3939 (session_id, path, name, type, kernel_id)""" )
4040 return self ._cursor
4141
@@ -46,7 +46,7 @@ def connection(self):
4646 self ._connection = sqlite3 .connect (':memory:' )
4747 self ._connection .row_factory = sqlite3 .Row
4848 return self ._connection
49-
49+
5050 def close (self ):
5151 """Close the sqlite connection"""
5252 if self ._cursor is not None :
@@ -57,13 +57,13 @@ def __del__(self):
5757 """Close connection once SessionManager closes"""
5858 self .close ()
5959
60+ @gen .coroutine
6061 def session_exists (self , path ):
6162 """Check to see if the session of a given name exists"""
63+ exists = False
6264 self .cursor .execute ("SELECT * FROM session WHERE path=?" , (path ,))
6365 row = self .cursor .fetchone ()
64- if row is None :
65- return False
66- else :
66+ if row is not None :
6767 # Note, although we found a row for the session, the associated kernel may have
6868 # been culled or died unexpectedly. If that's the case, we should delete the
6969 # row, thereby terminating the session. This can be done via a call to
@@ -103,13 +103,14 @@ def start_kernel_for_session(self, session_id, path, name, type, kernel_name):
103103 # py2-compat
104104 raise gen .Return (kernel_id )
105105
106+ @gen .coroutine
106107 def save_session (self , session_id , path = None , name = None , type = None , kernel_id = None ):
107108 """Saves the items for the session with the given session_id
108-
109+
109110 Given a session_id (and any other of the arguments), this method
110111 creates a row in the sqlite session database that holds the information
111112 for a session.
112-
113+
113114 Parameters
114115 ----------
115116 session_id : str
@@ -122,7 +123,7 @@ def save_session(self, session_id, path=None, name=None, type=None, kernel_id=No
122123 the type of the session
123124 kernel_id : str
124125 a uuid for the kernel associated with this session
125-
126+
126127 Returns
127128 -------
128129 model : dict
@@ -134,9 +135,10 @@ def save_session(self, session_id, path=None, name=None, type=None, kernel_id=No
134135 result = yield maybe_future (self .get_session (session_id = session_id ))
135136 raise gen .Return (result )
136137
138+ @gen .coroutine
137139 def get_session (self , ** kwargs ):
138140 """Returns the model for a particular session.
139-
141+
140142 Takes a keyword argument and searches for the value in the session
141143 database, then returns the rest of the session's info.
142144
@@ -149,7 +151,7 @@ def get_session(self, **kwargs):
149151 Returns
150152 -------
151153 model : dict
152- returns a dictionary that includes all the information from the
154+ returns a dictionary that includes all the information from the
153155 session described by the kwarg.
154156 """
155157 if not kwargs :
@@ -180,19 +182,20 @@ def get_session(self, **kwargs):
180182 model = yield maybe_future (self .row_to_model (row ))
181183 raise gen .Return (model )
182184
185+ @gen .coroutine
183186 def update_session (self , session_id , ** kwargs ):
184187 """Updates the values in the session database.
185-
188+
186189 Changes the values of the session with the given session_id
187- with the values from the keyword arguments.
188-
190+ with the values from the keyword arguments.
191+
189192 Parameters
190193 ----------
191194 session_id : str
192195 a uuid that identifies a session in the sqlite3 database
193196 **kwargs : str
194197 the key must correspond to a column title in session database,
195- and the value replaces the current value in the session
198+ and the value replaces the current value in the session
196199 with session_id.
197200 """
198201 yield maybe_future (self .get_session (session_id = session_id ))
@@ -209,6 +212,11 @@ def update_session(self, session_id, **kwargs):
209212 query = "UPDATE session SET %s WHERE session_id=?" % (', ' .join (sets ))
210213 self .cursor .execute (query , list (kwargs .values ()) + [session_id ])
211214
215+ def kernel_culled (self , kernel_id ):
216+ """Checks if the kernel is still considered alive and returns true if its not found. """
217+ return kernel_id not in self .kernel_manager
218+
219+ @gen .coroutine
212220 def row_to_model (self , row , tolerate_culled = False ):
213221 """Takes sqlite database session row and turns it into a dictionary"""
214222 kernel_culled = yield maybe_future (self .kernel_culled (row ['kernel_id' ]))
@@ -227,7 +235,7 @@ def row_to_model(self, row, tolerate_culled=False):
227235 format (kernel_id = row ['kernel_id' ],session_id = row ['session_id' ])
228236 if tolerate_culled :
229237 self .log .warning (msg + " Continuing..." )
230- return None
238+ raise gen . Return ( None )
231239 raise KeyError (msg )
232240
233241 kernel_model = yield maybe_future (self .kernel_manager .kernel_model (row ['kernel_id' ]))
@@ -236,13 +244,14 @@ def row_to_model(self, row, tolerate_culled=False):
236244 'path' : row ['path' ],
237245 'name' : row ['name' ],
238246 'type' : row ['type' ],
239- 'kernel' : self . kernel_manager . kernel_model ( row [ 'kernel_id' ])
247+ 'kernel' : kernel_model
240248 }
241249 if row ['type' ] == 'notebook' :
242250 # Provide the deprecated API.
243251 model ['notebook' ] = {'path' : row ['path' ], 'name' : row ['name' ]}
244- return model
252+ raise gen . Return ( model )
245253
254+ @gen .coroutine
246255 def list_sessions (self ):
247256 """Returns a list of dictionaries containing all the information from
248257 the session database"""
@@ -256,7 +265,7 @@ def list_sessions(self):
256265 result .append (model )
257266 except KeyError :
258267 pass
259- return result
268+ raise gen . Return ( result )
260269
261270 @gen .coroutine
262271 def delete_session (self , session_id ):
0 commit comments