##// END OF EJS Templates
Clean up session class
Jessica B. Hamrick -
Show More
@@ -1,149 +1,186
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'services/kernels/js/kernel',
8 'services/kernels/js/kernel',
9 ], function(IPython, $, utils, kernel) {
9 ], function(IPython, $, utils, kernel) {
10 "use strict";
10 "use strict";
11
11
12 var Session = function(options){
12 var Session = function (options) {
13 this.kernel = null;
14 this.id = null;
13 this.id = null;
15 this.notebook = options.notebook;
14 this.notebook = {
16 this.events = options.notebook.events;
15 name: options.notebook_name,
17 this.name = options.notebook_name;
16 path: options.notebook_path
18 this.path = options.notebook_path;
17 };
19 this.kernel_name = options.kernel_name;
18 this.kernel = {
19 name: options.kernel_name
20 };
21
20 this.base_url = options.base_url;
22 this.base_url = options.base_url;
21 this.ws_url = options.ws_url;
23 this.ws_url = options.ws_url;
22 };
24 this.sessions_url = utils.url_join_encode(this.base_url, 'api/sessions');
23
25
24 Session.prototype.start = function (success, error) {
26 this.notebook_obj = options.notebook;
25 var that = this;
27 this.events = options.events;
26 var model = {
27 notebook : {
28 name : this.name,
29 path : this.path
30 },
31 kernel : {
32 name : this.kernel_name
33 }
34 };
28 };
35 var settings = {
29
30 /**
31 * GET /api/sessions
32 */
33 Session.prototype.list = function (success, error) {
34 $.ajax(this.sessions_url, {
36 processData : false,
35 processData: false,
37 cache : false,
36 cache: false,
38 type : "POST",
37 type: "GET",
39 data: JSON.stringify(model),
40 dataType : "json",
38 dataType: "json",
41 success : function (data, status, xhr) {
39 success: success,
42 that._handle_start_success(data);
40 error: this._on_error(error)
41 });
42 };
43
44 /**
45 * POST /api/sessions
46 */
47 Session.prototype.start = function (success, error) {
48 var that = this;
49 var on_success = function (data, status, xhr) {
50 var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels");
51 that.kernel = new kernel.Kernel(kernel_service_url, that.ws_url, that.notebook_obj, that.kernel_name);
52 that.kernel._kernel_started(data.kernel);
43 if (success) {
53 if (success) {
44 success(data, status, xhr);
54 success(data, status, xhr);
45 }
55 }
46 },
56 };
47 error : function (xhr, status, err) {
57 var on_error = function (xhr, status, err) {
48 that._handle_start_failure(xhr, status, err);
58 that.events.trigger('status_dead.Kernel');
49 if (error !== undefined) {
59 if (error) {
50 error(xhr, status, err);
60 error(xhr, status, err);
51 }
61 }
52 utils.log_ajax_error(xhr, status, err);
53 }
54 };
55 var url = utils.url_join_encode(this.base_url, 'api/sessions');
56 $.ajax(url, settings);
57 };
62 };
58
63
59 Session.prototype.rename_notebook = function (name, path) {
64 $.ajax(this.sessions_url, {
60 this.name = name;
61 this.path = path;
62 var model = {
63 notebook : {
64 name : this.name,
65 path : this.path
66 }
67 };
68 var settings = {
69 processData : false,
65 processData: false,
70 cache : false,
66 cache: false,
71 type : "PATCH",
67 type: "POST",
72 data: JSON.stringify(model),
68 data: JSON.stringify(this._get_model()),
73 dataType : "json",
69 dataType: "json",
74 error : utils.log_ajax_error,
70 success: this._on_success(on_success),
75 };
71 error: this._on_error(on_error)
76 var url = utils.url_join_encode(this.base_url, 'api/sessions', this.id);
72 });
77 $.ajax(url, settings);
78 };
73 };
79
74
80 Session.prototype.delete = function (success, error) {
75 /**
81 var settings = {
76 * GET /api/sessions/[:session_id]
77 */
78 Session.prototype.get_info = function (success, error) {
79 var url = utils.url_join_encode(this.sessions_url, this.id);
80 $.ajax(url, {
82 processData : false,
81 processData: false,
83 cache : false,
82 cache: false,
84 type : "DELETE",
83 type: "GET",
85 dataType : "json",
84 dataType: "json",
86 success : success,
85 success: this._on_success(success),
87 error : error || utils.log_ajax_error,
86 error: this._on_error(error)
88 };
87 });
89 if (this.kernel) {
90 this.kernel.running = false;
91 this.kernel.stop_channels();
92 }
93 var url = utils.url_join_encode(this.base_url, 'api/sessions', this.id);
94 $.ajax(url, settings);
95 };
88 };
96
89
97 // Kernel related things
98 /**
90 /**
99 * Create the Kernel object associated with this Session.
91 * PATCH /api/sessions/[:session_id]
100 *
101 * @method _handle_start_success
102 */
92 */
103 Session.prototype._handle_start_success = function (data, status, xhr) {
93 Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) {
104 this.id = data.id;
94 this.notebook.name = notebook_name;
105 // If we asked for 'python', the response will have 'python3' or 'python2'.
95 this.notebook.path = notebook_path;
106 this.kernel_name = data.kernel.name;
96 this.kernel.name = kernel_name;
107 this.events.trigger('started.Session', this);
97
108 var kernel_service_url = utils.url_path_join(this.base_url, "api/kernels");
98 var url = utils.url_join_encode(this.sessions_url, this.id);
109 this.kernel = new kernel.Kernel(kernel_service_url, this.ws_url, this.notebook, this.kernel_name);
99 $.ajax(url, {
110 this.kernel._kernel_started(data.kernel);
100 processData: false,
101 cache: false,
102 type: "PATCH",
103 data: JSON.stringify(this._get_model()),
104 dataType : "json",
105 success: this._on_success(success),
106 error: this._on_error(error)
107 });
111 };
108 };
112
109
113 Session.prototype._handle_start_failure = function (xhr, status, error) {
110 Session.prototype.rename_notebook = function (name, path, success, error) {
114 this.events.trigger('start_failed.Session', [this, xhr, status, error]);
111 this.change(name, path, this.kernel.name, success, error);
115 };
112 };
116
113
117 /**
114 /**
118 * Prompt the user to restart the IPython kernel.
115 * DELETE /api/sessions/[:session_id]
119 *
120 * @method restart_kernel
121 */
116 */
122 Session.prototype.restart_kernel = function () {
117 Session.prototype.kill = function (success, error) {
123 this.kernel.restart();
118 if (this.kernel) {
119 this.kernel.running = false;
120 this.kernel.stop_channels();
121 }
122
123 var url = utils.url_join_encode(this.sessions_url, this.id);
124 $.ajax(url, {
125 processData: false,
126 cache: false,
127 type: "DELETE",
128 dataType: "json",
129 success: this._on_success(success),
130 error: this._on_error(error)
131 });
124 };
132 };
125
133
126 Session.prototype.interrupt_kernel = function() {
134 Session.prototype._get_model = function () {
127 this.kernel.interrupt();
135 return {
136 notebook: this.notebook,
137 kernel: this.kernel
138 };
128 };
139 };
129
140
141 Session.prototype._update_model = function (data) {
142 this.id = data.id;
143 if (data.notebook) {
144 this.notebook.name = data.notebook.name;
145 this.notebook.path = data.notebook.path;
146 }
147 if (data.kernel) {
148 this.kernel.name = data.kernel.name;
149 }
150 };
151
152 Session.prototype._on_success = function (success) {
153 var that = this;
154 return function (data, status, xhr) {
155 that._update_model(data);
156 if (success) {
157 success(data, status, xhr);
158 }
159 };
160 };
130
161
131 Session.prototype.kill_kernel = function() {
162 Session.prototype._on_error = function (error) {
132 this.kernel.kill();
163 return function (xhr, status, err) {
164 utils.log_ajax_error(xhr, status, err);
165 if (error) {
166 error(xhr, status, err);
167 }
133 };
168 };
169 };
170
134
171
135 var SessionAlreadyStarting = function (message) {
172 var SessionAlreadyStarting = function (message) {
136 this.name = "SessionAlreadyStarting";
173 this.name = "SessionAlreadyStarting";
137 this.message = (message || "");
174 this.message = (message || "");
138 };
175 };
139
176
140 SessionAlreadyStarting.prototype = Error.prototype;
177 SessionAlreadyStarting.prototype = Error.prototype;
141
178
142 // For backwards compatability.
179 // For backwards compatability.
143 IPython.Session = Session;
180 IPython.Session = Session;
144
181
145 return {
182 return {
146 Session: Session,
183 Session: Session,
147 SessionAlreadyStarting: SessionAlreadyStarting,
184 SessionAlreadyStarting: SessionAlreadyStarting
148 };
185 };
149 });
186 });
General Comments 0
You need to be logged in to leave comments. Login now