##// END OF EJS Templates
Catch interrupted poll() in terminal console...
Catch interrupted poll() in terminal console Alternative to my own PR #8108 - catch ZMQError in run_cell, and if it's caused by an interrupt, ignore it. more complex, especially if we want to handle the timeout nicely as proposed in the comments, but it's possibly also more convenient for other users of that API. Or perhaps not - I'm not sure what makes sense for other API consumers in this case. Fixes gh-8105

File last commit:

r18919:5a0249bc
r20836:0b3b28de
Show More
session.js
319 lines | 9.9 KiB | application/javascript | JavascriptLexer
Jonathan Frederic
Almost done!...
r17198 // Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
Zachary Sailer
manual rebase static/services (add session.js)
r12987
Jonathan Frederic
Almost done!...
r17198 define([
'base/js/namespace',
Jonathan Frederic
MWE,...
r17200 'jquery',
Jonathan Frederic
Almost done!...
r17198 'base/js/utils',
MinRK
remove unnecessary 'js' subdir from services...
r18422 'services/kernels/kernel',
Jonathan Frederic
Fix imports of "modules",...
r17202 ], function(IPython, $, utils, kernel) {
MinRK
review pass on multidir js
r13103 "use strict";
Jonathan Frederic
Almost done!...
r17198
Jessica B. Hamrick
Add docstrings to session.js
r18216 /**
* Session object for accessing the session REST api. The session
* should be used to start kernels and then shut them down -- for
* all other operations, the kernel object should be used.
*
* Options should include:
* - notebook_path: the path (not including name) to the notebook
* - kernel_name: the type of kernel (e.g. python3)
* - base_url: the root url of the notebook server
* - ws_url: the url to access websockets
* - notebook: Notebook object
*
* @class Session
* @param {Object} options
*/
Jessica B. Hamrick
Clean up session class
r18198 var Session = function (options) {
Zachary Sailer
Fixed session js ajax request....
r13060 this.id = null;
Jessica B. Hamrick
Fix bugs introduced into session.js
r18200 this.notebook_model = {
Jessica B. Hamrick
Clean up session class
r18198 path: options.notebook_path
};
Jessica B. Hamrick
Fix bugs introduced into session.js
r18200 this.kernel_model = {
id: null,
Jessica B. Hamrick
Clean up session class
r18198 name: options.kernel_name
};
Jonathan Frederic
Some JS test fixes
r17212 this.base_url = options.base_url;
MinRK
pass ws_url to kernel constructor...
r17308 this.ws_url = options.ws_url;
Jessica B. Hamrick
Fix shutdown test
r18206 this.session_service_url = utils.url_join_encode(this.base_url, 'api/sessions');
this.session_url = null;
Jessica B. Hamrick
Clean up session class
r18198
Jessica B. Hamrick
Fix bugs introduced into session.js
r18200 this.notebook = options.notebook;
this.kernel = null;
Jessica B. Hamrick
Add docstrings to session.js
r18216 this.events = options.notebook.events;
Jessica B. Hamrick
Better way of logging events
r18224
this.bind_events();
};
Session.prototype.bind_events = function () {
var that = this;
Jessica B. Hamrick
Fix bugs with this and that
r18239 var record_status = function (evt, info) {
console.log('Session: ' + evt.type + ' (' + info.session.id + ')');
Jessica B. Hamrick
Better way of logging events
r18224 };
Jessica B. Hamrick
Add status_ready.Kernel event and rename status_started to status_created
r18230 this.events.on('kernel_created.Session', record_status);
Jessica B. Hamrick
Better way of logging events
r18224 this.events.on('kernel_dead.Session', record_status);
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.on('kernel_killed.Session', record_status);
Jessica B. Hamrick
Handle and test for failed restart in session.js
r18234
// if the kernel dies, then also remove the session
this.events.on('kernel_dead.Kernel', function () {
that.delete();
});
Zachary Sailer
manual rebase static/services (add session.js)
r12987 };
Jessica B. Hamrick
Clean up session class
r18198
Jessica B. Hamrick
Better way of logging events
r18224
Jessica B. Hamrick
Add docstrings to session.js
r18216 // Public REST api functions
Jessica B. Hamrick
Clean up session class
r18198 /**
* GET /api/sessions
Jessica B. Hamrick
Add docstrings to session.js
r18216 *
* Get a list of the current sessions.
*
* @function list
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
Jessica B. Hamrick
Clean up session class
r18198 */
Session.prototype.list = function (success, error) {
Jessica B. Hamrick
Fix shutdown test
r18206 $.ajax(this.session_service_url, {
Jessica B. Hamrick
Clean up session class
r18198 processData: false,
cache: false,
type: "GET",
dataType: "json",
success: success,
error: this._on_error(error)
});
};
/**
* POST /api/sessions
Jessica B. Hamrick
Add docstrings to session.js
r18216 *
* Start a new session. This function can only executed once.
*
* @function start
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
Jessica B. Hamrick
Clean up session class
r18198 */
MinRK
avoid race condition when deleting/starting sessions...
r17649 Session.prototype.start = function (success, error) {
MinRK
review pass on multidir js
r13103 var that = this;
Jessica B. Hamrick
Clean up session class
r18198 var on_success = function (data, status, xhr) {
Min RK
fix event hookup for kernel changed...
r18919 if (that.kernel) {
that.kernel.name = that.kernel_model.name;
} else {
Jessica B. Hamrick
Add restart function to session
r18221 var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels");
that.kernel = new kernel.Kernel(kernel_service_url, that.ws_url, that.notebook, that.kernel_model.name);
}
Jessica B. Hamrick
Add status_ready.Kernel event and rename status_started to status_created
r18230 that.events.trigger('kernel_created.Session', {session: that, kernel: that.kernel});
that.kernel._kernel_created(data.kernel);
Jessica B. Hamrick
Clean up session class
r18198 if (success) {
success(data, status, xhr);
MinRK
review pass on multidir js
r13103 }
};
Jessica B. Hamrick
Clean up session class
r18198 var on_error = function (xhr, status, err) {
Jessica B. Hamrick
Add test for kernel_dead.Session
r18227 that.events.trigger('kernel_dead.Session', {session: that, xhr: xhr, status: status, error: err});
Jessica B. Hamrick
Clean up session class
r18198 if (error) {
error(xhr, status, err);
MinRK
improve indicators and handling of dead kernels and broken websocket connections...
r17676 }
Zachary Sailer
changes after session manager code review
r13057 };
Jessica B. Hamrick
Clean up session class
r18198
Jessica B. Hamrick
Fix shutdown test
r18206 $.ajax(this.session_service_url, {
Jessica B. Hamrick
Clean up session class
r18198 processData: false,
cache: false,
type: "POST",
data: JSON.stringify(this._get_model()),
dataType: "json",
success: this._on_success(on_success),
error: this._on_error(on_error)
});
Zachary Sailer
manual rebase static/services (add session.js)
r12987 };
Jessica B. Hamrick
Clean up session class
r18198
/**
* GET /api/sessions/[:session_id]
Jessica B. Hamrick
Add docstrings to session.js
r18216 *
* Get information about a session.
*
* @function get_info
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
Jessica B. Hamrick
Clean up session class
r18198 */
Session.prototype.get_info = function (success, error) {
Jessica B. Hamrick
Fix shutdown test
r18206 $.ajax(this.session_url, {
Jessica B. Hamrick
Clean up session class
r18198 processData: false,
cache: false,
type: "GET",
dataType: "json",
success: this._on_success(success),
error: this._on_error(error)
});
MinRK
review pass on multidir js
r13103 };
Jessica B. Hamrick
Clean up session class
r18198
/**
* PATCH /api/sessions/[:session_id]
Jessica B. Hamrick
Add docstrings to session.js
r18216 *
* Rename or move a notebook. If the given name or path are
* undefined, then they will not be changed.
*
* @function rename_notebook
Min RK
update frontend with path/name changes...
r18752 * @param {string} [path] - new notebook path
Jessica B. Hamrick
Add docstrings to session.js
r18216 * @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
Jessica B. Hamrick
Clean up session class
r18198 */
Min RK
update frontend with path/name changes...
r18752 Session.prototype.rename_notebook = function (path, success, error) {
Jessica B. Hamrick
Add docstrings to session.js
r18216 if (path !== undefined) {
this.notebook_model.path = path;
Jessica B. Hamrick
Update session tests
r18211 }
Jessica B. Hamrick
Fix shutdown test
r18206 $.ajax(this.session_url, {
Jessica B. Hamrick
Clean up session class
r18198 processData: false,
cache: false,
type: "PATCH",
data: JSON.stringify(this._get_model()),
Jessica B. Hamrick
Update session tests
r18211 dataType: "json",
Jessica B. Hamrick
Clean up session class
r18198 success: this._on_success(success),
error: this._on_error(error)
});
};
/**
* DELETE /api/sessions/[:session_id]
Jessica B. Hamrick
Add docstrings to session.js
r18216 *
* Kill the kernel and shutdown the session.
*
* @function delete
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
Jessica B. Hamrick
Clean up session class
r18198 */
Jessica B. Hamrick
Fix bugs introduced into session.js
r18200 Session.prototype.delete = function (success, error) {
Jessica B. Hamrick
Fix shutdown test
r18206 if (this.kernel) {
Jessica B. Hamrick
Rename all status_event to kernel_event
r18238 this.events.trigger('kernel_killed.Session', {session: this, kernel: this.kernel});
Jessica B. Hamrick
Fix shutdown test
r18206 this.kernel._kernel_dead();
}
Jessica B. Hamrick
Clean up session class
r18198
Jessica B. Hamrick
Fix shutdown test
r18206 $.ajax(this.session_url, {
Jessica B. Hamrick
Clean up session class
r18198 processData: false,
cache: false,
type: "DELETE",
dataType: "json",
Jessica B. Hamrick
Fix shutdown test
r18206 success: this._on_success(success),
Jessica B. Hamrick
Clean up session class
r18198 error: this._on_error(error)
});
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 };
Jessica B. Hamrick
Add docstrings to session.js
r18216
Jessica B. Hamrick
Add docstring to Session.restart
r18228 /**
* Restart the session by deleting it and the starting it
* fresh. If options are given, they can include any of the
* following:
*
* - notebook_path - the path to the notebook
* - kernel_name - the name (type) of the kernel
*
* @function restart
* @param {Object} [options] - options for the new kernel
* @param {function} [success] - function executed on ajax success
* @param {function} [error] - functon executed on ajax error
*/
Jessica B. Hamrick
Add restart function to session
r18221 Session.prototype.restart = function (options, success, error) {
var that = this;
var start = function () {
if (options && options.notebook_path) {
that.notebook_model.path = options.notebook_path;
}
if (options && options.kernel_name) {
that.kernel_model.name = options.kernel_name;
}
that.kernel_model.id = null;
that.start(success, error);
};
this.delete(start, start);
};
Jessica B. Hamrick
Add docstrings to session.js
r18216 // Helper functions
/**
Min RK
update frontend with path/name changes...
r18752 * Get the data model for the session, which includes the notebook path
* and kernel (name and id).
Jessica B. Hamrick
Add docstrings to session.js
r18216 *
* @function _get_model
* @returns {Object} - the data model
*/
Jessica B. Hamrick
Clean up session class
r18198 Session.prototype._get_model = function () {
return {
Jessica B. Hamrick
Fix bugs introduced into session.js
r18200 notebook: this.notebook_model,
kernel: this.kernel_model
Jessica B. Hamrick
Clean up session class
r18198 };
Zachary Sailer
manual rebase static/services (add session.js)
r12987 };
MinRK
improve indicators and handling of dead kernels and broken websocket connections...
r17676
Jessica B. Hamrick
Add docstrings to session.js
r18216 /**
* Update the data model from the given JSON object, which should
* have attributes of `id`, `notebook`, and/or `kernel`. If
* provided, the notebook data must include name and path, and the
* kernel data must include name and id.
*
* @function _update_model
* @param {Object} data - updated data model
*/
Jessica B. Hamrick
Clean up session class
r18198 Session.prototype._update_model = function (data) {
Jessica B. Hamrick
Fix bugs introduced into session.js
r18200 if (data && data.id) {
this.id = data.id;
Jessica B. Hamrick
Fix shutdown test
r18206 this.session_url = utils.url_join_encode(this.session_service_url, this.id);
Jessica B. Hamrick
Fix bugs introduced into session.js
r18200 }
if (data && data.notebook) {
this.notebook_model.path = data.notebook.path;
Jessica B. Hamrick
Clean up session class
r18198 }
Jessica B. Hamrick
Fix bugs introduced into session.js
r18200 if (data && data.kernel) {
this.kernel_model.name = data.kernel.name;
this.kernel_model.id = data.kernel.id;
Jessica B. Hamrick
Clean up session class
r18198 }
Zachary Sailer
manual rebase static/services (add session.js)
r12987 };
Jessica B. Hamrick
Clean up session class
r18198
Jessica B. Hamrick
Add docstrings to session.js
r18216 /**
* Handle a successful AJAX request by updating the session data
* model with the response, and then optionally calling a provided
* callback.
*
* @function _on_success
* @param {function} success - callback
*/
Jessica B. Hamrick
Clean up session class
r18198 Session.prototype._on_success = function (success) {
var that = this;
return function (data, status, xhr) {
that._update_model(data);
if (success) {
success(data, status, xhr);
}
};
Zachary Sailer
fix kill_and_exit button in notebook
r12995 };
Jessica B. Hamrick
Add docstrings to session.js
r18216 /**
* Handle a failed AJAX request by logging the error message, and
* then optionally calling a provided callback.
*
* @function _on_error
* @param {function} error - callback
*/
Jessica B. Hamrick
Clean up session class
r18198 Session.prototype._on_error = function (error) {
return function (xhr, status, err) {
utils.log_ajax_error(xhr, status, err);
if (error) {
error(xhr, status, err);
}
};
Zachary Sailer
fix kill_and_exit button in notebook
r12995 };
Jessica B. Hamrick
Clean up session class
r18198
Jessica B. Hamrick
Add docstrings to session.js
r18216 /**
* Error type indicating that the session is already starting.
*/
MinRK
avoid race condition when deleting/starting sessions...
r17649 var SessionAlreadyStarting = function (message) {
this.name = "SessionAlreadyStarting";
this.message = (message || "");
};
SessionAlreadyStarting.prototype = Error.prototype;
Jonathan Frederic
Almost done!...
r17198 // For backwards compatability.
Zachary Sailer
manual rebase static/services (add session.js)
r12987 IPython.Session = Session;
MinRK
avoid race condition when deleting/starting sessions...
r17649 return {
Session: Session,
Jessica B. Hamrick
Clean up session class
r18198 SessionAlreadyStarting: SessionAlreadyStarting
MinRK
avoid race condition when deleting/starting sessions...
r17649 };
Jonathan Frederic
Almost done!...
r17198 });