2022-10-20 14:33:54 +00:00
|
|
|
import { formatLogs } from '../helpers/logHelper';
|
2019-03-21 05:46:49 +00:00
|
|
|
import { TaskViewModel } from '../models/task';
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').factory('TaskService', [
|
|
|
|
'$q',
|
|
|
|
'Task',
|
2022-10-20 14:33:54 +00:00
|
|
|
function TaskServiceFactory($q, Task) {
|
2020-04-10 21:54:53 +00:00
|
|
|
'use strict';
|
|
|
|
var service = {};
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.task = function (id) {
|
|
|
|
var deferred = $q.defer();
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
Task.get({ id: id })
|
|
|
|
.$promise.then(function success(data) {
|
|
|
|
var task = new TaskViewModel(data);
|
|
|
|
deferred.resolve(task);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve task details', err: err });
|
|
|
|
});
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.tasks = function (filters) {
|
|
|
|
var deferred = $q.defer();
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
Task.query({ filters: filters ? filters : {} })
|
|
|
|
.$promise.then(function success(data) {
|
|
|
|
var tasks = data.map(function (item) {
|
|
|
|
return new TaskViewModel(item);
|
|
|
|
});
|
|
|
|
deferred.resolve(tasks);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve tasks', err: err });
|
|
|
|
});
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.logs = function (id, stdout, stderr, timestamps, since, tail) {
|
|
|
|
var deferred = $q.defer();
|
2018-05-04 07:45:05 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
var parameters = {
|
|
|
|
id: id,
|
|
|
|
stdout: stdout || 0,
|
|
|
|
stderr: stderr || 0,
|
|
|
|
timestamps: timestamps || 0,
|
|
|
|
since: since || 0,
|
|
|
|
tail: tail || 'all',
|
|
|
|
};
|
2018-02-28 06:19:28 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
Task.logs(parameters)
|
|
|
|
.$promise.then(function success(data) {
|
2022-10-20 14:33:54 +00:00
|
|
|
var logs = formatLogs(data.logs, { stripHeaders: true, withTimestamps: !!timestamps });
|
2020-04-10 21:54:53 +00:00
|
|
|
deferred.resolve(logs);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject(err);
|
|
|
|
});
|
2018-05-04 07:45:05 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return deferred.promise;
|
|
|
|
};
|
2018-02-28 06:19:28 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return service;
|
|
|
|
},
|
|
|
|
]);
|