woodpecker/cmd/drone-server/static/scripts/services/logs.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-04-08 22:43:59 +00:00
'use strict';
(function () {
/**
* The LogService provides access to build
* log data using REST API calls.
*/
function LogService($http, $window) {
/**
* Gets a task logs.
*
* @param {string} Name of the repository.
* @param {number} Number of the build.
* @param {number} Number of the task.
*/
this.get = function(repoName, number, step) {
return $http.get('/api/repos/'+repoName+'/logs/'+number+'/'+step);
2015-04-08 22:43:59 +00:00
};
2015-04-30 02:57:43 +00:00
var callback,
events,
2015-04-30 02:57:43 +00:00
token = localStorage.getItem('access_token');
this.subscribe = function (repoName, number, step, _callback) {
callback = _callback;
var route = ['/api/stream/', repoName, '/', number, '/', step, '?access_token=', token].join('')
events = new EventSource(route, { withCredentials: true });
events.onmessage = function (event) {
2015-04-30 02:57:43 +00:00
if (callback !== undefined) {
callback(event.data);
}
};
events.onerror = function (event) {
callback = undefined;
if (events !== undefined) {
events.close();
events = undefined;
}
console.log('user event stream closed due to error.', event);
2015-04-30 02:57:43 +00:00
};
};
this.unsubscribe = function () {
callback = undefined;
if (events !== undefined) {
events.close();
events = undefined;
2015-04-30 02:57:43 +00:00
}
};
2015-04-08 22:43:59 +00:00
}
angular
.module('drone')
.service('logs', LogService);
})();