Semaphore/public/js/controllers/projects/taskRunner.js

79 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-04-17 02:20:23 +02:00
define(function () {
app.registerController('CreateTaskCtrl', ['$scope', '$http', 'Template', 'Project', function ($scope, $http, Template, Project) {
console.log(Template);
$scope.task = {};
$scope.run = function (task) {
task.template_id = Template.id;
$http.post(Project.getURL() + '/tasks', task).success(function (t) {
2016-05-08 11:36:17 +02:00
$scope.$close(t);
2016-04-17 02:20:23 +02:00
}).error(function (_, status) {
swal('Error', 'error launching task: HTTP ' + status, 'error');
});
}
}]);
app.registerController('TaskCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.raw = false;
var logData = [];
var onDestroy = [];
2016-05-08 11:36:17 +02:00
onDestroy.push($scope.$on('task.log', function (evt, data) {
var o = data.output + '\n';
var d = moment(data.time);
if (!$scope.raw) {
o = d.format('HH:mm:ss') + ': ' + o;
}
for (var i = 0; i < logData.length; i++) {
if (d.isAfter(logData[i].time)) {
// too far -- no point scanning rest of data as its in chronological order
break;
}
if (d.isSame(logData[i].time) && data.output == logData[i].output) {
return;
}
}
$scope.output_formatted += o;
2016-05-08 11:36:17 +02:00
if (!$scope.$$phase) $scope.$digest();
}));
onDestroy.push($scope.$on('task.update', function (evt, data) {
$scope.task.status = data.status;
$scope.task.start = data.start;
$scope.task.end = data.end;
if (!$scope.$$phase) $scope.$digest();
}));
$scope.reload = function () {
$http.get($scope.project.getURL() + '/tasks/' + $scope.task.id + '/output')
.success(function (output) {
logData = output;
var out = [];
output.forEach(function (o) {
var pre = '';
if (!$scope.raw) pre = moment(o.time).format('HH:mm:ss') + ': ';
out.push(pre + o.output);
});
2016-05-01 13:24:09 +02:00
$scope.output_formatted = out.join('\n') + '\n';
});
}
$scope.$watch('raw', function () {
$scope.reload();
});
$scope.$on('$destroy', function () {
logData = null;
onDestroy.forEach(function (f) {
f();
});
})
2016-04-17 02:20:23 +02:00
}]);
});