##// END OF EJS Templates
Handle sessions where the kernel has been killed
Thomas Kluyver -
Show More
@@ -53,7 +53,6 b' class SessionManager(LoggingConfigurable):'
53 """Start a database connection"""
53 """Start a database connection"""
54 if self._connection is None:
54 if self._connection is None:
55 self._connection = sqlite3.connect(':memory:')
55 self._connection = sqlite3.connect(':memory:')
56 self._connection.row_factory = self.row_factory
57 return self._connection
56 return self._connection
58
57
59 def __del__(self):
58 def __del__(self):
@@ -141,7 +140,12 b' class SessionManager(LoggingConfigurable):'
141 query = "SELECT * FROM session WHERE %s" % (' AND '.join(conditions))
140 query = "SELECT * FROM session WHERE %s" % (' AND '.join(conditions))
142
141
143 self.cursor.execute(query, list(kwargs.values()))
142 self.cursor.execute(query, list(kwargs.values()))
144 model = self.cursor.fetchone()
143 try:
144 model = self.row_to_model(self.cursor, self.cursor.fetchone())
145 except KeyError:
146 # The kernel is missing, so the session just got deleted.
147 model = None
148
145 if model is None:
149 if model is None:
146 q = []
150 q = []
147 for key, value in kwargs.items():
151 for key, value in kwargs.items():
@@ -179,9 +183,14 b' class SessionManager(LoggingConfigurable):'
179 query = "UPDATE session SET %s WHERE session_id=?" % (', '.join(sets))
183 query = "UPDATE session SET %s WHERE session_id=?" % (', '.join(sets))
180 self.cursor.execute(query, list(kwargs.values()) + [session_id])
184 self.cursor.execute(query, list(kwargs.values()) + [session_id])
181
185
182 def row_factory(self, cursor, row):
186 def row_to_model(self, cursor, row):
183 """Takes sqlite database session row and turns it into a dictionary"""
187 """Takes sqlite database session row and turns it into a dictionary"""
184 row = sqlite3.Row(cursor, row)
188 row = sqlite3.Row(cursor, row)
189 if row['kernel_id'] not in self.kernel_manager:
190 # The kernel was killed without deleting the session. Should never occur.
191 self.delete_session(row['session_id'])
192 raise KeyError
193
185 model = {
194 model = {
186 'id': row['session_id'],
195 'id': row['session_id'],
187 'notebook': {
196 'notebook': {
@@ -196,7 +205,13 b' class SessionManager(LoggingConfigurable):'
196 """Returns a list of dictionaries containing all the information from
205 """Returns a list of dictionaries containing all the information from
197 the session database"""
206 the session database"""
198 c = self.cursor.execute("SELECT * FROM session")
207 c = self.cursor.execute("SELECT * FROM session")
199 return list(c.fetchall())
208 result = []
209 for row in c:
210 try:
211 result.append(self.row_to_model(c, row))
212 except KeyError:
213 pass
214 return result
200
215
201 def delete_session(self, session_id):
216 def delete_session(self, session_id):
202 """Deletes the row in the session database with given session_id"""
217 """Deletes the row in the session database with given session_id"""
General Comments 0
You need to be logged in to leave comments. Login now