From 695b5a035be7c5ceeba289f1613b5e0e5b6d2aba Mon Sep 17 00:00:00 2001 From: Lisa Junger Date: Thu, 4 Dec 2014 16:25:53 +0100 Subject: issue #172 added timeout monitoring to ajax calls. --- web-ui/app/js/features/features.js | 4 +-- web-ui/app/js/helpers/monitored_ajax.js | 49 ++++++++++++++++++++++++++ web-ui/app/js/mail_view/data/mail_sender.js | 7 ++-- web-ui/app/js/mixins/with_monitored_ajax.js | 48 +++++++++++++++++++++++++ web-ui/app/js/services/mail_service.js | 26 +++++++------- web-ui/app/js/tags/data/tags.js | 4 +-- web-ui/test/spec/services/mail_service.spec.js | 3 +- 7 files changed, 121 insertions(+), 20 deletions(-) create mode 100644 web-ui/app/js/helpers/monitored_ajax.js create mode 100644 web-ui/app/js/mixins/with_monitored_ajax.js (limited to 'web-ui') diff --git a/web-ui/app/js/features/features.js b/web-ui/app/js/features/features.js index 1a00658d..04e1fa63 100644 --- a/web-ui/app/js/features/features.js +++ b/web-ui/app/js/features/features.js @@ -16,7 +16,7 @@ */ /* global _ */ 'use strict'; -define([], function() { +define(['helpers/monitored_ajax'], function(monitoredAjax) { var cachedDisabledFeatures; var cachedDispatcherFeatures; @@ -32,7 +32,7 @@ define([], function() { function fetchFeatures() { var features; - $.ajax('/features', { + monitoredAjax(this, '/features', { async: false, success: function (results){ features = results; diff --git a/web-ui/app/js/helpers/monitored_ajax.js b/web-ui/app/js/helpers/monitored_ajax.js new file mode 100644 index 00000000..7fdd568a --- /dev/null +++ b/web-ui/app/js/helpers/monitored_ajax.js @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2014 ThoughtWorks, Inc. + * + * Pixelated is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Pixelated is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Pixelated. If not, see . + */ +/*global _ */ + +define( + ['page/events', + 'views/i18n'], + function(events, i18n) { + function monitoredAjax(on, url, config) { + if (config) { + config.timeout = 5*1000; + } + return $.ajax(url, config).fail(function(xmlhttprequest, textstatus, message) { + console.log("fails for reason: " + textstatus); + msg = "" + switch (textstatus) { + case "timeout": + msg = "a timeout occurred"; + break; + case "error": + msg = "problems talking to server"; + break; + case "parseerror": + msg = "got invalid response from server"; + break; + default: + msg = "unexpected problem while talking to server"; + } + on.trigger(document, events.ui.userAlerts.displayMessage, { message: i18n(msg) }); + }.bind(this)); + } + + return monitoredAjax; + } +); diff --git a/web-ui/app/js/mail_view/data/mail_sender.js b/web-ui/app/js/mail_view/data/mail_sender.js index 7ada0d16..f769c262 100644 --- a/web-ui/app/js/mail_view/data/mail_sender.js +++ b/web-ui/app/js/mail_view/data/mail_sender.js @@ -19,9 +19,10 @@ define( 'flight/lib/component', 'mail_view/data/mail_builder', 'page/events', + 'helpers/monitored_ajax', 'features' ], - function (defineComponent, mailBuilder, events, features) { + function (defineComponent, mailBuilder, events, monitoredAjax, features) { 'use strict'; return defineComponent(mailSender); @@ -60,7 +61,7 @@ define( }); this.sendMail = function(event, data) { - $.ajax(this.attr.mailsResource, { + monitoredAjax(this, this.attr.mailsResource, { type: 'POST', dataType: 'json', contentType: 'application/json; charset=utf-8', @@ -70,7 +71,7 @@ define( }; this.saveMail = function(mail) { - return $.ajax(this.attr.mailsResource, { + return monitoredAjax(this, this.attr.mailsResource, { type: 'PUT', dataType: 'json', contentType: 'application/json; charset=utf-8', diff --git a/web-ui/app/js/mixins/with_monitored_ajax.js b/web-ui/app/js/mixins/with_monitored_ajax.js new file mode 100644 index 00000000..2fbd1766 --- /dev/null +++ b/web-ui/app/js/mixins/with_monitored_ajax.js @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2014 ThoughtWorks, Inc. + * + * Pixelated is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Pixelated is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Pixelated. If not, see . + */ +/*global _ */ + +define( + ['page/events', + 'views/i18n'], + function(events, i18n) { + function withMonitoredAjax() { + this.monitoredAjax = function (url, config) { + config.timeout = 1; + return $.ajax(url, config).fail(function(xmlhttprequest, textstatus, message) { + console.log("fails for reason: " + textstatus); + msg = "" + switch (textstatus) { + case "timeout": + msg = "a timeout occurred"; + break; + case "error": + msg = "problems talking to server"; + break; + case "parseerror": + msg = "got invalid response from server"; + break; + default: + msg = "unexpected problem while talking to server"; + } + this.trigger(document, events.ui.userAlerts.displayMessage, { message: i18n(msg) }); + }.bind(this)); + }; + }; + + return withMonitoredAjax; + }); diff --git a/web-ui/app/js/services/mail_service.js b/web-ui/app/js/services/mail_service.js index 1383f74e..7c593885 100644 --- a/web-ui/app/js/services/mail_service.js +++ b/web-ui/app/js/services/mail_service.js @@ -22,9 +22,10 @@ define( 'flight/lib/component', 'views/i18n', 'services/model/mail', + 'helpers/monitored_ajax', 'page/events', 'features' - ], function (defineComponent, i18n, Mail, events, features) { + ], function (defineComponent, i18n, Mail, monitoredAjax, events, features) { 'use strict'; @@ -65,7 +66,7 @@ define( this.trigger(document, events.ui.userAlerts.displayMessage, { message: msg }); }; - $.ajax('/mail/' + ident + '/tags', { + monitoredAjax(this, '/mail/' + ident + '/tags', { type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify({newtags: data.tags}) @@ -79,12 +80,12 @@ define( mailIdents = _.map(data.checkedMails, function (mail) { return mail.ident; }); - $.ajax('/mails/read', { + monitoredAjax(this, '/mails/read', { type: 'POST', data: {idents: JSON.stringify(mailIdents)} }).done(this.triggerMailsRead(data.checkedMails)); } else { - $.ajax('/mail/' + data.ident + '/read', {type: 'POST'}); + monitoredAjax(this, '/mail/' + data.ident + '/read', {type: 'POST'}); } }; @@ -94,12 +95,12 @@ define( mailIdents = _.map(data.checkedMails, function (mail) { return mail.ident; }); - $.ajax('/mails/unread', { + monitoredAjax(this, '/mails/unread', { type: 'POST', data: {idents: JSON.stringify(mailIdents)} }).done(this.triggerMailsRead(data.checkedMails)); } else { - $.ajax('/mail/' + data.ident + '/read', {type: 'POST'}); + monitoredAjax(this, '/mail/' + data.ident + '/read', {type: 'POST'}); } }; @@ -124,7 +125,7 @@ define( }; this.deleteMail = function (ev, data) { - $.ajax('/mail/' + data.mail.ident, + monitoredAjax(this, '/mail/' + data.mail.ident, {type: 'DELETE'}) .done(this.triggerDeleted(data)) .fail(this.errorMessage(i18n('Could not delete email'))); @@ -136,7 +137,7 @@ define( return mail.ident; }); - $.ajax('/mails', { + monitoredAjax(this, '/mails', { type: 'DELETE', data: {idents: JSON.stringify(mailIdents)} }).done(this.triggerDeleted(dataToDelete)) @@ -197,14 +198,15 @@ define( var w = this.attr.w; var url = this.attr.mailsResource + '?q=' + escaped(this.excludeTrashedEmailsForDraftsAndSent(query)) + '&p=' + p + '&w=' + w; this.attr.lastQuery = this.excludeTrashedEmailsForDraftsAndSent(query); - $.ajax(url, { dataType: 'json' }) + monitoredAjax(this, url, { dataType: 'json' }) .done(function (data) { this.attr.numPages = Math.ceil(data.stats.total / this.attr.w); var eventToTrigger = fromRefresh ? events.mails.availableForRefresh : events.mails.available; this.trigger(document, eventToTrigger, _.merge(_.merge({tag: tag }, eventData), this.parseMails(data))); }.bind(this)) .fail(function () { - this.trigger(document, events.ui.userAlerts.displayMessage, { message: i18n('Could not fetch messages') }); + //this.trigger(document, events.ui.userAlerts.displayMessage, { message: i18n('Could not fetch messages') }); + console.log("this fail has been called, too") }.bind(this)); }; @@ -215,7 +217,7 @@ define( this.fetchSingle = function (event, data) { var fetchUrl = createSingleMailUrl(this.attr.singleMailResource, data.mail); - $.ajax(fetchUrl, { dataType: 'json' }) + monitoredAjax(this, fetchUrl, { dataType: 'json' }) .done(function (mail) { if (_.isNull(mail)) { this.trigger(data.caller, events.mail.notFound); @@ -254,7 +256,7 @@ define( return; } - $.ajax('/draft_reply_for/' + data.ident, { dataType: 'json' }) + monitoredAjax(this, '/draft_reply_for/' + data.ident, { dataType: 'json' }) .done(function (mail) { if (_.isNull(mail)) { this.trigger(document, events.mail.draftReply.notFound); diff --git a/web-ui/app/js/tags/data/tags.js b/web-ui/app/js/tags/data/tags.js index 4a1e6af0..40614fea 100644 --- a/web-ui/app/js/tags/data/tags.js +++ b/web-ui/app/js/tags/data/tags.js @@ -14,7 +14,7 @@ * You should have received a copy of the GNU Affero General Public License * along with Pixelated. If not, see . */ -define(['flight/lib/component', 'page/events', 'mixins/with_feature_toggle'], function (defineComponent, events, withFeatureToggle) { +define(['flight/lib/component', 'page/events', 'helpers/monitored_ajax', 'mixins/with_feature_toggle'], function (defineComponent, events, monitoredAjax, withFeatureToggle) { 'use strict'; var DataTags = defineComponent(dataTags, withFeatureToggle('tags', function() { @@ -49,7 +49,7 @@ define(['flight/lib/component', 'page/events', 'mixins/with_feature_toggle'], fu }); this.fetchTags = function(event, params) { - $.ajax(this.attr.tagsResource) + monitoredAjax(this, this.attr.tagsResource) .done(sendTagsBackTo(this, params)); }; diff --git a/web-ui/test/spec/services/mail_service.spec.js b/web-ui/test/spec/services/mail_service.spec.js index a76fa7c3..64eaa616 100644 --- a/web-ui/test/spec/services/mail_service.spec.js +++ b/web-ui/test/spec/services/mail_service.spec.js @@ -13,7 +13,8 @@ describeComponent('services/mail_service', function () { } ); it('marks the desired message as read', function () { - var readRequest = spyOn($, 'ajax').and.returnValue({}); + var deferred = $.Deferred() + var readRequest = spyOn($, 'ajax').and.returnValue(deferred); this.component.trigger(Pixelated.events.mail.read, {ident: 1}); -- cgit v1.2.3