##// END OF EJS Templates
Clean up session class
Jessica B. Hamrick -
Show More
@@ -10,127 +10,164 define([
10 10 "use strict";
11 11
12 12 var Session = function(options){
13 this.kernel = null;
14 13 this.id = null;
15 this.notebook = options.notebook;
16 this.events = options.notebook.events;
17 this.name = options.notebook_name;
18 this.path = options.notebook_path;
19 this.kernel_name = options.kernel_name;
14 this.notebook = {
15 name: options.notebook_name,
16 path: options.notebook_path
17 };
18 this.kernel = {
19 name: options.kernel_name
20 };
21
20 22 this.base_url = options.base_url;
21 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) {
25 var that = this;
26 var model = {
27 notebook : {
28 name : this.name,
29 path : this.path
30 },
31 kernel : {
32 name : this.kernel_name
33 }
26 this.notebook_obj = options.notebook;
27 this.events = options.events;
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 35 processData : false,
37 36 cache : false,
38 type : "POST",
39 data: JSON.stringify(model),
37 type: "GET",
40 38 dataType : "json",
41 success : function (data, status, xhr) {
42 that._handle_start_success(data);
39 success: success,
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 53 if (success) {
44 54 success(data, status, xhr);
45 55 }
46 },
47 error : function (xhr, status, err) {
48 that._handle_start_failure(xhr, status, err);
49 if (error !== undefined) {
56 };
57 var on_error = function (xhr, status, err) {
58 that.events.trigger('status_dead.Kernel');
59 if (error) {
50 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) {
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 = {
64 $.ajax(this.sessions_url, {
69 65 processData : false,
70 66 cache : false,
71 type : "PATCH",
72 data: JSON.stringify(model),
67 type: "POST",
68 data: JSON.stringify(this._get_model()),
73 69 dataType : "json",
74 error : utils.log_ajax_error,
75 };
76 var url = utils.url_join_encode(this.base_url, 'api/sessions', this.id);
77 $.ajax(url, settings);
70 success: this._on_success(on_success),
71 error: this._on_error(on_error)
72 });
78 73 };
79 74
80 Session.prototype.delete = function (success, error) {
81 var settings = {
75 /**
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 81 processData : false,
83 82 cache : false,
84 type : "DELETE",
83 type: "GET",
85 84 dataType : "json",
86 success : success,
87 error : error || utils.log_ajax_error,
88 };
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);
85 success: this._on_success(success),
86 error: this._on_error(error)
87 });
95 88 };
96 89
97 // Kernel related things
98 90 /**
99 * Create the Kernel object associated with this Session.
100 *
101 * @method _handle_start_success
91 * PATCH /api/sessions/[:session_id]
102 92 */
103 Session.prototype._handle_start_success = function (data, status, xhr) {
104 this.id = data.id;
105 // If we asked for 'python', the response will have 'python3' or 'python2'.
106 this.kernel_name = data.kernel.name;
107 this.events.trigger('started.Session', this);
108 var kernel_service_url = utils.url_path_join(this.base_url, "api/kernels");
109 this.kernel = new kernel.Kernel(kernel_service_url, this.ws_url, this.notebook, this.kernel_name);
110 this.kernel._kernel_started(data.kernel);
93 Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) {
94 this.notebook.name = notebook_name;
95 this.notebook.path = notebook_path;
96 this.kernel.name = kernel_name;
97
98 var url = utils.url_join_encode(this.sessions_url, this.id);
99 $.ajax(url, {
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) {
114 this.events.trigger('start_failed.Session', [this, xhr, status, error]);
110 Session.prototype.rename_notebook = function (name, path, success, 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.
119 *
120 * @method restart_kernel
115 * DELETE /api/sessions/[:session_id]
121 116 */
122 Session.prototype.restart_kernel = function () {
123 this.kernel.restart();
117 Session.prototype.kill = function (success, error) {
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() {
127 this.kernel.interrupt();
134 Session.prototype._get_model = function () {
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() {
132 this.kernel.kill();
162 Session.prototype._on_error = function (error) {
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 172 var SessionAlreadyStarting = function (message) {
136 173 this.name = "SessionAlreadyStarting";
@@ -144,6 +181,6 define([
144 181
145 182 return {
146 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