From 1cd37b80586aff800ea2f9b2dad6b442eb047c1c 2014-11-07 23:42:40 From: Min RK Date: 2014-11-07 23:42:40 Subject: [PATCH] add sticky `Connection lost` notification when reconnect process gives up --- diff --git a/IPython/html/static/notebook/js/notificationarea.js b/IPython/html/static/notebook/js/notificationarea.js index 1b50a27..9fca816 100644 --- a/IPython/html/static/notebook/js/notificationarea.js +++ b/IPython/html/static/notebook/js/notificationarea.js @@ -132,6 +132,13 @@ define([ knw.warning("Connecting to kernel"); }); + this.events.on('kernel_connection_dead.Kernel', function (evt, info) { + knw.danger("Connection lost", undefined, function () { + // schedule reconnect a short time in the future, don't reconnect immediately + setTimeout($.proxy(info.kernel.reconnect, info.kernel), 500); + }, {title: 'click to reconnect'}); + }); + this.events.on('kernel_connected.Kernel', function () { knw.info("Connected", 500); }); diff --git a/IPython/html/static/services/kernels/kernel.js b/IPython/html/static/services/kernels/kernel.js index adfabd4..77cf4b1 100644 --- a/IPython/html/static/services/kernels/kernel.js +++ b/IPython/html/static/services/kernels/kernel.js @@ -66,6 +66,7 @@ define([ this._autorestart_attempt = 0; this._reconnect_attempt = 0; + this.reconnect_limit = 7; }; /** @@ -335,7 +336,11 @@ define([ if (this.is_connected()) { return; } - this.events.trigger('kernel_reconnecting.Kernel', {kernel: this}); + this._reconnect_attempt = this._reconnect_attempt + 1; + this.events.trigger('kernel_reconnecting.Kernel', { + kernel: this, + attempt: this._reconnect_attempt, + }); this.start_channels(); }; @@ -527,20 +532,27 @@ define([ this.events.trigger('kernel_disconnected.Kernel', {kernel: this}); if (error) { console.log('WebSocket connection failed: ', ws_url); - this._reconnect_attempt = this._reconnect_attempt + 1; this.events.trigger('kernel_connection_failed.Kernel', {kernel: this, ws_url: ws_url, attempt: this._reconnect_attempt}); } - if (this._reconnect_attempt < 7) { + this._schedule_reconnect(); + }; + + Kernel.prototype._schedule_reconnect = function () { + // function to call when kernel connection is lost + // schedules reconnect, or fires 'connection_dead' if reconnect limit is hit + if (this._reconnect_attempt < this.reconnect_limit) { var timeout = Math.pow(2, this._reconnect_attempt); console.log("Connection lost, reconnecting in " + timeout + " seconds."); setTimeout($.proxy(this.reconnect, this), 1e3 * timeout); } else { - this._reconnect_attempt = 1; - this.events.trigger('kernel_connection_given_up.Kernel', {kernel: this, ws_url: ws_url, attempt: this._reconnect_attempt}); + this.events.trigger('kernel_connection_dead.Kernel', { + kernel: this, + reconnect_attempt: this._reconnect_attempt, + }); console.log("Failed to reconnect, giving up."); } }; - + /** * Close the websocket channels. After successful close, the value * in `this.channels[channel_name]` will be null.