Bind task manager backbone app to websocket using autobahn

This commit is contained in:
Nicolas Le Goff
2014-02-26 16:05:41 +01:00
parent d3bf3f747e
commit 0a2d4fe04d
7 changed files with 222 additions and 50 deletions

View File

@@ -12,58 +12,90 @@ define([
"underscore",
"backbone",
"models/scheduler",
"common/websockets/connection",
"apps/admin/tasks-manager/views/scheduler",
"apps/admin/tasks-manager/views/tasks",
"apps/admin/tasks-manager/views/ping",
"apps/admin/tasks-manager/views/refresh",
"apps/admin/tasks-manager/collections/tasks"
], function ($, _, Backbone, Scheduler, SchedulerView, TasksView, PingView, TasksCollection) {
], function ($, _, Backbone, Scheduler, WSConnection, SchedulerView, TasksView, PingView, RefreshView, TasksCollection) {
var create = function() {
window.TaskManagerApp = {
$scope: $("#task-manager-app"),
$tasksListView : $("#tasks-list-view", this.$scope),
$schedulerView : $("#scheduler-view", this.$scope),
$pingView : $("#pingTime", this.$scope)
$tasksListView : $(".tasks-list-view", this.$scope),
$schedulerView : $(".scheduler-view", this.$scope),
$pingView : $(".ping-view", this.$scope),
$refreshView : $(".refresh-view", this.$scope),
eventAggregator: _.extend({}, Backbone.Events),
wsuri: "ws://dev.phrasea.net:9090/websockets",
wstopic: "http://phraseanet.com/topics/admin/task-manager"
};
TaskManagerApp.tasksCollection = new TasksCollection();
TaskManagerApp.Scheduler = new Scheduler();
TaskManagerApp.pingView = new PingView({
el: TaskManagerApp.$pingView
TaskManagerApp.pingView = new PingView({el: TaskManagerApp.$pingView});
TaskManagerApp.refreshView = new RefreshView({
el: TaskManagerApp.$refreshView,
pingView: TaskManagerApp.pingView,
tasksCollection: TaskManagerApp.tasksCollection,
scheduler: TaskManagerApp.Scheduler
});
}
var load = function() {
TaskManagerApp.refreshView.refreshAction();
// fetch objects
$.when.apply($, [
TaskManagerApp.tasksCollection.fetch(),
TaskManagerApp.Scheduler.fetch()
]).done(
function () {
TaskManagerApp.schedulerView = new SchedulerView({
model: TaskManagerApp.Scheduler,
el: TaskManagerApp.$schedulerView
});
TaskManagerApp.tasksView = new TasksView({
collection: TaskManagerApp.tasksCollection,
el: TaskManagerApp.$tasksListView
});
// Init & render views
TaskManagerApp.schedulerView = new SchedulerView({model: TaskManagerApp.Scheduler, el: TaskManagerApp.$schedulerView});
TaskManagerApp.tasksView = new TasksView({collection: TaskManagerApp.tasksCollection, el: TaskManagerApp.$tasksListView});
// render views
TaskManagerApp.tasksView.render();
TaskManagerApp.schedulerView.render();
// Sets connection to the web socket
var ws = new WSConnection({url:TaskManagerApp.wsuri, topic: TaskManagerApp.wstopic, eventAggregator: TaskManagerApp.eventAggregator});
ws.run();
// On ticks re-render ping view, update tasks & scheduler model
TaskManagerApp.eventAggregator.on("ws:manager-tick", function(response) {
var $this = this;
$this.pingView.render();
$this.Scheduler.set({"actual": "started", "process-id": response.message.manager["process-id"]});
_.each(response.message.jobs, function(data, id) {
var jobModel = $this.tasksCollection.get(id);
if ("undefined" !== jobModel) {
jobModel.set({"actual": data["status"], "process-id": data["process-id"]});
}
});
});
}
);
};
var initialize = function () {
create();
var regexp = /task-manager/;
$(document).ajaxComplete(function(event, request, settings) {
if ("undefined" !== typeof settings && regexp.test(settings.url)) {
TaskManagerApp.refreshView.loadState(false);
}
});
$(document).ajaxStart(function(event, request, settings) {
if ("undefined" !== typeof settings && regexp.test(settings.url)) {
TaskManagerApp.refreshView.loadState(true);
}
});
load();
};
return {
create: create,
load: load,
initialize: initialize
};
});

View File

@@ -0,0 +1,72 @@
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
define([
"jquery",
"underscore",
"backbone",
""
], function ($, _, Backbone) {
var RefreshView = Backbone.View.extend({
initialize: function(options) {
if (!"pingView" in options) {
throw "You must set the ping view"
}
this.pingView = options.pingView;
if (!"scheduler" in options) {
throw "You must set the scheduler model"
}
this.scheduler = options.scheduler;
if (!"tasksCollection" in options) {
throw "You must set the tasks collection model"
}
this.tasksCollection = options.tasksCollection;
this.refreshUrl = this.$el.data('refresh-url');
},
events: {
"click .btn-refresh": "refreshAction"
},
refreshAction: function(event) {
var $this = this;
$.ajax({
dataType: "json",
url: $this.refreshUrl,
data: {},
success: function(response) {
$this.pingView.render();
$this.scheduler.set({
"actual": response.manager["actual"],
"process-id": response.manager["process-id"],
"configuration": response.manager["configuration"]
});
_.each(response.tasks, function(data, id) {
var jobModel = $this.tasksCollection.get(id);
if ("undefined" !== jobModel) {
jobModel.set({
"actual": data["actual"],
"process-id": data["process-id"],
"configuration": data["configuration"]
});
}
});
}
});
},
loadState: function(state) {
if (state) {
$("#spinner", this.$el).addClass('icon-spinner icon-spin');
} else {
$("#spinner", this.$el).removeClass('icon-spinner icon-spin');
}
}
});
return RefreshView;
});